rust-lang / rust-lang/rust-clippy

`nonminimal_bool` suggestions missing required parenthesis

Open
#8,684 1 comment 0 reactions 1 assignee View on GitHub

@liquid-dragons is already working on this.

Since Apr 24, 2022.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The nonminimal_bool suggestions are missing required parenthesis if they replace part of a larger expression. This causes cargo --fix to fail because the new code doesn't compile.

Reproducer

I tried this code:

fn main() {
    let x = 1usize;
    let b = false;
    println!("{}", !(x > 20) ^ b);
}

and then ran cargo --fix

I expected to see this happen:

The expression would be simplified to (x <= 20) ^ b

Instead, this happened:

Clippy tried to change the expression to x <= 20 ^ b which doesn't compile.

Output from Cargo:

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/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[E0277]: no implementation for `{integer} ^ bool`
 --> src/main.rs:4:28
  |
4 |     println!("{}", x <= 20 ^ b);
  |                            ^ no implementation for `{integer} ^ bool`
  |
  = help: the trait `std::ops::BitXor<bool>` is not implemented for `{integer}`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Original diagnostics will follow.

warning: this boolean expression can be simplified
 --> src/main.rs:4:20
  |
4 |     println!("{}", !(x > 20) ^ b);
  |                    ^^^^^^^^^ help: try: `x <= 20`
  |
  = note: `#[warn(clippy::nonminimal_bool)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
Version
rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-unknown-linux-gnu
release: 1.60.0
LLVM version: 14.0.0
Additional Labels

@rustbot label +I-suggestion-causes-error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.