Skip to content

collapsible_match suggestion change meaning #16875

@TeXitoi

Description

@TeXitoi

Summary

Code in the form pattern => {if test {…}} is suggested to be simplified as pattern if test => {…} but the meaning is different: if the pattern match but not the test, no other pattern are checked in the original code, but other patterns are checked in the suggestion.

Lint Name

collapsible_match

Reproducer

Corrected code should have the same result as the original code, here is an example showing it is false:

fn check(a: Option<&str>, b: i32) -> i32 {
    let mut res = 0;
    match a {
        Some(_) => {
            if b == 0 {
                res = 1
            }
        }
        _ if b == 1 => res = 2,
        _ => {}
    }
    res
}

fn check_clippy_suggestion(a: Option<&str>, b: i32) -> i32 {
    let mut res = 0;
    match a {
        Some(_) if b == 0 => res = 1,
        _ if b == 1 => res = 2,
        _ => {}
    }
    res
}

fn main() {
    assert_eq!(check(Some(""), 1), check_clippy_suggestion(Some(""), 1));
}

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

Version

rustc 1.95.0 (59807616e 2026-04-14)
binary: rustc
commit-hash: 59807616e1fa2540724bfbac14d7976d7e4a3860
commit-date: 2026-04-14
host: x86_64-unknown-linux-gnu
release: 1.95.0
LLVM version: 22.1.2

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions