Skip to content

match-like-matches-macro does not like if_let_guards #15841

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::match-like-matches-macro

this code:

//@ run-pass
//@ edition: 2024

#![feature(if_let_guard)]

fn check_if_let(opt: Option<Option<Option<i32>>>, value: i32) -> bool {
    if let Some(first) = opt
        && let Some(second) = first
        && let Some(third) = second
        && third == value
    {
        true
    }
    else {
        false
    }
}

fn check_let_guard(opt: Option<Option<Option<i32>>>, value: i32) -> bool {
    match opt {
        Some(first) if let Some(second) = first && let Some(third) = second && third == value => {
            true
        }
        _ => {
            false
        }
    }
}

fn check_while_let(opt: Option<Option<Option<i32>>>, value: i32) -> bool {
    while let Some(first) = opt
        && let Some(second) = first
        && let Some(third) = second
        && third == value
    {
        return true;
    }
    false
}

fn main() {
    assert_eq!(check_if_let(Some(Some(Some(1))), 1), true);
    assert_eq!(check_if_let(Some(Some(Some(1))), 9), false);

    assert_eq!(check_let_guard(Some(Some(Some(1))), 1), true);
    assert_eq!(check_let_guard(Some(Some(Some(1))), 9), false);

    assert_eq!(check_while_let(Some(Some(Some(1))), 1), true);
    assert_eq!(check_while_let(Some(Some(Some(1))), 9), false);
}

caused the following diagnostics:

    Checking _then-else-blocks v0.1.0 (/tmp/icemaker_global_tempdir.paNtiJF6jVLU/icemaker_clippyfix_tempdir.l3PH0z2X7i2C/_then-else-blocks)
warning: match expression looks like `matches!` macro
  --> src/main.rs:20:5
   |
20 | /     match opt {
21 | |         Some(first) if let Some(second) = first && let Some(third) = second && third == value => {
22 | |             true
...  |
27 | |     }
   | |_____^ help: try: `matches!(opt, Some(first) if let Some(second) = first && let Some(third) = second && third == value)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
   = note: requested on the command line with `--force-warn clippy::match-like-matches-macro`

warning: `_then-else-blocks` (bin "_then-else-blocks") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s

However after applying these diagnostics, the resulting code:

//@ run-pass
//@ edition: 2024

#![feature(if_let_guard)]

fn check_if_let(opt: Option<Option<Option<i32>>>, value: i32) -> bool {
    if let Some(first) = opt
        && let Some(second) = first
        && let Some(third) = second
        && third == value
    {
        true
    }
    else {
        false
    }
}

fn check_let_guard(opt: Option<Option<Option<i32>>>, value: i32) -> bool {
    matches!(opt, Some(first) if let Some(second) = first && let Some(third) = second && third == value)
}

fn check_while_let(opt: Option<Option<Option<i32>>>, value: i32) -> bool {
    while let Some(first) = opt
        && let Some(second) = first
        && let Some(third) = second
        && third == value
    {
        return true;
    }
    false
}

fn main() {
    assert_eq!(check_if_let(Some(Some(Some(1))), 1), true);
    assert_eq!(check_if_let(Some(Some(Some(1))), 9), false);

    assert_eq!(check_let_guard(Some(Some(Some(1))), 1), true);
    assert_eq!(check_let_guard(Some(Some(Some(1))), 9), false);

    assert_eq!(check_while_let(Some(Some(Some(1))), 1), true);
    assert_eq!(check_while_let(Some(Some(Some(1))), 9), false);
}

no longer compiled:

    Checking _then-else-blocks v0.1.0 (/tmp/icemaker_global_tempdir.paNtiJF6jVLU/icemaker_clippyfix_tempdir.l3PH0z2X7i2C/_then-else-blocks)
error: no rules expected keyword `let`
   --> src/main.rs:20:34
    |
 20 |     matches!(opt, Some(first) if let Some(second) = first && let Some(third) = second && third == value)
    |                                  ^^^ no rules expected this token in macro call
    |
note: while trying to match meta-variable `$guard:expr`
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/macros/mod.rs:435:42
    |
435 |     ($expression:expr, $pattern:pat $(if $guard:expr)? $(,)?) => {
    |                                          ^^^^^^^^^^^

error: could not compile `_then-else-blocks` (bin "_then-else-blocks" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_then-else-blocks` (bin "_then-else-blocks") due to 1 previous error

Version:

rustc 1.92.0-nightly (4a54b26d3 2025-10-07)
binary: rustc
commit-hash: 4a54b26d30dac43778afb0e503524b763fce0eee
commit-date: 2025-10-07
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2

Metadata

Metadata

Assignees

Labels

I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions