Add more pedantic clippy rules (#7830)

This commit is contained in:
Shahar Naveh authored and GitHub committed 2026-05-12 20:57:37 +09:00
1 parent e588675fdc
commit 7b2379dbef
25 files changed
+50 -43

No files matched your search

+7
View File
@@ -343,6 +343,13 @@ unnested_or_patterns = "warn"
# pedantic lints to enforce gradually
cloned_instead_of_copied = "warn"
collapsible_else_if = "warn"
comparison_chain = "warn"
explicit_into_iter_loop = "warn"
explicit_iter_loop = "warn"
filter_map_next = "warn"
flat_map_option = "warn"
inconsistent_struct_constructor = "warn"
manual_is_variant_and = "warn"
map_unwrap_or = "warn"
must_use_candidate = "warn"
+3 -3
View File
@@ -823,7 +823,7 @@ impl Compiler {
}
}
for elt in elts.iter() {
for elt in elts {
if let ast::Expr::Starred(ast::ExprStarred { value, .. }) = elt {
// When we hit first star, build sequence with elements so far
if !sequence_built {
@@ -11509,7 +11509,7 @@ impl Compiler {
let mut current_string = Wtf8Buf::new();
let mut interp_count: u32 = 0;
for tstring in tstring_value.iter() {
for tstring in tstring_value {
self.collect_tstring_strings(
tstring,
&mut all_strings,
@@ -11534,7 +11534,7 @@ impl Compiler {
}
);
for tstring in tstring_value.iter() {
for tstring in tstring_value {
self.compile_tstring_interpolations(tstring)?;
}
+3 -3
View File
@@ -529,7 +529,7 @@ impl CodeInfo {
// Final DCE: truncate instructions after terminal ops in linearized blocks.
// This catches dead code created by normalize_jumps after the initial DCE.
for block in blocks.iter_mut() {
for block in &mut blocks {
if let Some(pos) = block
.instructions
.iter()
@@ -540,7 +540,7 @@ impl CodeInfo {
}
// Pre-compute cache_entries for real (non-pseudo) instructions
for block in blocks.iter_mut() {
for block in &mut blocks {
for instr in &mut block.instructions {
if let AnyInstruction::Real(op) = instr.instr {
instr.cache_entries = op.cache_entries() as u32;
@@ -9430,7 +9430,7 @@ impl CodeInfo {
// Fix up handler stack_depth in ExceptHandlerInfo using start_depths
// computed above: depth = start_depth - 1 - preserve_lasti
for block in self.blocks.iter_mut() {
for block in &mut self.blocks {
for ins in &mut block.instructions {
if let Some(ref mut handler) = ins.except_handler {
let h_start = start_depths[handler.handler_block.idx()];
+1 -1
View File
@@ -652,7 +652,7 @@ impl CodeUnits {
/// Disable adaptive specialization by setting all counters to unreachable.
/// Used for CPython-compiled bytecode where specialization may not be safe.
pub fn disable_specialization(&self) {
for counter in self.adaptive_counters.iter() {
for counter in &self.adaptive_counters {
counter.store(UNREACHABLE_BACKOFF, Ordering::Relaxed);
}
}
+2 -2
View File
@@ -928,13 +928,13 @@ pub fn serialize_code<W: Write, C: Constant>(buf: &mut W, code: &CodeObject<C>)
let total_lp_count = code.varnames.len() + cell_only_names.len() + code.freevars.len();
buf.write_u8(Type::Tuple as u8);
write_len(buf, total_lp_count);
for n in code.varnames.iter() {
for n in &code.varnames {
write_marshal_str(buf, n.as_ref());
}
for &n in &cell_only_names {
write_marshal_str(buf, n);
}
for n in code.freevars.iter() {
for n in &code.freevars {
write_marshal_str(buf, n.as_ref());
}
// 10: co_localspluskinds — use the stored kinds directly
+4 -4
View File
@@ -609,7 +609,7 @@ mod decl {
// Disable SIGSEGV handler for access violations to avoid double output
if code == 0xC0000005 {
unsafe {
for handler in FAULTHANDLER_HANDLERS.iter_mut() {
for handler in &mut FAULTHANDLER_HANDLERS {
if handler.signum == libc::SIGSEGV {
faulthandler_disable_fatal_handler(handler);
break;
@@ -632,7 +632,7 @@ mod decl {
}
unsafe {
for handler in FAULTHANDLER_HANDLERS.iter_mut() {
for handler in &mut FAULTHANDLER_HANDLERS {
if handler.enabled {
continue;
}
@@ -661,7 +661,7 @@ mod decl {
}
unsafe {
for handler in FAULTHANDLER_HANDLERS.iter_mut() {
for handler in &mut FAULTHANDLER_HANDLERS {
if handler.enabled {
continue;
}
@@ -700,7 +700,7 @@ mod decl {
}
unsafe {
for handler in FAULTHANDLER_HANDLERS.iter_mut() {
for handler in &mut FAULTHANDLER_HANDLERS {
faulthandler_disable_fatal_handler(handler);
}
}
+2 -2
View File
@@ -473,7 +473,7 @@ pub(super) fn cert_der_to_dict_helper(
if let Some(ext) = ext_map.get(&OID_X509_EXT_CRL_DISTRIBUTION_POINTS)
&& let ParsedExtension::CRLDistributionPoints(cdp) = &ext.parsed_extension()
{
for dp in cdp.points.iter() {
for dp in &cdp.points {
if let Some(dist_point) = &dp.distribution_point {
use x509_parser::extensions::DistributionPointName;
if let DistributionPointName::FullName(names) = dist_point {
@@ -583,7 +583,7 @@ pub(super) fn build_verified_chain(
let issuer_name = last_cert.issuer();
let mut found_issuer = false;
for ca_der in ca_certs_der.iter() {
for ca_der in ca_certs_der {
let (_, ca_cert) = match X509Certificate::from_der(ca_der) {
Ok(parsed) => parsed,
Err(_) => continue,
+1 -1
View File
@@ -99,7 +99,7 @@ impl Initializer for PyNamespace {
};
// Validate keys are strings and set attributes
for (key, value) in dict.into_iter() {
for (key, value) in dict {
let key_str = key
.downcast_ref::<crate::builtins::PyStr>()
.ok_or_else(|| {
+1 -1
View File
@@ -55,7 +55,7 @@ impl Constructor for PyTemplate {
let mut interpolations: Vec<PyObjectRef> = Vec::new();
let mut last_was_str = false;
for item in args.args.iter() {
for item in &args.args {
if let Ok(s) = item.clone().downcast::<PyStr>() {
if last_was_str {
// Concatenate adjacent strings
+2 -2
View File
@@ -824,7 +824,7 @@ impl PyType {
pub(crate) fn init_slots(&self, ctx: &Context) {
// Inherit slots from MRO (mro[0] is self, so skip it)
let mro: Vec<_> = self.mro.read()[1..].to_vec();
for base in mro.iter() {
for base in &mro {
self.inherit_slots(base);
}
@@ -833,7 +833,7 @@ impl PyType {
let mut slot_name_set = std::collections::HashSet::new();
// mro[0] is self, so skip it; self.attributes is checked separately below
for cls in self.mro.read()[1..].iter() {
for cls in &self.mro.read()[1..] {
for &name in cls.attributes.read().keys() {
if name.as_bytes().starts_with(b"__") && name.as_bytes().ends_with(b"__") {
slot_name_set.insert(name);
+1 -1
View File
@@ -16,7 +16,7 @@ use rustpython_common::static_cell;
/// 1. Has a function set in the type's slots
/// 2. Doesn't already have an attribute in the type's dict
pub fn add_operators(class: &'static Py<PyType>, ctx: &Context) {
for def in SLOT_DEFS.iter() {
for def in SLOT_DEFS {
// Skip __new__ - it has special handling
if def.name == "__new__" {
continue;
+1 -1
View File
@@ -405,7 +405,7 @@ pub(super) mod types {
// If it's a tuple of types
if let Some(tuple) = condition.downcast_ref::<PyTuple>() {
let mut types = Vec::new();
for item in tuple.iter() {
for item in tuple {
let typ: PyTypeRef = item.clone().try_into_value(vm).map_err(|_| {
vm.new_type_error(
"expected a function, exception type or tuple of exception types",
+1 -1
View File
@@ -978,7 +978,7 @@ impl Frame {
// Non-merged cell: find the name by skipping merged cellvars
let mut found_name = None;
let mut skip = nonmerged_cell_idx;
for cv in code.cellvars.iter() {
for cv in &code.cellvars {
let is_merged = code.varnames.contains(cv);
if !is_merged {
if skip == 0 {
+2 -2
View File
@@ -699,7 +699,7 @@ impl GcState {
if debug.contains(GcDebugFlags::SAVEALL) {
let mut garbage_guard = self.garbage.lock();
for obj_ref in truly_dead.iter() {
for obj_ref in &truly_dead {
garbage_guard.push(obj_ref.clone());
}
}
@@ -708,7 +708,7 @@ impl GcState {
// Break cycles by clearing references (tp_clear)
// Use deferred drop context to prevent stack overflow.
rustpython_common::refcount::with_deferred_drops(|| {
for obj_ref in truly_dead.iter() {
for obj_ref in &truly_dead {
if obj_ref.gc_has_clear() {
let edges = unsafe { obj_ref.gc_clear() };
drop(edges);
+1 -1
View File
@@ -1828,7 +1828,7 @@ impl PyObject {
if let Some(dict_ref) = ext.dict.as_ref().and_then(|d| d.replace(None)) {
result.push(dict_ref.into());
}
for slot in ext.slots.iter() {
for slot in &ext.slots {
if let Some(val) = slot.write().take() {
result.push(val);
}
+1 -1
View File
@@ -882,7 +882,7 @@ pub(crate) mod _ctypes {
&& let Ok(fields) = fields_attr.try_to_value::<Vec<PyObjectRef>>(vm)
{
let mut max_align = 1usize;
for field in fields.iter() {
for field in &fields {
if let Some(tuple) = field.downcast_ref::<crate::builtins::PyTuple>()
&& let Some(field_type) = tuple.get(1)
{
+2 -2
View File
@@ -2540,7 +2540,7 @@ fn make_fields(
return Err(vm.new_type_error("_fields_ must be a sequence"));
};
for pair in fieldlist.iter() {
for pair in &fieldlist {
let field_tuple = pair
.downcast_ref::<PyTuple>()
.ok_or_else(|| vm.new_type_error("_fields_ must contain tuples"))?;
@@ -2599,7 +2599,7 @@ pub(super) fn make_anon_fields(cls: &Py<PyType>, vm: &VirtualMachine) -> PyResul
return Err(vm.new_type_error("_anonymous_ must be a sequence"));
};
for fname_obj in anon_names.iter() {
for fname_obj in &anon_names {
let fname = fname_obj
.downcast_ref::<PyStr>()
.ok_or_else(|| vm.new_type_error("_anonymous_ items must be strings"))?;
+3 -3
View File
@@ -16,7 +16,7 @@ pub(super) fn calculate_struct_size(cls: &Py<PyType>, vm: &VirtualMachine) -> Py
let fields: Vec<PyObjectRef> = fields_attr.try_to_value(vm)?;
let mut total_size = 0usize;
for field in fields.iter() {
for field in &fields {
if let Some(tuple) = field.downcast_ref::<PyTuple>()
&& let Some(field_type) = tuple.get(1)
{
@@ -738,7 +738,7 @@ impl PyCStructure {
if let Some(fields_attr) = type_obj.get_direct_attr(vm.ctx.intern_str("_fields_")) {
let fields: Vec<PyObjectRef> = fields_attr.try_to_value(vm)?;
for field in fields.iter() {
for field in &fields {
if current_index >= args.len() {
break;
}
@@ -786,7 +786,7 @@ impl Initializer for PyCStructure {
}
// 2. Process keyword arguments
for (key, value) in args.kwargs.iter() {
for (key, value) in &args.kwargs {
zelf.as_object()
.set_attr(vm.ctx.intern_str(key.as_str()), value.clone(), vm)?;
}
+3 -3
View File
@@ -16,7 +16,7 @@ pub(super) fn calculate_union_size(cls: &Py<PyType>, vm: &VirtualMachine) -> PyR
let fields: Vec<PyObjectRef> = fields_attr.try_to_value(vm)?;
let mut max_size = 0usize;
for field in fields.iter() {
for field in &fields {
if let Some(tuple) = field.downcast_ref::<PyTuple>()
&& let Some(field_type) = tuple.get(1)
{
@@ -609,7 +609,7 @@ impl PyCUnion {
if let Some(fields_attr) = type_obj.get_direct_attr(vm.ctx.intern_str("_fields_")) {
let fields: Vec<PyObjectRef> = fields_attr.try_to_value(vm)?;
for field in fields.iter() {
for field in &fields {
if current_index >= args.len() {
break;
}
@@ -656,7 +656,7 @@ impl Initializer for PyCUnion {
}
// 2. Process keyword arguments
for (key, value) in args.kwargs.iter() {
for (key, value) in &args.kwargs {
zelf.as_object()
.set_attr(vm.ctx.intern_str(key.as_str()), value.clone(), vm)?;
}
+2 -2
View File
@@ -4532,7 +4532,7 @@ mod _io {
let dict_ref: PyRef<PyDict> = dict.clone().try_into_value(vm)?;
if let Some(obj_dict) = zelf.as_object().dict() {
obj_dict.clear();
for (key, value) in dict_ref.into_iter() {
for (key, value) in dict_ref {
obj_dict.set_item(&*key, value, vm)?;
}
}
@@ -4774,7 +4774,7 @@ mod _io {
let dict_ref: PyRef<PyDict> = dict.clone().try_into_value(vm)?;
if let Some(obj_dict) = zelf.as_object().dict() {
obj_dict.clear();
for (key, value) in dict_ref.into_iter() {
for (key, value) in dict_ref {
obj_dict.set_item(&*key, value, vm)?;
}
}
+1 -1
View File
@@ -164,7 +164,7 @@ pub(crate) mod decl {
fn typing_type_repr_value(value: &PyObjectRef, vm: &VirtualMachine) -> PyResult {
if let Ok(tuple) = value.try_to_ref::<PyTuple>(vm) {
let mut parts = Vec::with_capacity(tuple.len());
for item in tuple.iter() {
for item in tuple {
parts.push(typing_type_repr(item, vm)?);
}
let inner = if parts.len() == 1 {
+1 -1
View File
@@ -69,7 +69,7 @@ mod atexit {
pub fn _run_exitfuncs(vm: &VirtualMachine) {
let funcs: Vec<_> = core::mem::take(&mut *vm.state.atexit_funcs.lock());
// Callbacks stored in LIFO order, iterate forward
for entry in funcs.into_iter() {
for entry in funcs {
let (func, args) = *entry;
if let Err(e) = func.call(args, vm) {
let exit = e.fast_isinstance(vm.ctx.exceptions.system_exit);
+1 -1
View File
@@ -133,7 +133,7 @@ mod gc {
let stats = gc_state::gc_state().get_stats();
let mut result = Vec::with_capacity(3);
for stat in stats.iter() {
for stat in &stats {
let dict = vm.ctx.new_dict();
dict.set_item("collections", vm.ctx.new_int(stat.collections).into(), vm)?;
dict.set_item("collected", vm.ctx.new_int(stat.collected).into(), vm)?;
+2 -2
View File
@@ -293,7 +293,7 @@ mod decl {
}
} else if let Some(d) = obj.downcast_ref::<PyDict>() {
buf.write_u8(b'{');
for (k, v) in d.into_iter() {
for (k, v) in d {
write_object_depth(buf, &k, refs, version, vm, depth - 1)?;
write_object_depth(buf, &v, refs, version, vm, depth - 1)?;
}
@@ -572,7 +572,7 @@ mod decl {
check_no_code(&elem, vm)?;
}
} else if let Some(dict) = obj.downcast_ref::<PyDict>() {
for (k, v) in dict.into_iter() {
for (k, v) in dict {
check_no_code(&k, vm)?;
check_no_code(&v, vm)?;
}
+2 -2
View File
@@ -1074,7 +1074,7 @@ pub(crate) mod module {
// Build environment strings as "KEY=VALUE\0" wide strings
let mut env_strings: Vec<widestring::WideCString> = Vec::new();
for (key, value) in env.into_iter() {
for (key, value) in env {
let key = FsPath::try_from_path_like(key, true, vm)?;
let value = FsPath::try_from_path_like(value, true, vm)?;
let key_str = key.to_string_lossy();
@@ -1192,7 +1192,7 @@ pub(crate) mod module {
let env = crate::stdlib::os::envobj_to_dict(env, vm)?;
// Build environment strings as "KEY=VALUE\0" wide strings
let mut env_strings: Vec<widestring::WideCString> = Vec::new();
for (key, value) in env.into_iter() {
for (key, value) in env {
let key = PyStrRef::try_from_object(vm, key)?;
let value = PyStrRef::try_from_object(vm, value)?;
let key_str = key.expect_str();