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
Summary
The lint triggers on
betachannel 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:
I saw this happen:
I expected to see this happen:
The lint should not trigger. The if branch can't be collapsed into the pattern, since
is_foodtakes ownership ofval.If the if branch is collapse, it gives an error:
Version
Additional Labels
No response