Skip to content

integer_division false negative for NonZero denominators #14652

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

Open
SLUCHABLUB opened this issue Apr 19, 2025 · 1 comment · May be fixed by #14664
Open

integer_division false negative for NonZero denominators #14652

SLUCHABLUB opened this issue Apr 19, 2025 · 1 comment · May be fixed by #14664
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@SLUCHABLUB
Copy link

SLUCHABLUB commented Apr 19, 2025

Summary

The integer_division lint, as I've understood it, is meant to restrict the use of integer division. When dividing an integer by a NonZero version of it, whilst safe from division-by-zero errors, the remainder is still discarded. I believe that this lint should be emitted in such cases, but currently, it is not.

Lint Name

integer_division

Reproducer

I tried this code:

#![warn(clippy::integer_division)]

use std::num::NonZeroU32;

const TWO: NonZeroU32 = NonZeroU32::new(2).unwrap();

fn main() {
    let x: u32 = 3;

    // emits the lint
    println!("{}", x / 2);
    // does not emit the lint
    println!("{}", x / TWO);
    
    // both print "1"
}

I expected to see this happen:

I expected x / TWO to produce a warning.

Instead, this happened:

No warning was emitted.

Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7
@SLUCHABLUB SLUCHABLUB added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Apr 19, 2025
@alex-semenyuk
Copy link
Member

@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-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants