Skip to content

Commit 12b8305

Browse files
authored
Add more clippy rules (RustPython#8183)
* clippy `assigning_clones` * string_lit_as_bytes * tuple_array_conversions * while_float * manual_assert * Revert "string_lit_as_bytes" This reverts commit f40f756.
1 parent eae567f commit 12b8305

12 files changed

Lines changed: 43 additions & 35 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,17 @@ search_is_some = "warn"
371371
significant_drop_in_scrutinee = "warn"
372372
single_option_map = "warn"
373373
trait_duplication_in_bounds = "warn"
374+
tuple_array_conversions = "warn"
374375
type_repetition_in_bounds = "warn"
375376
unnecessary_struct_initialization = "warn"
376377
unused_peekable = "warn"
377378
unused_rounding = "warn"
378379
use_self = "warn"
379380
useless_let_if_seq = "warn"
381+
while_float = "warn"
380382

381383
# pedantic lints to enforce gradually
384+
assigning_clones = "warn"
382385
bool_to_int_with_if = "warn"
383386
checked_conversions = "warn"
384387
cloned_instead_of_copied = "warn"
@@ -404,6 +407,7 @@ iter_filter_is_ok = "warn"
404407
iter_filter_is_some = "warn"
405408
large_futures = "warn"
406409
large_types_passed_by_value = "warn"
410+
manual_assert = "warn"
407411
manual_instant_elapsed = "warn"
408412
manual_is_variant_and = "warn"
409413
map_unwrap_or = "warn"

crates/codegen/src/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,7 @@ impl<'warnings> Compiler<'warnings> {
21572157

21582158
fn expose_annotation_format_parameter(code: &mut CodeObject) {
21592159
if let Some(first) = code.varnames.first_mut() {
2160-
*first = "format".to_owned();
2160+
*first = String::from("format");
21612161
}
21622162
}
21632163

crates/codegen/src/symboltable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,9 @@ impl SymbolTableBuilder {
16321632
}
16331633

16341634
if type_params.is_none() {
1635-
self.class_name = prev_class.clone();
1635+
self.class_name.clone_from(&prev_class);
16361636
}
1637+
16371638
if let Some(arguments) = arguments {
16381639
self.scan_expressions(&arguments.args, ExpressionContext::Load)?;
16391640
for keyword in &arguments.keywords {

crates/common/src/float_ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use num_traits::{Signed, ToPrimitive};
44

55
#[must_use]
66
pub const fn decompose_float(value: f64) -> (f64, i32) {
7-
if 0.0 == value {
8-
(0.0, 0i32)
7+
if value == 0.0 {
8+
(0.0, 0)
99
} else {
1010
let bits = value.to_bits();
1111
let exponent: i32 = ((bits >> 52) & 0x7ff) as i32 - 1022;

crates/common/src/hash.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ impl HashSecret {
4949
let k1 = u64::from_le_bytes(right.try_into().unwrap());
5050
Self { k0, k1 }
5151
}
52-
}
5352

54-
impl HashSecret {
5553
pub fn hash_value<T: Hash + ?Sized>(&self, data: &T) -> PyHash {
5654
fix_sentinel(mod_int(self.hash_one(data) as _))
5755
}
@@ -94,7 +92,7 @@ pub const fn hash_pointer(value: usize) -> PyHash {
9492

9593
#[inline]
9694
#[must_use]
97-
pub fn hash_float(value: f64) -> Option<PyHash> {
95+
pub const fn hash_float(value: f64) -> Option<PyHash> {
9896
// cpython _Py_HashDouble
9997
if !value.is_finite() {
10098
return if value.is_infinite() {
@@ -111,6 +109,8 @@ pub fn hash_float(value: f64) -> Option<PyHash> {
111109
let mut m = frexp.0;
112110
let mut e = frexp.1;
113111
let mut x: PyUHash = 0;
112+
113+
#[expect(clippy::while_float, reason = "keep this loop like CPython does it")]
114114
while m != 0.0 {
115115
x = ((x << 28) & MODULUS) | (x >> (BITS - 28));
116116
m *= 268_435_456.0; // 2**28
@@ -137,13 +137,14 @@ pub fn hash_float(value: f64) -> Option<PyHash> {
137137

138138
#[must_use]
139139
pub fn hash_bigint(value: &BigInt) -> PyHash {
140-
let ret = match value.to_i64() {
141-
Some(i) => mod_int(i),
142-
None => (value % MODULUS).to_i64().unwrap_or_else(|| unsafe {
143-
// SAFETY: MODULUS < i64::MAX, so value % MODULUS is guaranteed to be in the range of i64
144-
core::hint::unreachable_unchecked()
145-
}),
140+
let ret = if let Some(v) = value.to_i64() {
141+
mod_int(v)
142+
} else {
143+
// SAFETY:
144+
// MODULUS < i64::MAX, so value % MODULUS is guaranteed to be in the range of i64
145+
unsafe { (value % MODULUS).to_i64().unwrap_unchecked() }
146146
};
147+
147148
fix_sentinel(ret)
148149
}
149150

crates/jit/src/instructions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,11 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
666666
| Instruction::LoadFastBorrowLoadFastBorrow { var_nums } => {
667667
let oparg = var_nums.get(arg);
668668
let (idx1, idx2) = oparg.indexes();
669+
670+
#[expect(
671+
clippy::tuple_array_conversions,
672+
reason = "Seems like a false positive"
673+
)]
669674
for idx in [idx1, idx2] {
670675
let local = self.variables[idx]
671676
.as_ref()

crates/vm/src/function/builtin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ const fn zst_ref_out_of_thin_air<T: 'static>(x: T) -> &'static T {
5757
// operation. if T isn't zero-sized, we don't have to worry about it because we'll fail to compile.
5858
core::mem::forget(x);
5959
const {
60-
if core::mem::size_of::<T>() != 0 {
61-
panic!("can't use a non-zero-sized type here")
62-
}
60+
assert!(
61+
core::mem::size_of::<T>() == 0,
62+
"can't use a non-zero-sized type here"
63+
);
6364
// SAFETY: we just confirmed that T is zero-sized, so we can
6465
// pull a value of it out of thin air.
6566
unsafe { core::ptr::NonNull::<T>::dangling().as_ref() }

crates/vm/src/getpath.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn init_path_config(settings: &Settings) -> Paths {
121121
// - sys.executable should be the launcher path (where user invoked Python)
122122
// - sys._base_executable should be the real Python executable
123123
let exe_dir = if let Ok(launcher) = crate::host_env::os::var("__PYVENV_LAUNCHER__") {
124-
paths.executable = launcher.clone();
124+
paths.executable.clone_from(&launcher);
125125
paths.base_executable = real_executable;
126126
PathBuf::from(&launcher).parent().map(PathBuf::from)
127127
} else {
@@ -152,7 +152,7 @@ pub fn init_path_config(settings: &Settings) -> Paths {
152152
paths.base_prefix = calculated_prefix;
153153
} else {
154154
// Not in venv: prefix == base_prefix
155-
paths.prefix = calculated_prefix.clone();
155+
paths.prefix.clone_from(&calculated_prefix);
156156
paths.base_prefix = calculated_prefix;
157157
}
158158

@@ -163,7 +163,7 @@ pub fn init_path_config(settings: &Settings) -> Paths {
163163
} else {
164164
calculate_exec_prefix(search_dir.as_ref(), paths.prefix.as_ref())
165165
};
166-
paths.base_exec_prefix = paths.base_prefix.clone();
166+
paths.base_exec_prefix.clone_from(&paths.base_prefix);
167167

168168
// Step 7: Calculate base_executable (if not already set by __PYVENV_LAUNCHER__)
169169
if paths.base_executable.is_empty() {

crates/vm/src/stdlib/os.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,9 +1795,9 @@ pub(super) mod _os {
17951795
#[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))]
17961796
#[pyfunction]
17971797
fn getloadavg(vm: &VirtualMachine) -> PyResult<(f64, f64, f64)> {
1798-
let loadavg = crate::host_env::time::getloadavg()
1799-
.map_err(|_| vm.new_os_error("Load averages are unobtainable"))?;
1800-
Ok((loadavg[0], loadavg[1], loadavg[2]))
1798+
crate::host_env::time::getloadavg()
1799+
.map(Into::into)
1800+
.map_err(|_| vm.new_os_error("Load averages are unobtainable"))
18011801
}
18021802

18031803
#[cfg(unix)]
@@ -1918,7 +1918,6 @@ pub(super) mod _os {
19181918
}
19191919
}
19201920

1921-
/// Perform a statvfs system call on the given path.
19221921
#[cfg(all(unix, not(target_os = "redox")))]
19231922
#[pyfunction]
19241923
#[pyfunction(name = "fstatvfs")]

crates/vm/src/types/slot.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,11 +1667,10 @@ pub trait Initializer: PyPayload {
16671667
.matches(&class_name_for_debug as &str)
16681668
.count()
16691669
== 2;
1670-
if double_appearance {
1671-
panic!(
1672-
"This type `{class_name_for_debug}` doesn't seem to support `init`. Override `slot_init` instead: {msg}"
1673-
);
1674-
}
1670+
assert!(
1671+
!double_appearance,
1672+
"This type `{class_name_for_debug}` doesn't seem to support `init`. Override `slot_init` instead: {msg}"
1673+
)
16751674
}
16761675
}
16771676
return Err(err);

0 commit comments

Comments
 (0)