Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[arithmetic_side_effects] Division by NonZeroU* cannot panic #11392

Closed
guilliamxavier opened this issue Aug 24, 2023 · 1 comment · Fixed by rust-lang/rust#115415
Closed

[arithmetic_side_effects] Division by NonZeroU* cannot panic #11392

guilliamxavier opened this issue Aug 24, 2023 · 1 comment · Fixed by rust-lang/rust#115415
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@guilliamxavier
Copy link

guilliamxavier commented Aug 24, 2023

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

@guilliamxavier guilliamxavier added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 24, 2023
@c410-f3r
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants