Skip to content

collapsible_match should not trigger for if statements that take ownership #16705

@elenaf9

Description

@elenaf9

Summary

The lint triggers on beta channel for if branches that should be collapsed into the match statement.
This is not possible if the if statement takes ownership of a pattern value.

Lint Name

collapsible_match

Reproducer

I tried this code:

fn is_foo(s: String) -> bool {
    drop(s);
    true
}

fn main() {
    let x: Option<String> = Some("Test".into());

    let bar = match x {
        Some(val) => {
            if is_foo(val) {
                return;
            } else {
                false
            }
        }
        _ => false,
    };
    println!("{bar}")
}

I saw this happen:

error: this `if` can be collapsed into the outer `match`
  --> src/main.rs:11:13
   |
11 | /             if is_foo(val) {
12 | |                 return;
13 | |             } else {
14 | |                 false
15 | |             }
   | |_____________^
   |

I expected to see this happen:

The lint should not trigger. The if branch can't be collapsed into the pattern, since is_food takes ownership of val.

If the if branch is collapse, it gives an error:

error[E0507]: cannot move out of `val` in pattern guard
  --> src/main.rs:10:29
   |
10 |         Some(val) if is_foo(val) => {
   |                             ^^^ move occurs because `val` has type `std::string::String`, which does not implement the `Copy` trait

Version

rustc 1.95.0-beta.3 (94a8e826a 2026-03-08)
binary: rustc
commit-hash: 94a8e826a57fdc895506a0e9329246ae914740d5
commit-date: 2026-03-08
host: x86_64-unknown-linux-gnu
release: 1.95.0-beta.3
LLVM version: 22.1.0

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-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

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