Rollup merge of #87453 - ibraheemdev:i-68697, r=wesleywiser

Suggest removing unnecessary &mut as help message

Closes #68697
This commit is contained in:
Yuki Okushi 2021-07-28 18:28:17 +09:00 committed by GitHub
commit 954137ea0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 46 additions and 24 deletions

View File

@ -242,7 +242,12 @@ pub(crate) fn report_mutability_error(
.unwrap_or(false) => .unwrap_or(false) =>
{ {
err.span_label(span, format!("cannot {ACT}", ACT = act)); 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 // We want to suggest users use `let mut` for local (user
@ -324,7 +329,12 @@ pub(crate) fn report_mutability_error(
} => } =>
{ {
err.span_label(span, format!("cannot {ACT}", ACT = act)); 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] } PlaceRef { local, projection: [ProjectionElem::Deref] }

View File

@ -3,6 +3,7 @@ fn main() {
match op { match op {
Some(ref v) => { let a = &mut v; }, Some(ref v) => { let a = &mut v; },
//~^ ERROR cannot borrow `v` as mutable, as it is not declared as mutable //~^ ERROR cannot borrow `v` as mutable, as it is not declared as mutable
//~| HELP try removing `&mut` here
None => {}, None => {},
} }
} }

View File

@ -5,7 +5,7 @@ LL | Some(ref v) => { let a = &mut v; },
| ^^^^^^ | ^^^^^^
| | | |
| cannot borrow as mutable | cannot borrow as mutable
| try removing `&mut` here | help: try removing `&mut` here
error: aborting due to previous error error: aborting due to previous error

View File

@ -4,8 +4,10 @@
pub fn f(b: &mut i32) { pub fn f(b: &mut i32) {
g(&mut b); g(&mut b);
//~^ ERROR cannot borrow //~^ ERROR cannot borrow
//~| HELP try removing `&mut` here
g(&mut &mut b); g(&mut &mut b);
//~^ ERROR cannot borrow //~^ ERROR cannot borrow
//~| HELP try removing `&mut` here
} }
pub fn g(_: &mut i32) {} pub fn g(_: &mut i32) {}

View File

@ -5,16 +5,16 @@ LL | g(&mut b);
| ^^^^^^ | ^^^^^^
| | | |
| cannot borrow as mutable | 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 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); LL | g(&mut &mut b);
| ^^^^^^ | ^^^^^^
| | | |
| cannot borrow as mutable | cannot borrow as mutable
| try removing `&mut` here | help: try removing `&mut` here
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

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

View File

@ -5,14 +5,14 @@ LL | (&mut self).bar();
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| | | |
| cannot borrow as mutable | cannot borrow as mutable
| try removing `&mut` here | help: try removing `&mut` here
warning: function cannot return without recursing warning: function cannot return without recursing
--> $DIR/issue-31424.rs:12:5 --> $DIR/issue-31424.rs:13:5
| |
LL | fn bar(self: &mut Self) { LL | fn bar(self: &mut Self) {
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing | ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
LL | ...
LL | (&mut self).bar(); LL | (&mut self).bar();
| ----------------- recursive call site | ----------------- recursive call site
| |
@ -20,13 +20,13 @@ LL | (&mut self).bar();
= help: a `loop` may express intention better if this is on purpose = 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 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(); LL | (&mut self).bar();
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| | | |
| cannot borrow as mutable | cannot borrow as mutable
| try removing `&mut` here | help: try removing `&mut` here
error: aborting due to 2 previous errors; 1 warning emitted error: aborting due to 2 previous errors; 1 warning emitted

View File

@ -5,6 +5,7 @@ fn run(&self, z: &mut Z) { }
fn start(&mut self) { fn start(&mut self) {
self.run(&mut self); //~ ERROR cannot borrow self.run(&mut self); //~ ERROR cannot borrow
//~| ERROR cannot borrow //~| ERROR cannot borrow
//~| HELP try removing `&mut` here
} }
} }

View File

@ -5,7 +5,7 @@ LL | self.run(&mut self);
| ^^^^^^^^^ | ^^^^^^^^^
| | | |
| cannot borrow as mutable | 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 error[E0502]: cannot borrow `self` as mutable because it is also borrowed as immutable
--> $DIR/issue-34126.rs:6:18 --> $DIR/issue-34126.rs:6:18

View File

@ -4,4 +4,5 @@ fn main() {
let mut v: Vec<String> = Vec::new(); let mut v: Vec<String> = Vec::new();
let ref mut key = v[0]; let ref mut key = v[0];
get(&mut key); //~ ERROR cannot borrow get(&mut key); //~ ERROR cannot borrow
//~| HELP try removing `&mut` here
} }

View File

@ -5,7 +5,7 @@ LL | get(&mut key);
| ^^^^^^^^ | ^^^^^^^^
| | | |
| cannot borrow as mutable | cannot borrow as mutable
| try removing `&mut` here | help: try removing `&mut` here
error: aborting due to previous error error: aborting due to previous error

View File

@ -10,6 +10,7 @@ fn main() {
match x { match x {
TestEnum::Item(ref mut x) => { TestEnum::Item(ref mut x) => {
test(&mut x); //~ ERROR cannot borrow `x` as mutable, as it is not declared as mutable test(&mut x); //~ ERROR cannot borrow `x` as mutable, as it is not declared as mutable
//~| HELP try removing `&mut` here
} }
} }
} }

View File

@ -5,7 +5,7 @@ LL | test(&mut x);
| ^^^^^^ | ^^^^^^
| | | |
| cannot borrow as mutable | cannot borrow as mutable
| try removing `&mut` here | help: try removing `&mut` here
error: aborting due to previous error error: aborting due to previous error

View File

@ -3,11 +3,13 @@
impl Struct { impl Struct {
fn bar(self: &mut Self) { fn bar(self: &mut Self) {
//~^ WARN function cannot return without recursing //~^ WARN function cannot return without recursing
//~^^ HELP a `loop` may express intention better if this is on purpose
(&mut self).bar(); (&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596] //~^ 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(); (&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596] //~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
} }
@ -25,7 +27,8 @@ fn immref(&self) {
fn mtblref(&mut self) { fn mtblref(&mut self) {
(&mut self).bar(); (&mut self).bar();
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596] //~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
//~^^ HELP try removing `&mut` here
} }
} }
fn main () {} fn main() {}

View File

@ -3,7 +3,7 @@ warning: function cannot return without recursing
| |
LL | fn bar(self: &mut Self) { LL | fn bar(self: &mut Self) {
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing | ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
LL | ...
LL | (&mut self).bar(); LL | (&mut self).bar();
| ----------------- recursive call site | ----------------- recursive call site
| |
@ -11,16 +11,16 @@ LL | (&mut self).bar();
= help: a `loop` may express intention better if this is on purpose = 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 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(); LL | (&mut self).bar();
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| | | |
| cannot borrow as mutable | 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 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) { LL | fn imm(self) {
| ---- help: consider changing this to be mutable: `mut self` | ---- help: consider changing this to be mutable: `mut self`
@ -28,25 +28,25 @@ LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable | ^^^^^^^^^^^ cannot borrow as mutable
error[E0596]: cannot borrow `self` as mutable, as it is not declared 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(); LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable | ^^^^^^^^^^^ cannot borrow as mutable
error[E0596]: cannot borrow data in a `&` reference 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(); LL | (&mut self).bar();
| ^^^^^^^^^^^ cannot borrow as mutable | ^^^^^^^^^^^ cannot borrow as mutable
error[E0596]: cannot borrow `self` as mutable, as it is not declared 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(); LL | (&mut self).bar();
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| | | |
| cannot borrow as mutable | 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; 1 warning emitted