8000 Appease clippy · RustPython/RustPython@f88af65 · GitHub
[go: up one dir, main page]

Skip to content

Commit f88af65

Browse files
committed
Appease clippy
1 parent 832500d commit f88af65

File tree

10 files changed

+64
-64
lines changed

10 files changed

+64
-64
lines changed

common/src/str.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl From<Box<Wtf8>> for StrData {
159159
// doing the check is ~10x faster for ascii, and is actually only 2% slower worst case for
160160
// non-ascii; see https://github.com/RustPython/RustPython/pull/2586#issuecomment-844611532
161161
let kind = value.str_kind();
162-
unsafe { Self::new_str_unchecked(value.into(), kind) }
162+
unsafe { Self::new_str_unchecked(value, kind) }
163163
}
164164
}
165165

@@ -219,7 +219,9 @@ impl From<CodePoint> for StrData {
219219
}
220220

221221
impl StrData {
222-
/// # Safety: Given `bytes` must be valid data for given `kind`
222+
/// # Safety
223+
///
224+
/// Given `bytes` must be valid data for given `kind`
223225
pub unsafe fn new_str_unchecked(data: Box<Wtf8>, kind: StrKind) -> Self {
224226
let len = match kind {
225227
StrKind::Ascii => data.len().into(),
@@ -231,9 +233,9 @@ impl StrData {
231233
/// # Safety
232234
///
233235
/// `char_len` must be accurate.
234-
pub unsafe fn new_with_char_len(s: Box<Wtf8>, kind: StrKind, char_len: usize) -> Self {
236+
pub unsafe fn new_with_char_len(data: Box<Wtf8>, kind: StrKind, char_len: usize) -> Self {
235237
Self {
236-
data: s.into(),
238+
data,
237239
kind,
238240
len: char_len.into(),
239241
}
@@ -308,7 +310,7 @@ impl StrData {
308310
match self.as_str_kind() {
309311
PyKindStr::Ascii(s) => s[index].into(),
310312
PyKindStr::Utf8(s) => s.chars().nth(index).unwrap().into(),
311-
PyKindStr::Wtf8(w) => w.code_points().nth(index).unwrap().into(),
313+
PyKindStr::Wtf8(w) => w.code_points().nth(index).unwrap(),
312314
}
313315
}
314316
}

common/src/wtf8/mod.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
//! [WTF-8]: https://simonsapin.github.io/wtf-8
3232
//! [`OsStr`]: std::ffi::OsStr
3333
34+
#![allow(clippy::precedence, clippy::match_overlapping_arm)]
35+
3436
use core::fmt;
3537
use core::hash::{Hash, Hasher};
3638
use core::iter::FusedIterator;
@@ -74,7 +76,9 @@ impl fmt::Debug for CodePoint {
7476
impl CodePoint {
7577
/// Unsafely creates a new `CodePoint` without checking the value.
7678
///
77-
/// Only use when `value` is known to be less than or equal to 0x10FFFF.
79+
/// # Safety
80+
///
81+
/// `value` must be less than or equal to 0x10FFFF.
7882
#[inline]
7983
pub unsafe fn from_u32_unchecked(value: u32) -> CodePoint {
8084
CodePoint { value }
@@ -197,7 +201,7 @@ impl PartialEq<CodePoint> for char {
197201
///
198202
/// Similar to `String`, but can additionally contain surrogate code points
199203
/// if they’re not in a surrogate pair.
200-
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
204+
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Default)]
201205
pub struct Wtf8Buf {
202206
bytes: Vec<u8>,
203207
}
@@ -247,7 +251,7 @@ impl Wtf8Buf {
247251
/// Creates a new, empty WTF-8 string.
248252
#[inline]
249253
pub fn new() -> Wtf8Buf {
250-
Wtf8Buf { bytes: Vec::new() }
254+
Wtf8Buf::default()
251255
}
252256

253257
/// Creates a new, empty WTF-8 string with pre-allocated capacity for `capacity` bytes.
@@ -260,8 +264,9 @@ impl Wtf8Buf {
260264

261265
/// Creates a WTF-8 string from a WTF-8 byte vec.
262266
///
263-
/// Since the byte vec is not checked for valid WTF-8, this function is
264-
/// marked unsafe.
267+
/// # Safety
268+
///
269+
/// `value` must contain valid WTF-8.
265270
#[inline]
266271
pub unsafe fn from_bytes_unchecked(value: Vec<u8>) -> Wtf8Buf {
267272
Wtf8Buf { bytes: value }
@@ -279,18 +284,6 @@ impl Wtf8Buf {
279284
}
280285
}
281286

282-
/// Creates a WTF-8 string from a UTF-8 `&str` slice.
283-
///
284-
/// This copies the content of the slice.
285-
///
286-
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
287-
#[inline]
288-
pub fn from_str(s: &str) -> Wtf8Buf {
289-
Wtf8Buf {
290-
bytes: s.as_bytes().to_vec(),
291-
}
292-
}
293-
294287
pub fn clear(&mut self) {
295288
self.bytes.clear();
296289
}
@@ -540,6 +533,12 @@ impl From<String> for Wtf8Buf {
540533
}
541534
}
542535

536+
impl From<&str> for Wtf8Buf {
537+
fn from(s: &str) -> Self {
538+
Wtf8Buf::from_string(s.to_owned())
539+
}
540+
}
541+
543542
/// A borrowed slice of well-formed WTF-8 data.
544543
///
545544
/// Similar to `&str`, but can additionally contain surrogate code points
@@ -630,14 +629,15 @@ impl Wtf8 {
630629
///
631630
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
632631
#[inline]
633-
pub fn from_str(value: &str) -> &Wtf8 {
634-
unsafe { Wtf8::from_bytes_unchecked(value.as_bytes()) }
632+
pub fn new<S: AsRef<Wtf8> + ?Sized>(value: &S) -> &Wtf8 {
633+
value.as_ref()
635634
}
636635

637636
/// Creates a WTF-8 slice from a WTF-8 byte slice.
638637
///
639-
/// Since the byte slice is not checked for valid WTF-8, this functions is
640-
/// marked unsafe.
638+
/// # Safety
639+
///
640+
/// `value` must contain valid WTF-8.
641641
#[inline]
642642
pub unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 {
643643
// SAFETY: start with &[u8], end with fancy &[u8]
@@ -967,7 +967,7 @@ impl Wtf8 {
967967

968968
impl AsRef<Wtf8> for str {
969969
fn as_ref(&self) -> &Wtf8 {
970-
Wtf8::from_str(self)
970+
unsafe { Wtf8::from_bytes_unchecked(self.as_bytes()) }
971971
}
972972
}
973973

@@ -1090,6 +1090,10 @@ pub fn check_utf8_boundary(slice: &Wtf8, index: usize) {
10901090
}
10911091

10921092
/// Copied from core::str::raw::slice_unchecked
1093+
///
1094+
/// # Safety
1095+
///
1096+
/// `begin` and `end` must be within bounds and on codepoint boundaries.
10931097
#[inline]
10941098
pub unsafe fn slice_unchecked(s: &Wtf8, begin: usize, end: usize) -> &Wtf8 {
10951099
// SAFETY: memory layout of a &[u8] and &Wtf8 are the same
@@ -1158,7 +1162,7 @@ pub struct Wtf8CodePointIndices<'a> {
11581162
pub(super) iter: Wtf8CodePoints<'a>,
11591163
}
11601164

1161-
impl<'a> Iterator for Wtf8CodePointIndices<'a> {
1165+
impl Iterator for Wtf8CodePointIndices<'_> {
11621166
type Item = (usize, CodePoint);
11631167

11641168
#[inline]
@@ -1187,7 +1191,7 @@ impl<'a> Iterator for Wtf8CodePointIndices<'a> {
11871191
}
11881192
}
11891193

1190-
impl<'a> DoubleEndedIterator for Wtf8CodePointIndices<'a> {
1194+
impl DoubleEndedIterator for Wtf8CodePointIndices<'_> {
11911195
#[inline]
11921196
fn next_back(&mut self) -> Option<(usize, CodePoint)> {
11931197
self.iter.next_back().map(|ch| {
@@ -1286,8 +1290,11 @@ impl Hash for CodePoint {
12861290

12871291
// == BOX IMPLS ==
12881292

1289-
pub unsafe fn from_boxed_wtf8_unchecked(w: Box<[u8]>) -> Box<Wtf8> {
1290-
unsafe { Box::from_raw(Box::into_raw(w) as *mut Wtf8) }
1293+
/// # Safety
1294+
///
1295+
/// `value` must be valid WTF-8.
1296+
pub unsafe fn from_boxed_wtf8_unchecked(value: Box<[u8]>) -> Box<Wtf8> {
1297+
unsafe { Box::from_raw(Box::into_raw(value) as *mut Wtf8) }
12911298
}
12921299

12931300
impl Clone for Box<Wtf8> {

stdlib/src/unicodedata.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ mod unicodedata {
8686
}
8787

8888
fn check_age(&self, c: CodePoint) -> bool {
89-
c.to_char().map_or(true, |c| {
90-
Age::of(c).is_some_and(|age| age.actual() <= self.unic_version)
91-
})
89+
c.to_char()
90+
.is_none_or(|c| Age::of(c).is_some_and(|age| age.actual() <= self.unic_version))
9291
}
9392

9493
fn extract_char(

vm/src/builtins/genericalias.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl PyGenericAlias {
124124
Ok(format!(
125125
"{}[{}]",
126126
repr_item(self.origin.clone().into(), vm)?,
127-
if self.args.len() == 0 {
127+
if self.args.is_empty() {
128128
"()".to_owned()
129129
} else {
130130
self.args
@@ -261,23 +261,20 @@ fn subs_tvars(
261261
.and_then(|sub_params| {
262262
PyTupleRef::try_from_object(vm, sub_params)
263263
.ok()
264-
.and_then(|sub_params| {
265-
if sub_params.len() > 0 {
266-
let sub_args = sub_params
267-
.iter()
268-
.map(|arg| {
269-
if let Some(idx) = tuple_index(params, arg) {
270-
argitems[idx].clone()
271-
} else {
272-
arg.clone()
273-
}
274-
})
275-
.collect::<Vec<_>>();
276-
let sub_args: PyObjectRef = PyTuple::new_ref(sub_args, &vm.ctx).into();
277-
Some(obj.get_item(&*sub_args, vm))
278-
} else {
279-
None
280-
}
264+
.filter(|sub_params| !sub_params.is_empty())
265+
.map(|sub_params| {
266+
let sub_args = sub_params
267+
.iter()
268+
.map(|arg| {
269+
if let Some(idx) = tuple_index(params, arg) {
270+
argitems[idx].clone()
271+
} else {
272+
arg.clone()
273+
}
274+
})
275+
.collect::<Vec<_>>();
276+
let sub_args: PyObjectRef = PyTuple::new_ref(sub_args, &vm.ctx).into();
277+
obj.get_item(&*sub_args, vm)
281278
})
282279
})
283280
.unwrap_or(Ok(obj))

vm/src/builtins/object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn object_getstate_default(obj: &PyObject, required: bool, vm: &VirtualMachine)
159159
slots.set_item(name.as_str(), value, vm).unwrap();
160160
}
161161

162-
if slots.len() > 0 {
162+
if !slots.is_empty() {
163163
return (state, slots).to_pyresult(vm);
164164
}
165165
}

vm/src/builtins/set.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,7 @@ impl Initializer for PySet {
784784
type Args = OptionalArg<PyObjectRef>;
785785

786786
fn init(zelf: PyRef<Self>, iterable: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
787-
if zelf.len() > 0 {
788-
zelf.clear();
789-
}
787+
zelf.clear();
790788
if let OptionalArg::Present(it) = iterable {
791789
zelf.update(PosArgs::new(vec![it]), vm)?;
792790
}

vm/src/builtins/str.rs

Lines changed: 1 addition & 1 deletion
< 10000 /tbody>
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl Constructor for PyStr {
370370
impl PyStr {
371371
/// # Safety: Given `bytes` must be valid data for given `kind`
372372
unsafe fn new_str_unchecked(data: Box<Wtf8>, kind: StrKind) -> Self {
373-
unsafe { StrData::new_str_unchecked(data.into(), kind) }.into()
373+
unsafe { StrData::new_str_unchecked(data, kind) }.into()
374374
}
375375

376376
unsafe fn new_with_char_len<T: DeduceStrKind + Into<Box<Wtf8>>>(s: T, char_len: usize) -> Self {

vm/src/builtins/union.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl PyUnion {
204204
vm,
205205
)?;
206206
let mut res;
207-
if new_args.len() == 0 {
207+
if new_args.is_empty() {
208208
res = make_union(&new_args, vm);
209209
} else {
210210
res = new_args.fast_getitem(0);

vm/src/stdlib/builtins.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,9 @@ mod builtins {
619619
Ok(character) => Ok(character.to_u32()),
620620
Err(_) => {
621621
let string_len = string.char_len();
622-
return Err(vm.new_type_error(format!(
622+
Err(vm.new_type_error(format!(
623623
"ord() expected a character, but string of length {string_len} found"
624-
)));
624+
)))
625625
}
626626
},
627627
}

vm/src/vm/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,7 @@ impl VirtualMachine {
610610
let from_list = from_list.to_pyobject(self);
611611
import_func
612612
.call((module.to_owned(), globals, locals, from_list, level), self)
613-
.map_err(|exc| {
614-
import::remove_importlib_frames(self, &exc);
615-
exc
616-
})
613+
.inspect_err(|exc| import::remove_importlib_frames(self, exc))
617614
}
618615
}
619616
}

0 commit comments

Comments
 (0)
0