Skip to content

Cannot mutably borrow in a pattern guard false positive #37510

Closed
@matklad

Description

@matklad

This code fails to compile:

fn foo(_: &mut i32) -> bool { true }

fn main() {
    let opt = Some(92);
    let mut x = 62;

    if let Some(_) = opt {

    } else if foo(&mut x) {

    }
}
error
error[E0301]: cannot mutably borrow in a pattern guard
 --> main.rs:9:19
  |
9 |     } else if foo(&mut x) {
  |                   ^^^^^^ borrowed mutably in pattern guard

error[E0301]: cannot mutably borrow in a pattern guard
 --> main.rs:9:24
  |
9 |     } else if foo(&mut x) {
  |                        ^ borrowed mutably in pattern guard

error: aborting due to 2 previous errors

I believe this is a bug, because you can just add a pair of braces to "fix" it:

if let Some(_) = opt {

} else { if foo(&mut x) {

} }

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions