Skip to content

collapsible_match suggests an incorrect behavior change #7575

Closed
rust-lang/rust
#88163
@dtolnay

Description

@dtolnay
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
    

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions