Visitar URL original
Add some clippy lints by ShaharNaveh · Pull Request #7755 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ elided_lifetimes_in_paths = "warn"
alloc_instead_of_core = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
format_collect = "warn"
from_iter_instead_of_collect = "warn"
inefficient_to_string = "warn"
redundant_clone = "warn"
debug_assert_with_mut_call = "warn"
unused_peekable = "warn"
manual_is_variant_and = "warn"
or_fun_call = "warn"
unnested_or_patterns = "warn"

perf = "warn"
style = "warn"
complexity = "warn"
Expand Down
15 changes: 8 additions & 7 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ impl Compiler {
// Create the new compilation unit
let code_info = ir::CodeInfo {
flags,
source_path: source_path.clone(),
source_path,
private,
blocks: vec![ir::Block::default()],
current_block: BlockIdx::new(0),
Expand Down Expand Up @@ -6718,7 +6718,7 @@ impl Compiler {
}

// Restore the original pattern context.
*pc = old_pc.clone();
*pc = old_pc;
// Simulate Py_INCREF on pc.stores.
pc.stores = pc.stores.clone();
// In C, old_pc.fail_pop is set to NULL to avoid freeing it later.
Expand Down Expand Up @@ -14487,8 +14487,7 @@ def outer():
assert!(
matches!(
ops.get(cleanup_idx + 1),
Some(Instruction::JumpBackwardNoInterrupt { .. })
| Some(Instruction::JumpForward { .. })
Some(Instruction::JumpBackwardNoInterrupt { .. } | Instruction::JumpForward { .. })
),
"expected CLEANUP_THROW to jump to shared END_SEND block, got ops={ops:?}"
);
Expand Down Expand Up @@ -14533,7 +14532,7 @@ def f():
assert!(
matches!(
ops.get(first_pop_except + 1),
Some(Instruction::LoadSmallInt { .. }) | Some(Instruction::LoadConst { .. })
Some(Instruction::LoadSmallInt { .. } | Instruction::LoadConst { .. })
),
"expected line-after-except code immediately after POP_EXCEPT, got ops={ops:?}"
);
Expand Down Expand Up @@ -16145,8 +16144,10 @@ _pathseps_with_colon = {f':{s}' for s in path_separators}
assert!(
!ops.windows(2).any(|window| matches!(
window,
[Instruction::LoadFast { .. }, Instruction::GetIter]
| [Instruction::LoadFastCheck { .. }, Instruction::GetIter]
[
Instruction::LoadFast { .. } | Instruction::LoadFastCheck { .. },
Instruction::GetIter
]
)),
"module local outer iterable should not become a fast local, got ops={ops:?}"
);
Expand Down
68 changes: 40 additions & 28 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2036,8 +2036,9 @@ impl CodeInfo {
let idx = i as usize;
let swap_arg = match instructions[idx].instr.real() {
Some(Instruction::Swap { .. }) => u32::from(instructions[idx].arg),
Some(Instruction::Nop)
| Some(Instruction::PopTop | Instruction::StoreFast { .. }) => {
Some(
Instruction::Nop | Instruction::PopTop | Instruction::StoreFast { .. },
) => {
i -= 1;
continue;
}
Expand Down Expand Up @@ -2332,8 +2333,10 @@ impl CodeInfo {
match (curr_instr, next_instr) {
// Note: StoreFast + LoadFast → StoreFastLoadFast is done in a
// later pass aligned with CPython insert_superinstructions().
(Instruction::LoadConst { .. }, Instruction::ToBool)
| (Instruction::LoadSmallInt { .. }, Instruction::ToBool) => {
(
Instruction::LoadConst { .. } | Instruction::LoadSmallInt { .. },
Instruction::ToBool,
) => {
if let Some(value) =
const_truthiness(curr_instr, curr.arg, &self.metadata)
{
Expand All @@ -2355,10 +2358,10 @@ impl CodeInfo {
curr_instr,
OpArg::new(u32::from(curr.arg) | oparg::COMPARE_OP_BOOL_MASK),
)),
(Instruction::ContainsOp { .. }, Instruction::ToBool)
| (Instruction::IsOp { .. }, Instruction::ToBool) => {
Some((curr_instr, curr.arg))
}
(
Instruction::ContainsOp { .. } | Instruction::IsOp { .. },
Instruction::ToBool,
) => Some((curr_instr, curr.arg)),
(Instruction::LoadConst { consti }, Instruction::UnaryNot) => {
let constant = &self.metadata.consts[consti.get(curr.arg).as_usize()];
match constant {
Expand Down Expand Up @@ -2434,8 +2437,10 @@ impl CodeInfo {

let redundant = matches!(
(curr_instr, next_instr),
(Instruction::LoadConst { .. }, Instruction::PopTop)
| (Instruction::LoadSmallInt { .. }, Instruction::PopTop)
(
Instruction::LoadConst { .. } | Instruction::LoadSmallInt { .. },
Instruction::PopTop
)
) || matches!(curr_instr, Instruction::Copy { i } if i.get(curr.arg) == 1)
&& matches!(next_instr, Instruction::PopTop);

Expand Down Expand Up @@ -3757,7 +3762,7 @@ impl CodeInfo {
Some(Instruction::PushExcInfo) => {
in_exception_state = true;
}
Some(Instruction::PopExcept) | Some(Instruction::Reraise { .. }) => {
Some(Instruction::PopExcept | Instruction::Reraise { .. }) => {
in_exception_state = false;
}
Some(Instruction::LoadFastBorrow { .. }) if in_exception_state => {
Expand Down Expand Up @@ -4458,10 +4463,10 @@ impl CodeInfo {
Some(
Instruction::StoreFast { .. }
| Instruction::StoreFastLoadFast { .. }
| Instruction::StoreFastStoreFast { .. },
)
| Some(Instruction::DeleteFast { .. })
| Some(Instruction::LoadFastAndClear { .. }) => return false,
| Instruction::StoreFastStoreFast { .. }
| Instruction::DeleteFast { .. }
| Instruction::LoadFastAndClear { .. },
) => return false,
_ => {}
}
}
Expand Down Expand Up @@ -4613,8 +4618,10 @@ impl CodeInfo {
{
if matches!(
extra_info.instr.real(),
Some(Instruction::LoadFastBorrow { .. })
| Some(Instruction::LoadFastBorrowLoadFastBorrow { .. })
Some(
Instruction::LoadFastBorrow { .. }
| Instruction::LoadFastBorrowLoadFastBorrow { .. }
)
) {
to_deopt.push(*extra_instr_idx);
}
Expand Down Expand Up @@ -4642,8 +4649,10 @@ impl CodeInfo {
{
if matches!(
tail_info.instr.real(),
Some(Instruction::LoadFastBorrow { .. })
| Some(Instruction::LoadFastBorrowLoadFastBorrow { .. })
Some(
Instruction::LoadFastBorrow { .. }
| Instruction::LoadFastBorrowLoadFastBorrow { .. }
)
) {
cross_block_deopts.push((tail_block_idx, tail_instr_idx));
}
Expand Down Expand Up @@ -4722,7 +4731,7 @@ impl CodeInfo {
&& predecessor_blocks.iter().copied().all(|pred_idx| {
matches!(
last_real_instr(&self.blocks[pred_idx]),
Some(Instruction::PopIter) | Some(Instruction::Swap { .. })
Some(Instruction::PopIter | Instruction::Swap { .. })
)
})
})
Expand All @@ -4743,7 +4752,7 @@ impl CodeInfo {
&& (new_instructions.iter().all(|prev: &InstructionInfo| {
matches!(
prev.instr.real(),
Some(Instruction::Swap { .. }) | Some(Instruction::PopTop)
Some(Instruction::Swap { .. } | Instruction::PopTop)
)
}) || is_cleanup_restore_prefix(&new_instructions))
{
Expand Down Expand Up @@ -4906,8 +4915,9 @@ impl CodeInfo {
}
new_instructions.push(info);
}
Some(Instruction::LoadFast { var_num })
| Some(Instruction::LoadFastBorrow { var_num }) => {
Some(
Instruction::LoadFast { var_num } | Instruction::LoadFastBorrow { var_num },
) => {
let var_idx = usize::from(var_num.get(info.arg));
if var_idx < nlocals && unsafe_mask[var_idx] {
info.instr = Opcode::LoadFastCheck.into();
Expand All @@ -4918,8 +4928,10 @@ impl CodeInfo {
}
new_instructions.push(info);
}
Some(Instruction::LoadFastLoadFast { var_nums })
| Some(Instruction::LoadFastBorrowLoadFastBorrow { var_nums }) => {
Some(
Instruction::LoadFastLoadFast { var_nums }
| Instruction::LoadFastBorrowLoadFastBorrow { var_nums },
) => {
let packed = var_nums.get(info.arg);
let (idx1, idx2) = packed.indexes();
let idx1 = usize::from(idx1);
Expand Down Expand Up @@ -6802,7 +6814,7 @@ fn deoptimize_borrow_after_push_exc_info_in_blocks(blocks: &mut [Block]) {
Some(Instruction::PushExcInfo) => {
in_exception_state = true;
}
Some(Instruction::PopExcept) | Some(Instruction::Reraise { .. }) => {
Some(Instruction::PopExcept | Instruction::Reraise { .. }) => {
in_exception_state = false;
}
Some(Instruction::LoadFastBorrow { .. }) if in_exception_state => {
Expand Down Expand Up @@ -6896,7 +6908,7 @@ fn is_exit_without_lineno(block: &Block) -> bool {
&& prefix.iter().all(|info| {
matches!(
info.instr.real(),
Some(Instruction::PopExcept) | Some(Instruction::Nop)
Some(Instruction::PopExcept | Instruction::Nop)
)
})
&& prefix
Expand Down Expand Up @@ -6924,7 +6936,7 @@ fn shared_jump_back_target(block: &Block) -> Option<BlockIdx> {
if !prefix.iter().all(|info| {
matches!(
info.instr.real(),
Some(Instruction::PopExcept) | Some(Instruction::Nop)
Some(Instruction::PopExcept | Instruction::Nop)
)
}) {
return None;
Expand Down
10 changes: 6 additions & 4 deletions crates/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,10 +1410,12 @@ impl SymbolTableBuilder {
let parent_scope_typ = self.tables.last().map(|t| t.typ);
let should_save_annotation_block = matches!(
parent_scope_typ,
Some(CompilerScope::Class)
| Some(CompilerScope::Module)
| Some(CompilerScope::Function)
| Some(CompilerScope::AsyncFunction)
Some(
CompilerScope::Class
| CompilerScope::Module
| CompilerScope::Function
| CompilerScope::AsyncFunction
)
);
let saved_annotation_block = if should_save_annotation_block {
self.tables.last_mut().unwrap().annotation_block.take()
Expand Down
58 changes: 32 additions & 26 deletions crates/common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ impl FormatSpec {
pub fn is_decimal_int_format(&self) -> bool {
matches!(
self.format_type,
None | Some(FormatType::Decimal) | Some(FormatType::Number(Case::Lower))
None | Some(FormatType::Decimal | FormatType::Number(Case::Lower))
)
}

Expand Down Expand Up @@ -707,18 +707,20 @@ impl FormatSpec {
*case,
self.alternate_form,
)),
Some(FormatType::Decimal)
| Some(FormatType::Binary)
| Some(FormatType::Octal)
| Some(FormatType::Hex(_))
| Some(FormatType::String)
| Some(FormatType::Character)
| Some(FormatType::Number(Case::Upper))
| Some(FormatType::Unknown(_)) => {
Some(
FormatType::Decimal
| FormatType::Binary
| FormatType::Octal
| FormatType::Hex(_)
| FormatType::String
| FormatType::Character
| FormatType::Number(Case::Upper)
| FormatType::Unknown(_),
) => {
let ch = char::from(self.format_type.as_ref().unwrap());
Err(FormatSpecError::UnknownFormatCode(ch, "float"))
}
Some(FormatType::GeneralFormat(case)) | Some(FormatType::Number(case)) => {
Some(FormatType::GeneralFormat(case) | FormatType::Number(case)) => {
let precision = if precision == 0 { 1 } else { precision };
Ok(float::format_general(
precision,
Expand Down Expand Up @@ -835,10 +837,12 @@ impl FormatSpec {
Some(_) | None => Err(FormatSpecError::CodeNotInRange),
},
},
Some(FormatType::GeneralFormat(_))
| Some(FormatType::FixedPoint(_))
| Some(FormatType::Exponent(_))
| Some(FormatType::Percentage) => match num.to_f64() {
Some(
FormatType::GeneralFormat(_)
| FormatType::FixedPoint(_)
| FormatType::Exponent(_)
| FormatType::Percentage,
) => match num.to_f64() {
Some(float) => return self.format_float(float),
_ => Err(FormatSpecError::UnableToConvert),
},
Expand Down Expand Up @@ -895,7 +899,7 @@ impl FormatSpec {
if let Some(FormatAlign::AfterSign) = &self.align {
return Err(FormatSpecError::AlignmentFlag);
}
match &self.fill.unwrap_or(' '.into()).to_char() {
match &self.fill.unwrap_or_else(|| ' '.into()).to_char() {
Some('0') => Err(FormatSpecError::ZeroPadding),
_ => self.format_sign_and_align(&AsciiStr::new(&magnitude_str), "", FormatAlign::Right),
}
Expand Down Expand Up @@ -934,15 +938,17 @@ impl FormatSpec {
let precision = self.precision.unwrap_or(6);
let magnitude = num.abs();
let magnitude_str = match &self.format_type {
Some(FormatType::Decimal)
| Some(FormatType::Binary)
| Some(FormatType::Octal)
| Some(FormatType::Hex(_))
| Some(FormatType::String)
| Some(FormatType::Character)
| Some(FormatType::Number(Case::Upper))
| Some(FormatType::Percentage)
| Some(FormatType::Unknown(_)) => {
Some(
FormatType::Decimal
| FormatType::Binary
| FormatType::Octal
| FormatType::Hex(_)
| FormatType::String
| FormatType::Character
| FormatType::Number(Case::Upper)
| FormatType::Percentage
| FormatType::Unknown(_),
) => {
let ch = char::from(self.format_type.as_ref().unwrap());
Err(FormatSpecError::UnknownFormatCode(ch, "complex"))
}
Expand All @@ -952,7 +958,7 @@ impl FormatSpec {
*case,
self.alternate_form,
)),
Some(FormatType::GeneralFormat(case)) | Some(FormatType::Number(case)) => {
Some(FormatType::GeneralFormat(case) | FormatType::Number(case)) => {
let precision = if precision == 0 { 1 } else { precision };
Ok(float::format_general(
precision,
Expand Down Expand Up @@ -1014,7 +1020,7 @@ impl FormatSpec {
let align = self.align.unwrap_or(default_align);

let num_chars = magnitude_str.char_len();
let fill_char = self.fill.unwrap_or(' '.into());
let fill_char = self.fill.unwrap_or_else(|| ' '.into());
let fill_chars_needed: i32 = self.width.map_or(0, |w| {
cmp::max(0, (w as i32) - (num_chars as i32) - (sign_str.len() as i32))
});
Expand Down
4 changes: 1 addition & 3 deletions crates/common/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@ pub fn zfill(bytes: &[u8], width: usize) -> Vec<u8> {
bytes.to_vec()
} else {
let (sign, s) = match bytes.first() {
Some(_sign @ b'+') | Some(_sign @ b'-') => {
(unsafe { bytes.get_unchecked(..1) }, &bytes[1..])
}
Some(_sign @ (b'+' | b'-')) => (unsafe { bytes.get_unchecked(..1) }, &bytes[1..]),
_ => (&b""[..], bytes),
};
let mut filled = Vec::new();
Expand Down
8 changes: 3 additions & 5 deletions crates/derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1951,11 +1951,9 @@ where
{
use AttrName::*;
Ok(match attr_name {
attr_name @ Method | attr_name @ ClassMethod | attr_name @ StaticMethod => {
Box::new(MethodItem {
inner: ContentItemInner { index, attr_name },
})
}
attr_name @ (Method | ClassMethod | StaticMethod) => Box::new(MethodItem {
inner: ContentItemInner { index, attr_name },
}),
GetSet => Box::new(GetSetItem {
inner: ContentItemInner { index, attr_name },
}),
Expand Down
Loading
Loading