Skip to content

[arithmetic_side_effects] Division by potentially-zero can panic even with Wrapping #11393

Closed
@guilliamxavier

Description

@guilliamxavier

Summary

Division (/ and %, i.e. div() and rem()) of any core::num::Wrapping by Wrapping(0) (of the same type) panics, but the arithmetic_side_effects lint currently doesn't flag them (as opposed to the same operations with primitive integers).

(Note: kind of the opposite of #11392)

Lint Name

arithmetic_side_effects

Reproducer

I tried this code:

#![warn(clippy::arithmetic_side_effects)]

use std::num::Wrapping;

fn example_div(x: Wrapping<i32>, maybe_zero: Wrapping<i32>) -> Wrapping<i32> {
    x / maybe_zero
}

fn example_rem(x: Wrapping<i32>, maybe_zero: Wrapping<i32>) -> Wrapping<i32> {
    x % maybe_zero
}

fn main() {
    use std::panic;

    let [x, maybe_zero] = [1, 0].map(Wrapping);

    println!("{x} / {maybe_zero} is {:?}", panic::catch_unwind(|| example_div(x, maybe_zero)));
    println!("{x} % {maybe_zero} is {:?}", panic::catch_unwind(|| example_rem(x, maybe_zero)));
}

(note: compiles but panics with "attempt to divide by zero" and "attempt to calculate the remainder with a divisor of zero")

I expected to see this happen:

warning: arithmetic operation that can potentially result in unexpected side-effects
 --> src/main.rs:6:5
  |
6 |     x / maybe_zero
  |     ^^^^^^^^^^^^^^
  |
  = 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 |     x % maybe_zero
   |     ^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects

Instead, this happened:

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)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions