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

collapsible_match suggests an incorrect behavior change #7575

Closed
dtolnay opened this issue Aug 17, 2021 · 2 comments · Fixed by rust-lang/rust#88163
Closed

collapsible_match suggests an incorrect behavior change #7575

dtolnay opened this issue Aug 17, 2021 · 2 comments · Fixed by rust-lang/rust#88163
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@dtolnay
Copy link
Member

dtolnay commented Aug 17, 2021

fn next() -> Option<Enum> {
    None
}

enum Enum {
    Yes,
    No,
}

fn main() {
    while let Some(thing) = next() {
        if let Enum::Yes = thing {
            println!("yes");
        }
    }
}
$ cargo clippy

warning: unnecessary nested `if let` or `match`
  --> src/main.rs:12:9
   |
12 | /         if let Enum::Yes = thing {
13 | |             println!("yes");
14 | |         }
   | |_________^
   |
   = note: `#[warn(clippy::collapsible_match)]` on by default
help: the outer pattern can be modified to include the inner pattern
  --> src/main.rs:11:20
   |
11 |     while let Some(thing) = next() {
   |                    ^^^^^ replace this binding
12 |         if let Enum::Yes = thing {
   |                ^^^^^^^^^ with this pattern
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match

Clippy apparently wants this loop written as:

    while let Some(Enum::Yes) = next() {
        println!("yes");
    }

which is just not the same meaning.

Meta

  • cargo clippy -V: e.g. clippy 0.1.56 (0035d9d 2021-08-16)
  • rustc -Vv:
    rustc 1.56.0-nightly (0035d9dce 2021-08-16)
    binary: rustc
    commit-hash: 0035d9dcecee49d1f7349932bfa52c05a6f83641
    commit-date: 2021-08-16
    host: x86_64-unknown-linux-gnu
    release: 1.56.0-nightly
    LLVM version: 12.0.1
    
@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 17, 2021
dtolnay added a commit to serde-rs/serde that referenced this issue Aug 17, 2021
dtolnay added a commit to dtolnay/syn that referenced this issue Aug 17, 2021
rust-lang/rust-clippy#7575

    error: unnecessary nested `if let` or `match`
        --> src/generics.rs:1190:33
         |
    1190 | / ...                   if let TokenTree::Punct(q) = token {
    1191 | | ...                       if q.as_char() == '?' {
    1192 | | ...                           if let Some(TokenTree::Ident(c)) = iter.peek() {
    1193 | | ...                               if c == "const" {
    ...    |
    1201 | | ...                       }
    1202 | | ...                   }
         | |_______________________^
         |
         = note: `-D clippy::collapsible-match` implied by `-D clippy::all`
    help: the outer pattern can be modified to include the inner pattern
        --> src/generics.rs:1189:44
         |
    1189 | ...                   while let Some(token) = iter.next() {
         |                                      ^^^^^ replace this binding
    1190 | ...                       if let TokenTree::Punct(q) = token {
         |                                  ^^^^^^^^^^^^^^^^^^^ with this pattern
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match
@camsteffen
Copy link
Contributor

@rustbot claim

@teor2345
Copy link
Contributor

We had a similar issue with an outer match:

fn next() -> Option<Enum> {
    None
}

enum Enum {
    Yes,
    No,
}

fn main() {
    match next() {
        Some(thing) => {
            if let Enum::Yes = thing {
                println!("yes");
            }
        }
        _ => panic!("unexpected None"),
    }
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c09bd33fcac6b1c5f491e995f74503ce

Specifically, the original code does nothing on Enum::No, but the suggested code panics.

teor2345 added a commit to ZcashFoundation/zebra that referenced this issue Aug 19, 2021
We don't use the suggestion here, because it's actually wrong.
See rust-lang/rust-clippy#7575
teor2345 added a commit to ZcashFoundation/zebra that referenced this issue Aug 19, 2021
We don't use the suggestion here, because it's actually wrong.
See rust-lang/rust-clippy#7575
conradoplg pushed a commit to ZcashFoundation/zebra that referenced this issue Aug 19, 2021
We don't use the suggestion here, because it's actually wrong.
See rust-lang/rust-clippy#7575
camsteffen added a commit to camsteffen/rust that referenced this issue Aug 19, 2021
…=Manishearth

Fix clippy::collapsible_match with let expressions

This fixes rust-lang/rust-clippy#7575 which is a regression from rust-lang#80357. I am fixing the bug here instead of in the clippy repo (if that's okay) because a) the regression has not been synced yet and b) I would like to land the fix on nightly asap.

The fix is basically to re-generalize `match` and `if let` for the lint implementation (they were split because `if let` no longer desugars to `match` in the HIR).

Also fixes rust-lang/rust-clippy#7586
cc `@rust-lang/clippy`
`@xFrednet` do you want to review this?
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Aug 26, 2021
Fix clippy::collapsible_match with let expressions

This fixes rust-lang#7575 which is a regression from #80357. I am fixing the bug here instead of in the clippy repo (if that's okay) because a) the regression has not been synced yet and b) I would like to land the fix on nightly asap.

The fix is basically to re-generalize `match` and `if let` for the lint implementation (they were split because `if let` no longer desugars to `match` in the HIR).

Also fixes rust-lang#7586 and fixes rust-lang#7591
cc `@rust-lang/clippy`
`@xFrednet` do you want to review this?
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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants