Suggest not mutably borrowing a mutable reference
This commit is concerned with the case where the user tries to mutably borrow a mutable reference, thereby triggering an error. Instead of the existing suggestion to make the binding mutable, the compiler will now suggest to avoid borrowing altogether.
This commit is contained in:
parent
aa094a43cc
commit
3303e6847b
6 changed files with 114 additions and 39 deletions
9
src/test/ui/borrowck/mut-borrow-of-mut-ref.nll.stderr
Normal file
9
src/test/ui/borrowck/mut-borrow-of-mut-ref.nll.stderr
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
error[E0596]: cannot borrow immutable item `b` as mutable
|
||||
--> $DIR/mut-borrow-of-mut-ref.rs:18:7
|
||||
|
|
||||
LL | g(&mut b) //~ ERROR cannot borrow
|
||||
| ^^^^^^ cannot borrow as mutable
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0596`.
|
||||
21
src/test/ui/borrowck/mut-borrow-of-mut-ref.rs
Normal file
21
src/test/ui/borrowck/mut-borrow-of-mut-ref.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Suggest not mutably borrowing a mutable reference
|
||||
|
||||
fn main() {
|
||||
f(&mut 0)
|
||||
}
|
||||
|
||||
fn f(b: &mut i32) {
|
||||
g(&mut b) //~ ERROR cannot borrow
|
||||
}
|
||||
|
||||
fn g(_: &mut i32) {}
|
||||
13
src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr
Normal file
13
src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
error[E0596]: cannot borrow immutable argument `b` as mutable
|
||||
--> $DIR/mut-borrow-of-mut-ref.rs:18:12
|
||||
|
|
||||
LL | g(&mut b) //~ ERROR cannot borrow
|
||||
| ^ cannot borrow mutably
|
||||
help: consider removing the `&mut`, as it is an immutable binding to a mutable reference
|
||||
|
|
||||
LL | g(b) //~ ERROR cannot borrow
|
||||
| ^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0596`.
|
||||
|
|
@ -10,10 +10,12 @@ LL | (&mut self).bar(); //~ ERROR cannot borrow
|
|||
error[E0596]: cannot borrow immutable argument `self` as mutable
|
||||
--> $DIR/issue-31424.rs:23:15
|
||||
|
|
||||
LL | fn bar(self: &mut Self) {
|
||||
| --------------- consider changing this to `mut self: &mut Self`
|
||||
LL | (&mut self).bar(); //~ ERROR cannot borrow
|
||||
| ^^^^ cannot borrow mutably
|
||||
help: consider removing the `&mut`, as it is an immutable binding to a mutable reference
|
||||
|
|
||||
LL | self.bar(); //~ ERROR cannot borrow
|
||||
| ^^^^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue