Closed
Description
Summary
Division (/
and %
, i.e. div()
and rem()
) implemented for the following pairs of types:
(u8, core::num::NonZeroU8)
(u16, core::num::NonZeroU16)
(u32, core::num::NonZeroU32)
(u64, core::num::NonZeroU64)
(u128, core::num::NonZeroU128)
(usize, core::num::NonZeroUsize)
never panics (because the (x, 0)
and (MIN, -1)
cases are impossible), but the arithmetic_side_effects
lint currently flags them.
(Note: I recognize that this is a very specific case and don't know if it can be fixed)
Lint Name
arithmetic_side_effects
Reproducer
I tried this code:
#![warn(clippy::arithmetic_side_effects)]
use std::num::NonZeroUsize;
fn example_div(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
unsigned / nonzero_unsigned
}
fn example_rem(unsigned: usize, nonzero_unsigned: NonZeroUsize) -> usize {
unsigned % nonzero_unsigned
}
fn main() {
let (unsigned, nonzero_unsigned) = (0, NonZeroUsize::new(1).unwrap());
println!("{unsigned} / {nonzero_unsigned} is {}", example_div(unsigned, nonzero_unsigned));
println!("{unsigned} % {nonzero_unsigned} is {}", example_rem(unsigned, nonzero_unsigned));
}
(note: compiles and runs fine, output:
0 / 1 is Ok(0)
0 % 1 is Ok(0)
)
I saw this happen:
warning: arithmetic operation that can potentially result in unexpected side-effects
--> src/main.rs:6:5
|
6 | unsigned / nonzero_unsigned
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects
note: the lint level is defined here
--> src/main.rs:1:9
|
1 | #![warn(clippy::arithmetic_side_effects)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: arithmetic operation that can potentially result in unexpected side-effects
--> src/main.rs:10:5
|
10 | unsigned % nonzero_unsigned
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects
I expected to see this happen:
No warning
Version
Clippy 0.1.73 (2023-08-23 249595b) on the Rust Playground,
which I guess corresponds to rustc 1.74.0-nightly (2023-08-23 249595b7523fc07a99c1)
Additional Labels
No response