Skip to content

Commit e8711ed

Browse files
authored
Clippy warn on unnecessary wraps (RustPython#7869)
1 parent f8e0eeb commit e8711ed

72 files changed

Lines changed: 625 additions & 650 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,4 @@ inconsistent_struct_constructor = "warn"
358358
manual_is_variant_and = "warn"
359359
map_unwrap_or = "warn"
360360
must_use_candidate = "warn"
361+
unnecessary_wraps = "warn"

crates/codegen/src/compile.rs

Lines changed: 67 additions & 75 deletions
Large diffs are not rendered by default.

crates/codegen/src/string_parser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl StringParser {
6868
self.source[self.cursor..].as_bytes().first().copied()
6969
}
7070

71-
fn parse_unicode_literal(&mut self, literal_number: usize) -> Result<CodePoint, LexicalError> {
71+
fn parse_unicode_literal(&mut self, literal_number: usize) -> CodePoint {
7272
let mut p: u32 = 0u32;
7373
for i in 1..=literal_number {
7474
match self.next_char() {
@@ -79,7 +79,7 @@ impl StringParser {
7979
None => unreachable!(),
8080
}
8181
}
82-
Ok(CodePoint::from_u32(p).unwrap())
82+
CodePoint::from_u32(p).unwrap()
8383
}
8484

8585
fn parse_octet(&mut self, o: u8) -> char {
@@ -134,9 +134,9 @@ impl StringParser {
134134
't' => '\t'.into(),
135135
'v' => '\x0b'.into(),
136136
o @ '0'..='7' => self.parse_octet(o as u8).into(),
137-
'x' => self.parse_unicode_literal(2)?,
138-
'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4)?,
139-
'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8)?,
137+
'x' => self.parse_unicode_literal(2),
138+
'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4),
139+
'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8),
140140
'N' if !self.flags.is_byte_string() => self.parse_unicode_name()?.into(),
141141
// Special cases where the escape sequence is not a single character
142142
'\n' => return Ok(None),

crates/common/src/format.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,13 @@ impl FormatSpec {
580580
},
581581
};
582582

583-
self.format_sign_and_align(&AsciiStr::new(&magnitude_str), sign_str, FormatAlign::Right)
583+
Ok(
584+
self.format_sign_and_align(
585+
&AsciiStr::new(&magnitude_str),
586+
sign_str,
587+
FormatAlign::Right,
588+
),
589+
)
584590
}
585591

586592
/// Format a float with locale-aware 'n' format.
@@ -620,7 +626,13 @@ impl FormatSpec {
620626
}
621627
};
622628

623-
self.format_sign_and_align(&AsciiStr::new(&magnitude_str), sign_str, FormatAlign::Right)
629+
Ok(
630+
self.format_sign_and_align(
631+
&AsciiStr::new(&magnitude_str),
632+
sign_str,
633+
FormatAlign::Right,
634+
),
635+
)
624636
}
625637

626638
/// Format a complex number with locale-aware 'n' format.
@@ -672,7 +684,7 @@ impl FormatSpec {
672684
// No parentheses for 'n' format (CPython: add_parens=0)
673685
let magnitude_str = format!("{grouped_re}{grouped_im}");
674686

675-
self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right)
687+
Ok(self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right))
676688
}
677689

678690
pub fn format_bool(&self, input: bool) -> Result<String, FormatSpecError> {
@@ -789,7 +801,13 @@ impl FormatSpec {
789801
}
790802
};
791803
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, sign_str);
792-
self.format_sign_and_align(&AsciiStr::new(&magnitude_str), sign_str, FormatAlign::Right)
804+
Ok(
805+
self.format_sign_and_align(
806+
&AsciiStr::new(&magnitude_str),
807+
sign_str,
808+
FormatAlign::Right,
809+
),
810+
)
793811
}
794812

795813
#[inline]
@@ -863,11 +881,11 @@ impl FormatSpec {
863881
};
864882
let sign_prefix = format!("{sign_str}{prefix}");
865883
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str, &sign_prefix);
866-
self.format_sign_and_align(
884+
Ok(self.format_sign_and_align(
867885
&AsciiStr::new(&magnitude_str),
868886
&sign_prefix,
869887
FormatAlign::Right,
870-
)
888+
))
871889
}
872890

873891
pub fn format_string<T>(&self, s: &T) -> Result<String, FormatSpecError>
@@ -883,7 +901,7 @@ impl FormatSpec {
883901
Some(p) => s.deref().chars().take(p).collect(),
884902
None => s.deref().to_owned(),
885903
};
886-
self.format_sign_and_align(&truncated, "", FormatAlign::Left)
904+
Ok(self.format_sign_and_align(&truncated, "", FormatAlign::Left))
887905
}
888906
_ => {
889907
let ch = char::from(self.format_type.as_ref().unwrap());
@@ -905,7 +923,11 @@ impl FormatSpec {
905923
}
906924
match &self.fill.unwrap_or_else(|| ' '.into()).to_char() {
907925
Some('0') => Err(FormatSpecError::ZeroPadding),
908-
_ => self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right),
926+
_ => Ok(self.format_sign_and_align(
927+
&AsciiStr::new(&magnitude_str),
928+
"",
929+
FormatAlign::Right,
930+
)),
909931
}
910932
}
911933

@@ -1017,7 +1039,7 @@ impl FormatSpec {
10171039
magnitude_str: &T,
10181040
sign_str: &str,
10191041
default_align: FormatAlign,
1020-
) -> Result<String, FormatSpecError>
1042+
) -> String
10211043
where
10221044
T: CharLen + Deref<Target = str>,
10231045
{
@@ -1030,7 +1052,7 @@ impl FormatSpec {
10301052
});
10311053

10321054
let magnitude_str = magnitude_str.deref();
1033-
Ok(match align {
1055+
match align {
10341056
FormatAlign::Left => format!(
10351057
"{}{}{}",
10361058
sign_str,
@@ -1057,7 +1079,7 @@ impl FormatSpec {
10571079
Self::compute_fill_string(fill_char, right_fill_chars_needed);
10581080
format!("{left_fill_string}{sign_str}{magnitude_str}{right_fill_string}")
10591081
}
1060-
})
1082+
}
10611083
}
10621084
}
10631085

crates/derive-impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn pyclass(attr: PunctuatedNestedMeta, item: Item) -> TokenStream {
5353
#[must_use]
5454
pub fn pyexception(attr: PunctuatedNestedMeta, item: Item) -> TokenStream {
5555
if matches!(item, syn::Item::Impl(_)) {
56-
result_to_tokens(pyclass::impl_pyexception_impl(attr, item))
56+
pyclass::impl_pyexception_impl(attr, item)
5757
} else {
5858
result_to_tokens(pyclass::impl_pyexception(attr, item))
5959
}
@@ -84,7 +84,7 @@ pub fn py_freeze(input: TokenStream, compiler: &dyn Compiler) -> TokenStream {
8484

8585
#[must_use]
8686
pub fn pypayload(input: DeriveInput) -> TokenStream {
87-
result_to_tokens(pypayload::impl_pypayload(input))
87+
pypayload::impl_pypayload(input)
8888
}
8989

9090
#[must_use]

crates/derive-impl/src/pyclass.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ pub(crate) fn impl_pyclass(attr: PunctuatedNestedMeta, item: Item) -> Result<Tok
644644
// 1. no `clear`: HAS_CLEAR = HAS_TRAVERSE (default: same as traverse)
645645
// 2. `clear` or `clear = true`: HAS_CLEAR = true, try_clear calls Traverse::clear
646646
// 3. `clear = false`: HAS_CLEAR = false (rare: traverse without clear)
647-
let has_traverse = class_meta.inner()._has_key("traverse")?;
648-
let has_clear = if class_meta.inner()._has_key("clear")? {
647+
let has_traverse = class_meta.inner()._has_key("traverse");
648+
let has_clear = if class_meta.inner()._has_key("clear") {
649649
// If clear attribute is present, use its value
650650
class_meta.inner()._bool("clear")?
651651
} else {
@@ -804,9 +804,9 @@ pub(crate) fn impl_pyexception(attr: PunctuatedNestedMeta, item: Item) -> Result
804804
Ok(ret)
805805
}
806806

807-
pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> Result<TokenStream> {
807+
pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> TokenStream {
808808
let Item::Impl(imp) = item else {
809-
return Ok(item.into_token_stream());
809+
return item.into_token_stream();
810810
};
811811

812812
// Check if with(Constructor) is specified. If Constructor trait is used, don't generate slot_new
@@ -878,15 +878,15 @@ pub(crate) fn impl_pyexception_impl(attr: PunctuatedNestedMeta, item: Item) -> R
878878
quote!(, #(#extra_attrs),*)
879879
};
880880

881-
Ok(quote! {
881+
quote! {
882882
#[pyclass(flags(BASETYPE, HAS_DICT), with(#(#with_items),*) #extra_attrs_tokens)]
883883
impl #generics #self_ty {
884884
#(#items)*
885885
}
886886

887887
#slot_new
888888
#slot_init
889-
})
889+
}
890890
}
891891

892892
macro_rules! define_content_item {
@@ -1189,7 +1189,7 @@ where
11891189
args.cfgs.to_vec(),
11901190
tokens,
11911191
2,
1192-
)?;
1192+
);
11931193

11941194
Ok(())
11951195
}
@@ -1235,7 +1235,7 @@ where
12351235

12361236
args.context
12371237
.attribute_items
1238-
.add_item(ident.clone(), vec![py_name], cfgs, tokens, 1)?;
1238+
.add_item(ident.clone(), vec![py_name], cfgs, tokens, 1);
12391239

12401240
Ok(())
12411241
}
@@ -1944,12 +1944,12 @@ fn extract_impl_attrs(attr: PunctuatedNestedMeta, item: &Ident) -> Result<Extrac
19441944
fn impl_item_new<Item>(
19451945
index: usize,
19461946
attr_name: AttrName,
1947-
) -> Result<Box<dyn ImplItem<Item, AttrName = AttrName>>>
1947+
) -> Box<dyn ImplItem<Item, AttrName = AttrName>>
19481948
where
19491949
Item: ItemLike + ToTokens + GetIdent,
19501950
{
19511951
use AttrName::*;
1952-
Ok(match attr_name {
1952+
match attr_name {
19531953
attr_name @ (Method | ClassMethod | StaticMethod) => Box::new(MethodItem {
19541954
inner: ContentItemInner { index, attr_name },
19551955
}),
@@ -1968,15 +1968,15 @@ where
19681968
Member => Box::new(MemberItem {
19691969
inner: ContentItemInner { index, attr_name },
19701970
}),
1971-
})
1971+
}
19721972
}
19731973

19741974
fn attrs_to_content_items<F, R>(
19751975
attrs: &[Attribute],
19761976
item_new: F,
19771977
) -> Result<(Vec<R>, Vec<Attribute>)>
19781978
where
1979-
F: Fn(usize, AttrName) -> Result<R>,
1979+
F: Fn(usize, AttrName) -> R,
19801980
{
19811981
let mut cfgs: Vec<Attribute> = Vec::new();
19821982
let mut result = Vec::new();
@@ -2018,7 +2018,7 @@ where
20182018
}
20192019
};
20202020

2021-
result.push(item_new(i, attr_name)?);
2021+
result.push(item_new(i, attr_name));
20222022
}
20232023
Ok((result, cfgs))
20242024
}

crates/derive-impl/src/pymodule.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ impl ModuleItem for ClassItem {
816816
#set_attr
817817
},
818818
0,
819-
)?;
819+
);
820820
Ok(())
821821
}
822822
}
@@ -910,7 +910,7 @@ impl ModuleItem for StructSequenceItem {
910910
#set_attr
911911
},
912912
0,
913-
)?;
913+
);
914914
Ok(())
915915
}
916916
}
@@ -995,9 +995,9 @@ impl ModuleItem for AttributeItem {
995995
cfgs.clone(),
996996
tokens,
997997
1,
998-
)?;
998+
);
999999
Ok(())
1000-
})?;
1000+
});
10011001
return Ok(());
10021002
}
10031003
other => {
@@ -1053,7 +1053,7 @@ impl ModuleItem for AttributeItem {
10531053

10541054
args.context
10551055
.attribute_items
1056-
.add_item(ident, py_names, cfgs, tokens, 1)?;
1056+
.add_item(ident, py_names, cfgs, tokens, 1);
10571057

10581058
Ok(())
10591059
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use proc_macro2::TokenStream;
22
use quote::quote;
3-
use syn::{DeriveInput, Result};
3+
use syn::DeriveInput;
44

5-
pub(crate) fn impl_pypayload(input: DeriveInput) -> Result<TokenStream> {
5+
pub(crate) fn impl_pypayload(input: DeriveInput) -> TokenStream {
66
let ty = &input.ident;
77

8-
let ret = quote! {
8+
quote! {
99
impl ::rustpython_vm::PyPayload for #ty {
1010
#[inline]
1111
fn class(_ctx: &::rustpython_vm::vm::Context) -> &'static rustpython_vm::Py<::rustpython_vm::builtins::PyType> {
1212
<Self as ::rustpython_vm::class::StaticType>::static_type()
1313
}
1414
}
15-
};
16-
Ok(ret)
15+
}
1716
}

crates/derive-impl/src/util.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ impl ItemNursery {
4545
cfgs: Vec<Attribute>,
4646
tokens: TokenStream,
4747
sort_order: usize,
48-
) -> Result<()> {
48+
) {
4949
self.0.push(NurseryItem {
5050
attr_name,
5151
py_names,
5252
cfgs,
5353
tokens,
5454
sort_order,
5555
});
56-
Ok(())
5756
}
5857

5958
pub(crate) fn validate(self) -> Result<ValidatedItemNursery> {
@@ -216,8 +215,8 @@ impl ItemMetaInner {
216215
Ok(value)
217216
}
218217

219-
pub(crate) fn _has_key(&self, key: &str) -> Result<bool> {
220-
Ok(matches!(self.meta_map.get(key), Some((_, _))))
218+
pub(crate) fn _has_key(&self, key: &str) -> bool {
219+
matches!(self.meta_map.get(key), Some((_, _)))
221220
}
222221

223222
pub(crate) fn _bool(&self, key: &str) -> Result<bool> {

0 commit comments

Comments
 (0)