diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs index 28f6508cab2da..7282098fa808d 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs @@ -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 @@ -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] } diff --git a/src/test/ui/borrowck/issue-33819.rs b/src/test/ui/borrowck/issue-33819.rs index b73e85974a8e1..fff5015cdc1c9 100644 --- a/src/test/ui/borrowck/issue-33819.rs +++ b/src/test/ui/borrowck/issue-33819.rs @@ -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 => {}, } } diff --git a/src/test/ui/borrowck/issue-33819.stderr b/src/test/ui/borrowck/issue-33819.stderr index 8bc2d82cd3f14..f77fdbf2b6bfe 100644 --- a/src/test/ui/borrowck/issue-33819.stderr +++ b/src/test/ui/borrowck/issue-33819.stderr @@ -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 diff --git a/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs b/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs index 59b541a24d1bb..3f092846dd4c0 100644 --- a/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs +++ b/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs @@ -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) {} diff --git a/src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr b/src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr index 8710f20469868..cb7355b233596 100644 --- a/src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr +++ b/src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-31424.rs b/src/test/ui/did_you_mean/issue-31424.rs index d321d64a312c6..95ccf2a4c8993 100644 --- a/src/test/ui/did_you_mean/issue-31424.rs +++ b/src/test/ui/did_you_mean/issue-31424.rs @@ -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 } } diff --git a/src/test/ui/did_you_mean/issue-31424.stderr b/src/test/ui/did_you_mean/issue-31424.stderr index b9eb8dd236d2d..838e81043db7b 100644 --- a/src/test/ui/did_you_mean/issue-31424.stderr +++ b/src/test/ui/did_you_mean/issue-31424.stderr @@ -5,14 +5,14 @@ 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 | @@ -20,13 +20,13 @@ LL | (&mut self).bar(); = 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 diff --git a/src/test/ui/did_you_mean/issue-34126.rs b/src/test/ui/did_you_mean/issue-34126.rs index 4989577dbb68f..53516f4f24713 100644 --- a/src/test/ui/did_you_mean/issue-34126.rs +++ b/src/test/ui/did_you_mean/issue-34126.rs @@ -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 } } diff --git a/src/test/ui/did_you_mean/issue-34126.stderr b/src/test/ui/did_you_mean/issue-34126.stderr index 0843df29b5c46..669684fb3ddd7 100644 --- a/src/test/ui/did_you_mean/issue-34126.stderr +++ b/src/test/ui/did_you_mean/issue-34126.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-34337.rs b/src/test/ui/did_you_mean/issue-34337.rs index bb699609b3d39..e89eda33f8c93 100644 --- a/src/test/ui/did_you_mean/issue-34337.rs +++ b/src/test/ui/did_you_mean/issue-34337.rs @@ -4,4 +4,5 @@ fn main() { let mut v: Vec = Vec::new(); let ref mut key = v[0]; get(&mut key); //~ ERROR cannot borrow + //~| HELP try removing `&mut` here } diff --git a/src/test/ui/did_you_mean/issue-34337.stderr b/src/test/ui/did_you_mean/issue-34337.stderr index 81f7b6dbf1b21..1f18ea8923bda 100644 --- a/src/test/ui/did_you_mean/issue-34337.stderr +++ b/src/test/ui/did_you_mean/issue-34337.stderr @@ -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 diff --git a/src/test/ui/did_you_mean/issue-37139.rs b/src/test/ui/did_you_mean/issue-37139.rs index 07d855d0969f3..6a19d85ff79f9 100644 --- a/src/test/ui/did_you_mean/issue-37139.rs +++ b/src/test/ui/did_you_mean/issue-37139.rs @@ -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 } } } diff --git a/src/test/ui/did_you_mean/issue-37139.stderr b/src/test/ui/did_you_mean/issue-37139.stderr index 163817dd9bf58..dc1bdfaaed568 100644 --- a/src/test/ui/did_you_mean/issue-37139.stderr +++ b/src/test/ui/did_you_mean/issue-37139.stderr @@ -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 diff --git a/src/test/ui/nll/issue-51191.rs b/src/test/ui/nll/issue-51191.rs index 747bfe3a8a575..836587d93b84b 100644 --- a/src/test/ui/nll/issue-51191.rs +++ b/src/test/ui/nll/issue-51191.rs @@ -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] } @@ -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() {} diff --git a/src/test/ui/nll/issue-51191.stderr b/src/test/ui/nll/issue-51191.stderr index 4e2e4c20a02be..450993425e26b 100644 --- a/src/test/ui/nll/issue-51191.stderr +++ b/src/test/ui/nll/issue-51191.stderr @@ -3,7 +3,7 @@ warning: function cannot return without recursing | LL | fn bar(self: &mut Self) { | ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing -LL | +... LL | (&mut self).bar(); | ----------------- recursive call site | @@ -11,16 +11,16 @@ LL | (&mut self).bar(); = 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` @@ -28,25 +28,25 @@ 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