rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`collapsible_match` suggestions for pattern guards can cause compilation errors

Open jsgroth opened this issue 7 months ago • 3 comments

Summary

The part of collapsible_match that checks whether an if statement can be collapsed into a match arm does not appear to take into account that variables bound in match arms are immutable in guards, even if the variable itself is mutable or contains a mutable reference. In cases where the if statement calls a method on the destructured value that requires a mutable reference (or it otherwise mutates the value), this causes the lint to recommend a change that makes the code fail to compile.

Reproducer

Code:

struct Foo(u32);

impl Foo {
    fn mutates(&mut self) -> bool {
        self.0 += 1;
        self.0.is_multiple_of(2)
    }
}

#[allow(clippy::single_match)]
fn main() {
    let mut value: Option<Foo> = Some(Foo(42));
    match &mut value {
        Some(inner) => {
            if inner.mutates() {
                println!("hello");
            }
        }
        _ => {}
    }
}

Current output:

warning: this `if` can be collapsed into the outer `match`
  --> src/main.rs:15:13
   |
15 | /             if inner.mutates() {
16 | |                 println!("hello");
17 | |             }
   | |_____________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.95.0/index.html#collapsible_match
   = note: `#[warn(clippy::collapsible_match)]` on by default
help: collapse nested if block
   |
14 ~         Some(inner)
15 ~             if inner.mutates() => {
16 |                 println!("hello");
17 ~             }
   |

After this change, the code does not compile:

error[E0596]: cannot borrow `*inner` as mutable, as it is immutable for the pattern guard
  --> src/main.rs:15:16
   |
15 |             if inner.mutates() => {
   |                ^^^^^ cannot borrow as mutable
   |
   = note: variables bound in patterns are immutable until the end of the pattern guard

error: aborting due to 1 previous error

Desired output: Clippy should not recommend this change because the changed code does not compile.

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

jsgroth avatar Apr 16 '26 16:04 jsgroth

I just sent a fix for a related collapsible_match issue (#16860 / #16865) and this is a different problem in the same code path — the if-to-guard suggestion doesn't check whether the condition mutates pattern-bound variables. Variables are immutable in guards, so if inner.mutates() works in the arm body but not as a guard.

The fix would be to skip the suggestion when the if condition requires mutable access to any pattern binding. I can send a separate PR for this.

JasmineLCY avatar Apr 16 '26 19:04 JasmineLCY

This should have been fixed by #16708 which will be in Rust 1.96.

samueltardieu avatar Apr 18 '26 07:04 samueltardieu

@rustbot claim

JasmineLCY avatar Apr 22 '26 11:04 JasmineLCY