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

Suggest removing unnecessary &mut as help message #87453

Merged
merged 4 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,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 @@ -322,7 +327,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
9 changes: 6 additions & 3 deletions src/test/ui/nll/issue-51191.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![allow(unconditional_recursion)]

ibraheemdev marked this conversation as resolved.
Show resolved Hide resolved
struct Struct;

impl Struct {
fn bar(self: &mut Self) {
//~^ WARN function cannot return without recursing
(&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() {}
28 changes: 8 additions & 20 deletions src/test/ui/nll/issue-51191.stderr
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
warning: function cannot return without recursing
--> $DIR/issue-51191.rs:4: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-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
error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0596`.