Skip to content

Error message suggests using "ref mut mut" which is invalid #37139

@upsuper

Description

@upsuper

See the following code:

enum TestEnum {
    Item(i32),
}

fn test(_: &mut i32) {
}

fn main() {
    let mut x = TestEnum::Item(10);
    match x {
        TestEnum::Item(ref mut x) => {
            test(&mut x);
        }
    }
}

The compiler raises error and suggests that:

error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
11 |         TestEnum::Item(ref mut x) => {
   |                        --------- use `ref mut mut x` here to make mutable
12 |             test(&mut x);
   |                       ^ cannot borrow mutably

which doesn't make sense, because ref mut mut is invalid.

The correct code here is actually using test(x) directly as x itself is already a mutable reference there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions