Skip to content

"cannot assign in match guard" error message only triggers if the pattern doesn't bind anything #137690

@meithecatte

Description

@meithecatte

Code

fn meow(mut x: Option<u32>) {
    match x {
        Some(y) if { x = None; false } => loop {},
        _ => loop {},
    }
}

Current output

error[E0506]: cannot assign to `x` because it is borrowed
 --> meow.rs:3:22
  |
3 |         Some(y) if { x = None; false } => loop {},
  |              -       ^^^^^^^^        - borrow later used here
  |              |       |
  |              |       `x` is assigned to here but it was already borrowed
  |              `x` is borrowed here

Desired output

error[E0510]: cannot assign `x` in match guard
 --> meow.rs:3:22
  |
2 |     match x {
  |           - value is immutable in match guard
3 |         Some(y) if { x = None; false } => loop {},
  |                      ^^^^^^^^ cannot assign

Rationale and extra context

No response

Other cases

// with Some(_) instead of Some(y) as the pattern, E0510 is emitted already
fn meow(mut x: Option<u32>) {
    match x {
        Some(_) if { x = None; false } => loop {},
        _ => loop {},
    }
}

Rust Version

rustc 1.87.0-nightly (85abb2763 2025-02-25)
binary: rustc
commit-hash: 85abb276361c424d64743c0965242dd0e7b866d1
commit-date: 2025-02-25
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions