Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
upsuper opened this issue Oct 13, 2016 · 1 comment
Closed

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

upsuper opened this issue Oct 13, 2016 · 1 comment

Comments

@upsuper
Copy link
Contributor

upsuper commented Oct 13, 2016

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.

@nagisa
Copy link
Member

nagisa commented Oct 13, 2016

Similar #34337

bors added a commit that referenced this issue Nov 12, 2016
Fix invalid "ref mut mut" sugestion

Change output from:

```nocode
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
```

to

```nocode
error: cannot borrow immutable local variable `x` as mutable
  --> <anon>:12:23
   |
12 |             test(&mut x);
   |                       ^
   |                       |
   |                       cannot reborrow mutably
   |                       try removing `&mut` here
```
Fixes #37139, #34337, #34126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants