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

if-then-panic suggestion is not equivalent for complex conditions #7731

Closed
ghost opened this issue Sep 28, 2021 · 2 comments · Fixed by #7741
Closed

if-then-panic suggestion is not equivalent for complex conditions #7731

ghost opened this issue Sep 28, 2021 · 2 comments · Fixed by #7741
Labels
C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@ghost
Copy link

ghost commented Sep 28, 2021

I tried this code:

#![warn(clippy::if_then_panic)]

fn main() {
    if false && false { 
        // comment
        panic!("ABC"); 
    };
}

I expected to see this happen: The suggestion would be equivalent but use assert instead.

Instead, this happened:

 --> src/main.rs:5:5
  |
5 | /     if false && false { 
6 | |         // comment
7 | |         panic!("ABC"); 
8 | |     };
  | |_____^ help: try: `assert!(!false && false, "ABC");`

The suggestion doesn't correctly negate the condition and so changes the logic of the code. My original code will never panic. The suggestion will always panic.

Meta

Rust version (rustc -Vv):

rustc 1.57.0-nightly (fdf65053e 2021-09-07)
binary: rustc
commit-hash: fdf65053e99e8966f9bd83b5a8491326cb33d638
commit-date: 2021-09-07
host: x86_64-unknown-linux-gnu
release: 1.57.0-nightly
LLVM version: 13.0.0

@rustbot label +L-suggestion

@ghost ghost added the C-bug Category: Clippy is not doing the correct thing label Sep 28, 2021
@rustbot rustbot added the L-suggestion Lint: Improving, adding or fixing lint suggestions label Sep 28, 2021
surechen added a commit to surechen/rust-clippy that referenced this issue Sep 30, 2021
Make if_then_else handle situation of cond.kind = ExprKind::DropTemps(ExprKind::Binary(BinOpKind::And || BinOpKind::Or, left, right), ..) =
bors added a commit that referenced this issue Oct 1, 2021
Make if_then_panic handle situation of BinOpKind::And || BinOpKind::Or

fixes #7731

Make if_then_panic handle situation of cond.kind = ExprKind::DropTemps(ExprKind::Binary(BinOpKind::And || BinOpKind::Or, left, right), ..) =
@bors bors closed this as completed in fe999e8 Oct 1, 2021
@Thomasdezeeuw
Copy link
Contributor

Thomasdezeeuw commented Oct 2, 2021

Maybe I'm using an outdated version Clippy without the pr that closes this issue (clippy 0.1.57 (c02371c4 2021-10-01)), but there are more cases where the suggestion is wrong. For example:
if n == 0 { => assert!(!n == 0) (suggested)
if thread_id as usize >= MAX_THREADS { => assert!(!thread_id as usize >= MAX_THREADS)

@xFrednet
Copy link
Member

xFrednet commented Oct 2, 2021

It always takes a while until changes from this repo get synced to the rust repo that the nightly Clippy version is build from. The fix should be available to you during the next week 🙃

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 L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants