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 --fix removes necessary parens when changing 'len > 0' to '!.is_empty()' #10529

Closed
ingomancer opened this issue Mar 21, 2023 · 3 comments · Fixed by #10681
Closed

clippy --fix removes necessary parens when changing 'len > 0' to '!.is_empty()' #10529

ingomancer opened this issue Mar 21, 2023 · 3 comments · Fixed by #10681
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@ingomancer
Copy link

ingomancer commented Mar 21, 2023

I have the following minimized example:

fn main() {
    let a = [1, 2, 3];
    (a.len() > 0).then(|| println!("Hello, world!"));
}

running cargo clippy on this produces the following warning:

warning: length comparison to zero
 --> src/main.rs:3:5
  |
3 |     (a.len() > 0).then(|| println!("Hello, world!"));
  |     ^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!a.is_empty()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
  = note: `#[warn(clippy::len_zero)]` on by default

which makes sense to me.

I expected to see this happen: running 'cargo clippy --fix' changes the condition to the one suggested

Instead, this happened:

cargo clippy --fix
    Checking minimal_example v0.1.0 (/home/ingo/minimal_example)
warning: failed to automatically apply fixes suggested by rustc to crate `minimal_example`

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[E0600]: cannot apply unary operator `!` to type `std::option::Option<()>`
 --> src/main.rs:3:5
  |
3 |     !a.is_empty().then(|| println!("Hello, world!"));
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot apply unary operator `!`

error: aborting due to previous error

Reinserting parens around the condition makes it compile.

fn main() {
    let a = [1, 2, 3];
    (!a.is_empty()).then(|| println!("Hello, world!"));
}

Meta

rustc --version --verbose:

rustc 1.68.0 (2c8cc3432 2023-03-06)
binary: rustc
commit-hash: 2c8cc343237b8f7d5a3c3703e3a87f2eb2c54a74
commit-date: 2023-03-06
host: x86_64-unknown-linux-gnu
release: 1.68.0
LLVM version: 15.0.6

I get the same result on nightly, which is currently:
'rustc +nightly --version --verbose':

rustc 1.70.0-nightly (44f518058 2023-03-20)
binary: rustc
commit-hash: 44f5180584404d18058cbbf224c55255db4fdcbb
commit-date: 2023-03-20
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7

Regards,
ingo

Backtrace

@ingomancer ingomancer added the C-bug Category: Clippy is not doing the correct thing label Mar 21, 2023
@ingomancer ingomancer changed the title clippy --fix removes necessary parens when changing 'len > 0' to '.is_empty()' clippy --fix removes necessary parens when changing 'len > 0' to '!.is_empty()' Mar 21, 2023
@jyn514
Copy link
Member

jyn514 commented Mar 22, 2023

You should file this in rust-lang/rust-clippy.

@ingomancer
Copy link
Author

Yeah that does sound reasonable. I just followed the link in the message.

@J-ZhengLi
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants