Skip to content

Commit 926d69b

Browse files
authored
Add some clippy lints (RustPython#7755)
1 parent 8d1c68c commit 926d69b

59 files changed

Lines changed: 292 additions & 274 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ elided_lifetimes_in_paths = "warn"
252252
alloc_instead_of_core = "warn"
253253
std_instead_of_alloc = "warn"
254254
std_instead_of_core = "warn"
255+
format_collect = "warn"
256+
from_iter_instead_of_collect = "warn"
257+
inefficient_to_string = "warn"
258+
redundant_clone = "warn"
259+
debug_assert_with_mut_call = "warn"
260+
unused_peekable = "warn"
261+
manual_is_variant_and = "warn"
262+
or_fun_call = "warn"
263+
unnested_or_patterns = "warn"
264+
255265
perf = "warn"
256266
style = "warn"
257267
complexity = "warn"

crates/codegen/src/compile.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ impl Compiler {
13741374
// Create the new compilation unit
13751375
let code_info = ir::CodeInfo {
13761376
flags,
1377-
source_path: source_path.clone(),
1377+
source_path,
13781378
private,
13791379
blocks: vec![ir::Block::default()],
13801380
current_block: BlockIdx::new(0),
@@ -6718,7 +6718,7 @@ impl Compiler {
67186718
}
67196719

67206720
// Restore the original pattern context.
6721-
*pc = old_pc.clone();
6721+
*pc = old_pc;
67226722
// Simulate Py_INCREF on pc.stores.
67236723
pc.stores = pc.stores.clone();
67246724
// In C, old_pc.fail_pop is set to NULL to avoid freeing it later.
@@ -14487,8 +14487,7 @@ def outer():
1448714487
assert!(
1448814488
matches!(
1448914489
ops.get(cleanup_idx + 1),
14490-
Some(Instruction::JumpBackwardNoInterrupt { .. })
14491-
| Some(Instruction::JumpForward { .. })
14490+
Some(Instruction::JumpBackwardNoInterrupt { .. } | Instruction::JumpForward { .. })
1449214491
),
1449314492
"expected CLEANUP_THROW to jump to shared END_SEND block, got ops={ops:?}"
1449414493
);
@@ -14533,7 +14532,7 @@ def f():
1453314532
assert!(
1453414533
matches!(
1453514534
ops.get(first_pop_except + 1),
14536-
Some(Instruction::LoadSmallInt { .. }) | Some(Instruction::LoadConst { .. })
14535+
Some(Instruction::LoadSmallInt { .. } | Instruction::LoadConst { .. })
1453714536
),
1453814537
"expected line-after-except code immediately after POP_EXCEPT, got ops={ops:?}"
1453914538
);
@@ -16145,8 +16144,10 @@ _pathseps_with_colon = {f':{s}' for s in path_separators}
1614516144
assert!(
1614616145
!ops.windows(2).any(|window| matches!(
1614716146
window,
16148-
[Instruction::LoadFast { .. }, Instruction::GetIter]
16149-
| [Instruction::LoadFastCheck { .. }, Instruction::GetIter]
16147+
[
16148+
Instruction::LoadFast { .. } | Instruction::LoadFastCheck { .. },
16149+
Instruction::GetIter
16150+
]
1615016151
)),
1615116152
"module local outer iterable should not become a fast local, got ops={ops:?}"
1615216153
);

crates/codegen/src/ir.rs

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,8 +2036,9 @@ impl CodeInfo {
20362036
let idx = i as usize;
20372037
let swap_arg = match instructions[idx].instr.real() {
20382038
Some(Instruction::Swap { .. }) => u32::from(instructions[idx].arg),
2039-
Some(Instruction::Nop)
2040-
| Some(Instruction::PopTop | Instruction::StoreFast { .. }) => {
2039+
Some(
2040+
Instruction::Nop | Instruction::PopTop | Instruction::StoreFast { .. },
2041+
) => {
20412042
i -= 1;
20422043
continue;
20432044
}
@@ -2332,8 +2333,10 @@ impl CodeInfo {
23322333
match (curr_instr, next_instr) {
23332334
// Note: StoreFast + LoadFast → StoreFastLoadFast is done in a
23342335
// later pass aligned with CPython insert_superinstructions().
2335-
(Instruction::LoadConst { .. }, Instruction::ToBool)
2336-
| (Instruction::LoadSmallInt { .. }, Instruction::ToBool) => {
2336+
(
2337+
Instruction::LoadConst { .. } | Instruction::LoadSmallInt { .. },
2338+
Instruction::ToBool,
2339+
) => {
23372340
if let Some(value) =
23382341
const_truthiness(curr_instr, curr.arg, &self.metadata)
23392342
{
@@ -2355,10 +2358,10 @@ impl CodeInfo {
23552358
curr_instr,
23562359
OpArg::new(u32::from(curr.arg) | oparg::COMPARE_OP_BOOL_MASK),
23572360
)),
2358-
(Instruction::ContainsOp { .. }, Instruction::ToBool)
2359-
| (Instruction::IsOp { .. }, Instruction::ToBool) => {
2360-
Some((curr_instr, curr.arg))
2361-
}
2361+
(
2362+
Instruction::ContainsOp { .. } | Instruction::IsOp { .. },
2363+
Instruction::ToBool,
2364+
) => Some((curr_instr, curr.arg)),
23622365
(Instruction::LoadConst { consti }, Instruction::UnaryNot) => {
23632366
let constant = &self.metadata.consts[consti.get(curr.arg).as_usize()];
23642367
match constant {
@@ -2434,8 +2437,10 @@ impl CodeInfo {
24342437

24352438
let redundant = matches!(
24362439
(curr_instr, next_instr),
2437-
(Instruction::LoadConst { .. }, Instruction::PopTop)
2438-
| (Instruction::LoadSmallInt { .. }, Instruction::PopTop)
2440+
(
2441+
Instruction::LoadConst { .. } | Instruction::LoadSmallInt { .. },
2442+
Instruction::PopTop
2443+
)
24392444
) || matches!(curr_instr, Instruction::Copy { i } if i.get(curr.arg) == 1)
24402445
&& matches!(next_instr, Instruction::PopTop);
24412446

@@ -3757,7 +3762,7 @@ impl CodeInfo {
37573762
Some(Instruction::PushExcInfo) => {
37583763
in_exception_state = true;
37593764
}
3760-
Some(Instruction::PopExcept) | Some(Instruction::Reraise { .. }) => {
3765+
Some(Instruction::PopExcept | Instruction::Reraise { .. }) => {
37613766
in_exception_state = false;
37623767
}
37633768
Some(Instruction::LoadFastBorrow { .. }) if in_exception_state => {
@@ -4458,10 +4463,10 @@ impl CodeInfo {
44584463
Some(
44594464
Instruction::StoreFast { .. }
44604465
| Instruction::StoreFastLoadFast { .. }
4461-
| Instruction::StoreFastStoreFast { .. },
4462-
)
4463-
| Some(Instruction::DeleteFast { .. })
4464-
| Some(Instruction::LoadFastAndClear { .. }) => return false,
4466+
| Instruction::StoreFastStoreFast { .. }
4467+
| Instruction::DeleteFast { .. }
4468+
| Instruction::LoadFastAndClear { .. },
4469+
) => return false,
44654470
_ => {}
44664471
}
44674472
}
@@ -4613,8 +4618,10 @@ impl CodeInfo {
46134618
{
46144619
if matches!(
46154620
extra_info.instr.real(),
4616-
Some(Instruction::LoadFastBorrow { .. })
4617-
| Some(Instruction::LoadFastBorrowLoadFastBorrow { .. })
4621+
Some(
4622+
Instruction::LoadFastBorrow { .. }
4623+
| Instruction::LoadFastBorrowLoadFastBorrow { .. }
4624+
)
46184625
) {
46194626
to_deopt.push(*extra_instr_idx);
46204627
}
@@ -4642,8 +4649,10 @@ impl CodeInfo {
46424649
{
46434650
if matches!(
46444651
tail_info.instr.real(),
4645-
Some(Instruction::LoadFastBorrow { .. })
4646-
| Some(Instruction::LoadFastBorrowLoadFastBorrow { .. })
4652+
Some(
4653+
Instruction::LoadFastBorrow { .. }
4654+
| Instruction::LoadFastBorrowLoadFastBorrow { .. }
4655+
)
46474656
) {
46484657
cross_block_deopts.push((tail_block_idx, tail_instr_idx));
46494658
}
@@ -4722,7 +4731,7 @@ impl CodeInfo {
47224731
&& predecessor_blocks.iter().copied().all(|pred_idx| {
47234732
matches!(
47244733
last_real_instr(&self.blocks[pred_idx]),
4725-
Some(Instruction::PopIter) | Some(Instruction::Swap { .. })
4734+
Some(Instruction::PopIter | Instruction::Swap { .. })
47264735
)
47274736
})
47284737
})
@@ -4743,7 +4752,7 @@ impl CodeInfo {
47434752
&& (new_instructions.iter().all(|prev: &InstructionInfo| {
47444753
matches!(
47454754
prev.instr.real(),
4746-
Some(Instruction::Swap { .. }) | Some(Instruction::PopTop)
4755+
Some(Instruction::Swap { .. } | Instruction::PopTop)
47474756
)
47484757
}) || is_cleanup_restore_prefix(&new_instructions))
47494758
{
@@ -4906,8 +4915,9 @@ impl CodeInfo {
49064915
}
49074916
new_instructions.push(info);
49084917
}
4909-
Some(Instruction::LoadFast { var_num })
4910-
| Some(Instruction::LoadFastBorrow { var_num }) => {
4918+
Some(
4919+
Instruction::LoadFast { var_num } | Instruction::LoadFastBorrow { var_num },
4920+
) => {
49114921
let var_idx = usize::from(var_num.get(info.arg));
49124922
if var_idx < nlocals && unsafe_mask[var_idx] {
49134923
info.instr = Opcode::LoadFastCheck.into();
@@ -4918,8 +4928,10 @@ impl CodeInfo {
49184928
}
49194929
new_instructions.push(info);
49204930
}
4921-
Some(Instruction::LoadFastLoadFast { var_nums })
4922-
| Some(Instruction::LoadFastBorrowLoadFastBorrow { var_nums }) => {
4931+
Some(
4932+
Instruction::LoadFastLoadFast { var_nums }
4933+
| Instruction::LoadFastBorrowLoadFastBorrow { var_nums },
4934+
) => {
49234935
let packed = var_nums.get(info.arg);
49244936
let (idx1, idx2) = packed.indexes();
49254937
let idx1 = usize::from(idx1);
@@ -6802,7 +6814,7 @@ fn deoptimize_borrow_after_push_exc_info_in_blocks(blocks: &mut [Block]) {
68026814
Some(Instruction::PushExcInfo) => {
68036815
in_exception_state = true;
68046816
}
6805-
Some(Instruction::PopExcept) | Some(Instruction::Reraise { .. }) => {
6817+
Some(Instruction::PopExcept | Instruction::Reraise { .. }) => {
68066818
in_exception_state = false;
68076819
}
68086820
Some(Instruction::LoadFastBorrow { .. }) if in_exception_state => {
@@ -6896,7 +6908,7 @@ fn is_exit_without_lineno(block: &Block) -> bool {
68966908
&& prefix.iter().all(|info| {
68976909
matches!(
68986910
info.instr.real(),
6899-
Some(Instruction::PopExcept) | Some(Instruction::Nop)
6911+
Some(Instruction::PopExcept | Instruction::Nop)
69006912
)
69016913
})
69026914
&& prefix
@@ -6924,7 +6936,7 @@ fn shared_jump_back_target(block: &Block) -> Option<BlockIdx> {
69246936
if !prefix.iter().all(|info| {
69256937
matches!(
69266938
info.instr.real(),
6927-
Some(Instruction::PopExcept) | Some(Instruction::Nop)
6939+
Some(Instruction::PopExcept | Instruction::Nop)
69286940
)
69296941
}) {
69306942
return None;

crates/codegen/src/symboltable.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,10 +1410,12 @@ impl SymbolTableBuilder {
14101410
let parent_scope_typ = self.tables.last().map(|t| t.typ);
14111411
let should_save_annotation_block = matches!(
14121412
parent_scope_typ,
1413-
Some(CompilerScope::Class)
1414-
| Some(CompilerScope::Module)
1415-
| Some(CompilerScope::Function)
1416-
| Some(CompilerScope::AsyncFunction)
1413+
Some(
1414+
CompilerScope::Class
1415+
| CompilerScope::Module
1416+
| CompilerScope::Function
1417+
| CompilerScope::AsyncFunction
1418+
)
14171419
);
14181420
let saved_annotation_block = if should_save_annotation_block {
14191421
self.tables.last_mut().unwrap().annotation_block.take()

crates/common/src/format.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl FormatSpec {
490490
pub fn is_decimal_int_format(&self) -> bool {
491491
matches!(
492492
self.format_type,
493-
None | Some(FormatType::Decimal) | Some(FormatType::Number(Case::Lower))
493+
None | Some(FormatType::Decimal | FormatType::Number(Case::Lower))
494494
)
495495
}
496496

@@ -707,18 +707,20 @@ impl FormatSpec {
707707
*case,
708708
self.alternate_form,
709709
)),
710-
Some(FormatType::Decimal)
711-
| Some(FormatType::Binary)
712-
| Some(FormatType::Octal)
713-
| Some(FormatType::Hex(_))
714-
| Some(FormatType::String)
715-
| Some(FormatType::Character)
716-
| Some(FormatType::Number(Case::Upper))
717-
| Some(FormatType::Unknown(_)) => {
710+
Some(
711+
FormatType::Decimal
712+
| FormatType::Binary
713+
| FormatType::Octal
714+
| FormatType::Hex(_)
715+
| FormatType::String
716+
| FormatType::Character
717+
| FormatType::Number(Case::Upper)
718+
| FormatType::Unknown(_),
719+
) => {
718720
let ch = char::from(self.format_type.as_ref().unwrap());
719721
Err(FormatSpecError::UnknownFormatCode(ch, "float"))
720722
}
721-
Some(FormatType::GeneralFormat(case)) | Some(FormatType::Number(case)) => {
723+
Some(FormatType::GeneralFormat(case) | FormatType::Number(case)) => {
722724
let precision = if precision == 0 { 1 } else { precision };
723725
Ok(float::format_general(
724726
precision,
@@ -835,10 +837,12 @@ impl FormatSpec {
835837
Some(_) | None => Err(FormatSpecError::CodeNotInRange),
836838
},
837839
},
838-
Some(FormatType::GeneralFormat(_))
839-
| Some(FormatType::FixedPoint(_))
840-
| Some(FormatType::Exponent(_))
841-
| Some(FormatType::Percentage) => match num.to_f64() {
840+
Some(
841+
FormatType::GeneralFormat(_)
842+
| FormatType::FixedPoint(_)
843+
| FormatType::Exponent(_)
844+
| FormatType::Percentage,
845+
) => match num.to_f64() {
842846
Some(float) => return self.format_float(float),
843847
_ => Err(FormatSpecError::UnableToConvert),
844848
},
@@ -895,7 +899,7 @@ impl FormatSpec {
895899
if let Some(FormatAlign::AfterSign) = &self.align {
896900
return Err(FormatSpecError::AlignmentFlag);
897901
}
898-
match &self.fill.unwrap_or(' '.into()).to_char() {
902+
match &self.fill.unwrap_or_else(|| ' '.into()).to_char() {
899903
Some('0') => Err(FormatSpecError::ZeroPadding),
900904
_ => self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right),
901905
}
@@ -934,15 +938,17 @@ impl FormatSpec {
934938
let precision = self.precision.unwrap_or(6);
935939
let magnitude = num.abs();
936940
let magnitude_str = match &self.format_type {
937-
Some(FormatType::Decimal)
938-
| Some(FormatType::Binary)
939-
| Some(FormatType::Octal)
940-
| Some(FormatType::Hex(_))
941-
| Some(FormatType::String)
942-
| Some(FormatType::Character)
943-
| Some(FormatType::Number(Case::Upper))
944-
| Some(FormatType::Percentage)
945-
| Some(FormatType::Unknown(_)) => {
941+
Some(
942+
FormatType::Decimal
943+
| FormatType::Binary
944+
| FormatType::Octal
945+
| FormatType::Hex(_)
946+
| FormatType::String
947+
| FormatType::Character
948+
| FormatType::Number(Case::Upper)
949+
| FormatType::Percentage
950+
| FormatType::Unknown(_),
951+
) => {
946952
let ch = char::from(self.format_type.as_ref().unwrap());
947953
Err(FormatSpecError::UnknownFormatCode(ch, "complex"))
948954
}
@@ -952,7 +958,7 @@ impl FormatSpec {
952958
*case,
953959
self.alternate_form,
954960
)),
955-
Some(FormatType::GeneralFormat(case)) | Some(FormatType::Number(case)) => {
961+
Some(FormatType::GeneralFormat(case) | FormatType::Number(case)) => {
956962
let precision = if precision == 0 { 1 } else { precision };
957963
Ok(float::format_general(
958964
precision,
@@ -1014,7 +1020,7 @@ impl FormatSpec {
10141020
let align = self.align.unwrap_or(default_align);
10151021

10161022
let num_chars = magnitude_str.char_len();
1017-
let fill_char = self.fill.unwrap_or(' '.into());
1023+
let fill_char = self.fill.unwrap_or_else(|| ' '.into());
10181024
let fill_chars_needed: i32 = self.width.map_or(0, |w| {
10191025
cmp::max(0, (w as i32) - (num_chars as i32) - (sign_str.len() as i32))
10201026
});

crates/common/src/str.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,7 @@ pub fn zfill(bytes: &[u8], width: usize) -> Vec<u8> {
415415
bytes.to_vec()
416416
} else {
417417
let (sign, s) = match bytes.first() {
418-
Some(_sign @ b'+') | Some(_sign @ b'-') => {
419-
(unsafe { bytes.get_unchecked(..1) }, &bytes[1..])
420-
}
418+
Some(_sign @ (b'+' | b'-')) => (unsafe { bytes.get_unchecked(..1) }, &bytes[1..]),
421419
_ => (&b""[..], bytes),
422420
};
423421
let mut filled = Vec::new();

crates/derive-impl/src/pyclass.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,11 +1951,9 @@ where
19511951
{
19521952
use AttrName::*;
19531953
Ok(match attr_name {
1954-
attr_name @ Method | attr_name @ ClassMethod | attr_name @ StaticMethod => {
1955-
Box::new(MethodItem {
1956-
inner: ContentItemInner { index, attr_name },
1957-
})
1958-
}
1954+
attr_name @ (Method | ClassMethod | StaticMethod) => Box::new(MethodItem {
1955+
inner: ContentItemInner { index, attr_name },
1956+
}),
19591957
GetSet => Box::new(GetSetItem {
19601958
inner: ContentItemInner { index, attr_name },
19611959
}),

0 commit comments

Comments
 (0)