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

clippy::nonminimal_bool False positive on !0u32 #12408

Closed
ithinuel opened this issue Mar 3, 2024 · 2 comments
Closed

clippy::nonminimal_bool False positive on !0u32 #12408

ithinuel opened this issue Mar 3, 2024 · 2 comments
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

@ithinuel
Copy link

ithinuel commented Mar 3, 2024

Summary

Clippy suggests to transform var == !0u32 into var != 0u32 which are not equivalent.

See: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=97011897f0ad26ee23256f928eae9df2

Lint Name

clippy::nonminimal_bool

Reproducer

I tried this code:

use std::hint::black_box;

fn dev_id() -> u32 {
    0xFFFF_FFFF
}

fn main() {
    let id = black_box(dev_id());
    if id == 0 || id == !0 {
        println!("yes");
    }
}

I saw this happen:

warning: this boolean expression can be simplified
 --> src/main.rs:9:19
  |
9 |     if id == 0 || id == !0 {
  |                   ^^^^^^^^ help: try: `id != 0`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
  = note: `#[warn(clippy::nonminimal_bool)]` on by default

I expected to see this happen:
No warnings.

Version

rustc 1.78.0-nightly (2dceda4f3 2024-03-01)
binary: rustc
commit-hash: 2dceda4f32b97f60b122f2b32491e0267ef5cc0c
commit-date: 2024-03-01
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Additional Labels

No response

@ithinuel ithinuel 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 Mar 3, 2024
@ithinuel ithinuel changed the title [clippy::nonminimal_bool] False positive on !0u32 clippy::nonminimal_bool False positive on !0u32 Mar 3, 2024
@y21
Copy link
Member

y21 commented Mar 3, 2024

This was fixed in #12372 and the reproducer does not emit a warning on master anymore, so I think it's safe to close this :)

@y21 y21 closed this as completed Mar 3, 2024
@ithinuel
Copy link
Author

ithinuel commented Mar 4, 2024

Oh, I didn’t see that, thanks!

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

No branches or pull requests

2 participants