Skip to content

Commit

Permalink
Rollup merge of #87453 - ibraheemdev:i-68697, r=wesleywiser
Browse files Browse the repository at this point in the history
Suggest removing unnecessary &mut as help message

Closes #68697
  • Loading branch information
JohnTitor committed Jul 28, 2021
2 parents 287a252 + df5e516 commit 954137e
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
.unwrap_or(false) =>
{
err.span_label(span, format!("cannot {ACT}", ACT = act));
err.span_label(span, "try removing `&mut` here");
err.span_suggestion(
span,
"try removing `&mut` here",
String::new(),
Applicability::MaybeIncorrect,
);
}

// We want to suggest users use `let mut` for local (user
Expand Down Expand Up @@ -324,7 +329,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
} =>
{
err.span_label(span, format!("cannot {ACT}", ACT = act));
err.span_label(span, "try removing `&mut` here");
err.span_suggestion(
span,
"try removing `&mut` here",
String::new(),
Applicability::MaybeIncorrect,
);
}

PlaceRef { local, projection: [ProjectionElem::Deref] }
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/borrowck/issue-33819.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ fn main() {
match op {
Some(ref v) => { let a = &mut v; },
//~^ ERROR cannot borrow `v` as mutable, as it is not declared as mutable
//~| HELP try removing `&mut` here
None => {},
}
}
2 changes: 1 addition & 1 deletion src/test/ui/borrowck/issue-33819.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | Some(ref v) => { let a = &mut v; },
| ^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/borrowck/mut-borrow-of-mut-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
pub fn f(b: &mut i32) {
g(&mut b);
//~^ ERROR cannot borrow
//~| HELP try removing `&mut` here
g(&mut &mut b);
//~^ ERROR cannot borrow
//~| HELP try removing `&mut` here
}

pub fn g(_: &mut i32) {}
6 changes: 3 additions & 3 deletions src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ LL | g(&mut b);
| ^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error[E0596]: cannot borrow `b` as mutable, as it is not declared as mutable
--> $DIR/mut-borrow-of-mut-ref.rs:7:12
--> $DIR/mut-borrow-of-mut-ref.rs:8:12
|
LL | g(&mut &mut b);
| ^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error: aborting due to 2 previous errors

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/did_you_mean/issue-31424.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ struct Struct;
impl Struct {
fn foo(&mut self) {
(&mut self).bar(); //~ ERROR cannot borrow
//~^ HELP try removing `&mut` here
}

// In this case we could keep the suggestion, but to distinguish the
// two cases is pretty hard. It's an obscure case anyway.
fn bar(self: &mut Self) {
//~^ WARN function cannot return without recursing
//~^^ HELP a `loop` may express intention better if this is on purpose
(&mut self).bar(); //~ ERROR cannot borrow
//~^ HELP try removing `&mut` here
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/did_you_mean/issue-31424.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ LL | (&mut self).bar();
| ^^^^^^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

warning: function cannot return without recursing
--> $DIR/issue-31424.rs:12:5
--> $DIR/issue-31424.rs:13:5
|
LL | fn bar(self: &mut Self) {
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
LL |
...
LL | (&mut self).bar();
| ----------------- recursive call site
|
= note: `#[warn(unconditional_recursion)]` on by default
= help: a `loop` may express intention better if this is on purpose

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-31424.rs:14:9
--> $DIR/issue-31424.rs:16:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error: aborting due to 2 previous errors; 1 warning emitted

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/did_you_mean/issue-34126.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ impl Z {
fn start(&mut self) {
self.run(&mut self); //~ ERROR cannot borrow
//~| ERROR cannot borrow
//~| HELP try removing `&mut` here
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-34126.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | self.run(&mut self);
| ^^^^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error[E0502]: cannot borrow `self` as mutable because it is also borrowed as immutable
--> $DIR/issue-34126.rs:6:18
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/did_you_mean/issue-34337.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ fn main() {
let mut v: Vec<String> = Vec::new();
let ref mut key = v[0];
get(&mut key); //~ ERROR cannot borrow
//~| HELP try removing `&mut` here
}
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-34337.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | get(&mut key);
| ^^^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error: aborting due to previous error

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/did_you_mean/issue-37139.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
match x {
TestEnum::Item(ref mut x) => {
test(&mut x); //~ ERROR cannot borrow `x` as mutable, as it is not declared as mutable
//~| HELP try removing `&mut` here
}
}
}
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-37139.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | test(&mut x);
| ^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error: aborting due to previous error

Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/nll/issue-51191.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ struct Struct;
impl Struct {
fn bar(self: &mut Self) {
//~^ WARN function cannot return without recursing
//~^^ HELP a `loop` may express intention better if this is on purpose
(&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~^^ HELP try removing `&mut` here
}

fn imm(self) {
fn imm(self) { //~ HELP consider changing this to be mutable
(&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
}
Expand All @@ -25,7 +27,8 @@ impl Struct {
fn mtblref(&mut self) {
(&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~^^ HELP try removing `&mut` here
}
}

fn main () {}
fn main() {}
16 changes: 8 additions & 8 deletions src/test/ui/nll/issue-51191.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,50 @@ warning: function cannot return without recursing
|
LL | fn bar(self: &mut Self) {
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
LL |
...
LL | (&mut self).bar();
| ----------------- recursive call site
|
= note: `#[warn(unconditional_recursion)]` on by default
= help: a `loop` may express intention better if this is on purpose

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:6:9
--> $DIR/issue-51191.rs:7:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:11:9
--> $DIR/issue-51191.rs:13:9
|
LL | fn imm(self) {
| ---- help: consider changing this to be mutable: `mut self`
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:20:9
--> $DIR/issue-51191.rs:22:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/issue-51191.rs:20:9
--> $DIR/issue-51191.rs:22:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
--> $DIR/issue-51191.rs:26:9
--> $DIR/issue-51191.rs:28:9
|
LL | (&mut self).bar();
| ^^^^^^^^^^^
| |
| cannot borrow as mutable
| try removing `&mut` here
| help: try removing `&mut` here

error: aborting due to 5 previous errors; 1 warning emitted

Expand Down

0 comments on commit 954137e

Please sign in to comment.