From c7d7e70b79f69d91e13e81f88b83f591bef9b922 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Wed, 29 Jul 2026 16:01:27 -0400 Subject: [PATCH] Fix some clippy lints for 1.99 --- crates/common/src/format.rs | 3 +-- crates/compiler-core/src/bytecode.rs | 17 +++++++++++++---- crates/host_env/src/nt.rs | 4 +++- crates/stdlib/src/_testconsole.rs | 8 ++++---- crates/vm/src/builtins/interpolation.rs | 3 +-- crates/vm/src/dict_inner.rs | 2 +- crates/vm/src/stdlib/_sre.rs | 6 ++---- crates/vm/src/stdlib/sys/monitoring.rs | 10 +++++----- crates/vm/src/vm/mod.rs | 5 +---- crates/vm/src/vm/vm_object.rs | 3 +-- 10 files changed, 32 insertions(+), 29 deletions(-) diff --git a/crates/common/src/format.rs b/crates/common/src/format.rs index 4218dca7b74..5b0fb0a313b 100644 --- a/crates/common/src/format.rs +++ b/crates/common/src/format.rs @@ -1365,8 +1365,7 @@ impl FieldName { FieldType::Index(index) } else if first .as_str() - .ok() - .is_some_and(|s| s.bytes().all(|b| b.is_ascii_digit())) + .is_ok_and(|s| s.bytes().all(|b| b.is_ascii_digit())) { // All-digit segment whose value overflows usize itself. return Err(FormatParseError::TooManyDecimalDigits); diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index 3a7eec439e3..ba1639170a7 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -557,6 +557,14 @@ impl TryFrom<&[u8]> for CodeUnit { } } +impl TryFrom<[u8; 2]> for CodeUnit { + type Error = MarshalError; + + fn try_from(value: [u8; 2]) -> Result { + Ok(Self::new(value[0].try_into()?, value[1].into())) + } +} + pub struct CodeUnits { units: UnsafeCell>, adaptive_counters: Box<[AtomicU16]>, @@ -610,12 +618,13 @@ impl TryFrom<&[u8]> for CodeUnits { type Error = MarshalError; fn try_from(value: &[u8]) -> Result { - if !value.len().is_multiple_of(2) { + let (chunks, []) = value.as_chunks::<2>() else { return Err(Self::Error::InvalidBytecode); - } + }; - let units = value - .chunks_exact(2) + let units = chunks + .iter() + .copied() .map(CodeUnit::try_from) .collect::, _>>()?; Ok(units.into()) diff --git a/crates/host_env/src/nt.rs b/crates/host_env/src/nt.rs index 4c77b30e616..780a75910ea 100644 --- a/crates/host_env/src/nt.rs +++ b/crates/host_env/src/nt.rs @@ -1333,7 +1333,9 @@ pub fn readlink(path: &Path) -> Result { let path_slice = &buffer[path_start..path_end]; let mut wide_chars: Vec = path_slice - .chunks_exact(2) + .as_chunks::<2>() + .0 + .iter() .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]])) .collect(); diff --git a/crates/stdlib/src/_testconsole.rs b/crates/stdlib/src/_testconsole.rs index 78cba3b397d..3b7aad17178 100644 --- a/crates/stdlib/src/_testconsole.rs +++ b/crates/stdlib/src/_testconsole.rs @@ -17,11 +17,11 @@ mod _testconsole { let data = &*data; // Interpret as UTF-16-LE pairs - if !data.len().is_multiple_of(2) { + let (chunks, []) = data.as_chunks::<2>() else { return Err(vm.new_value_error("buffer must contain UTF-16-LE data (even length)")); - } - let wchars: Vec = data - .chunks_exact(2) + }; + let wchars: Vec = chunks + .iter() .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]])) .collect(); host_testconsole::write_console_input(fd, &wchars).map_err(|e| e.into_pyexception(vm)) diff --git a/crates/vm/src/builtins/interpolation.rs b/crates/vm/src/builtins/interpolation.rs index a865ff390de..0ae1b33120b 100644 --- a/crates/vm/src/builtins/interpolation.rs +++ b/crates/vm/src/builtins/interpolation.rs @@ -68,8 +68,7 @@ impl Constructor for PyInterpolation { .as_bytes() .iter() .exactly_one() - .ok() - .is_some_and(|s| matches!(*s, b's' | b'r' | b'a')); + .is_ok_and(|s| matches!(*s, b's' | b'r' | b'a')); if !has_flag { return Err(vm.new_value_error( "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'", diff --git a/crates/vm/src/dict_inner.rs b/crates/vm/src/dict_inner.rs index 3b5f5617f02..7c0e3443393 100644 --- a/crates/vm/src/dict_inner.rs +++ b/crates/vm/src/dict_inner.rs @@ -61,7 +61,7 @@ static KEYS_VERSION: AtomicU32 = AtomicU32::new(0); /// unrealistic in practice. fn next_keys_version() -> u32 { KEYS_VERSION - .fetch_update(Relaxed, Relaxed, |v| v.checked_add(1)) + .try_update(Relaxed, Relaxed, |v| v.checked_add(1)) .map_or(0, |v| v + 1) } diff --git a/crates/vm/src/stdlib/_sre.rs b/crates/vm/src/stdlib/_sre.rs index c90ff4d4f6f..03382549b47 100644 --- a/crates/vm/src/stdlib/_sre.rs +++ b/crates/vm/src/stdlib/_sre.rs @@ -146,11 +146,9 @@ mod _sre { let mut items = Vec::with_capacity(1); let v = template.borrow_vec(); let literal = v.first().ok_or_else(err)?.clone(); - let trunks = v[1..].chunks_exact(2); - - if !trunks.remainder().is_empty() { + let (trunks, []) = v[1..].as_chunks::<2>() else { return Err(err()); - } + }; for trunk in trunks { let index: usize = trunk[0] diff --git a/crates/vm/src/stdlib/sys/monitoring.rs b/crates/vm/src/stdlib/sys/monitoring.rs index 56a68cea619..f468a86b0a5 100644 --- a/crates/vm/src/stdlib/sys/monitoring.rs +++ b/crates/vm/src/stdlib/sys/monitoring.rs @@ -344,7 +344,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) { continue; } // Excluded: RESUME, END_FOR, CACHE (and their instrumented variants) - let base = op.to_base().map_or(op, |b| b); + let base = op.to_base().unwrap_or(op); if matches!( base, Instruction::Resume { .. } | Instruction::EndFor | Instruction::Cache @@ -387,7 +387,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) { .skip(first_traceable) { let op = unit.op; - let base = op.to_base().map_or(op, |b| b); + let base = op.to_base().unwrap_or(op); if matches!(base, Instruction::ExtendedArg) { continue; } @@ -425,7 +425,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) { let mut instr_idx = first_traceable; for unit in code.code.instructions[first_traceable..len].iter().copied() { let (op, arg) = arg_state.get(unit); - let base = op.to_base().map_or(op, |b| b); + let base = op.to_base().unwrap_or(op); if matches!(base, Instruction::ExtendedArg) || matches!(base, Instruction::Cache) { instr_idx += 1; @@ -460,7 +460,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) { && !no_loc_mask.get(target_idx).copied().unwrap_or(false) { let target_op = code.code.instructions[target_idx].op; - let target_base = target_op.to_base().map_or(target_op, |b| b); + let target_base = target_op.to_base().unwrap_or(target_op); // Skip synthetic cleanup targets. if matches!(target_base, Instruction::PopIter) { instr_idx += 1; @@ -483,7 +483,7 @@ pub(crate) fn instrument_code(code: &PyCode, events: u32) { && !no_loc_mask.get(target_idx).copied().unwrap_or(false) { let target_op = code.code.instructions[target_idx].op; - let target_base = target_op.to_base().map_or(target_op, |b| b); + let target_base = target_op.to_base().unwrap_or(target_op); if !matches!(target_base, Instruction::PopIter) && let Some((loc, _)) = line_locations.get(target_idx) && loc.line.get() > 0 diff --git a/crates/vm/src/vm/mod.rs b/crates/vm/src/vm/mod.rs index 3062ea07f98..801b1297b74 100644 --- a/crates/vm/src/vm/mod.rs +++ b/crates/vm/src/vm/mod.rs @@ -2092,10 +2092,7 @@ impl VirtualMachine { if exc.class().is(self.ctx.exceptions.attribute_error) { let exc = exc.as_object(); // Check if this exception was already augmented - let already_set = exc - .get_attr("name", self) - .ok() - .is_some_and(|v| !self.is_none(&v)); + let already_set = exc.get_attr("name", self).is_ok_and(|v| !self.is_none(&v)); if already_set { return; } diff --git a/crates/vm/src/vm/vm_object.rs b/crates/vm/src/vm/vm_object.rs index 8a7be140dc8..aa68f7f4dee 100644 --- a/crates/vm/src/vm/vm_object.rs +++ b/crates/vm/src/vm/vm_object.rs @@ -47,8 +47,7 @@ impl VirtualMachine { /// Returns true if the file object's `closed` attribute is truthy. fn file_is_closed(&self, file: &PyObject) -> bool { file.get_attr("closed", self) - .ok() - .is_some_and(|v| v.try_to_bool(self).unwrap_or(false)) + .is_ok_and(|v| v.try_to_bool(self).unwrap_or_default()) } pub(crate) fn flush_std(&self) -> i32 {