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

suggestion from nonminimal_bool with integer cast creates a syntax error due to parentheses removal #12761

Open
expectedjoe opened this issue May 4, 2024 · 0 comments
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

@expectedjoe
Copy link

Summary

When a bool expression is not minimal and the result is cast to an integer, the lint can "forget" the correct parentheses order triggering incorrect suggestions.

Reproducer

I tried this code:

fn f(a: i32) -> bool {
    !(1 >= 2) as i32 == a
}

I expected to see this suggested:

fn f(a: i32) -> bool {
    (1 < 2) as i32 == a
}

Instead, clippy suggested:

 --> src/main.rs:2:5
  |
2 |     !(1 >= 2) as i32 == a
  |     ^^^^^^^^^ help: try: `1 < 2`

and cargo clippy --fix produced:

error: comparison operators cannot be chained
 --> src/main.rs:2:7
  |
2 |     1 < 2 as i32 == a
  |       ^          ^^
  |
help: parenthesize the comparison
  |
2 |     (1 < 2 as i32) == a
  |     +            +

error: aborting due to 1 previous error

which is additionally not a good suggestion since (1 < 2 as i32) == a has a type error.

Version

rustc 1.80.0-nightly (d2d24e395 2024-05-03)
binary: rustc
commit-hash: d2d24e395a1e4fcee62ca17bf4cbddb1f903af97
commit-date: 2024-05-03
host: aarch64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.4

Additional Labels

@rustbot label +l-suggestion-causes-error

@expectedjoe expectedjoe added the C-bug Category: Clippy is not doing the correct thing label May 4, 2024
@y21 y21 added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label May 6, 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

No branches or pull requests

2 participants