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

Invalid clippy suggestion for !(a as type >= b) #12625

Closed
folkertdev opened this issue Apr 3, 2024 · 0 comments · Fixed by #12626
Closed

Invalid clippy suggestion for !(a as type >= b) #12625

folkertdev opened this issue Apr 3, 2024 · 0 comments · Fixed by #12626
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@folkertdev
Copy link
Contributor

Summary

it would be really surprising to me if this is not a duplicate, but I can't find a similar issue yet.

The problem is that in the right-hand side of an as ... expression, a < is interpreted as starting a generic instantiation (as in as Foo<Bar>. Hence, the suggestion needs to include extra parentheses so that the < in interpreted as a comparison operator as intended.

Reproducer

I tried this code:

fn main() {
    let b = 0;
    let a = 0;

    if !(a as std::ffi::c_uint >= b) {
        // ...
    }
}

I expected to see this happen:

warning: this boolean expression can be simplified
 --> src/main.rs:5:8
  |
5 |     if !(a as std::ffi::c_uint >= b) {
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(a as std::ffi::c_uint) < b`
  |
  = 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

Instead, this happened:

warning: this boolean expression can be simplified
 --> src/main.rs:5:8
  |
5 |     if !(a as std::ffi::c_uint >= b) {
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a as std::ffi::c_uint < b`
  |
  = 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

This blows up with cargo clippy --fix also:

> 
cargo clippy --fix --allow-dirty --allow-staged --broken-code
    Checking playground v0.1.0 (/home/folkertdev/rust/playground)
warning: failed to automatically apply fixes suggested by rustc to crate `playground`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: `<` is interpreted as a start of generic arguments for `std::ffi::c_uint`, not a comparison
 --> src/main.rs:5:30
  |
5 |     if a as std::ffi::c_uint < b {
  |                              ^ --- interpreted as generic arguments
  |                              |
  |                              not interpreted as comparison
  |
help: try comparing the cast value
  |
5 |     if (a as std::ffi::c_uint) < b {
  |        +                     +

error: aborting due to 1 previous error

Version

$ rustc --version --verbose
rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6

Additional Labels

No response

@folkertdev folkertdev added the C-bug Category: Clippy is not doing the correct thing label Apr 3, 2024
@J-ZhengLi J-ZhengLi added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 3, 2024
@bors bors closed this as completed in 2202493 Apr 8, 2024
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants