about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2021-07-28 18:28:17 +0900
committerGitHub <noreply@github.com>2021-07-28 18:28:17 +0900
commit954137ea0e36232dd43b68876140f40a3bf0fe88 (patch)
treee56d45e6c4135c7433f3f2f9f0b00f7c6bff23f0
parent287a252c0bcd76b7d833f1314a6d6b182fcfee21 (diff)
parentdf5e5168d862e5d5d013f53f9c569c7b5514dd8c (diff)
downloadrust-954137ea0e36232dd43b68876140f40a3bf0fe88.tar.gz
rust-954137ea0e36232dd43b68876140f40a3bf0fe88.zip
Rollup merge of #87453 - ibraheemdev:i-68697, r=wesleywiser
Suggest removing unnecessary &mut as help message

Closes #68697
-rw-r--r--compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs14
-rw-r--r--src/test/ui/borrowck/issue-33819.rs1
-rw-r--r--src/test/ui/borrowck/issue-33819.stderr2
-rw-r--r--src/test/ui/borrowck/mut-borrow-of-mut-ref.rs2
-rw-r--r--src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr6
-rw-r--r--src/test/ui/did_you_mean/issue-31424.rs3
-rw-r--r--src/test/ui/did_you_mean/issue-31424.stderr10
-rw-r--r--src/test/ui/did_you_mean/issue-34126.rs1
-rw-r--r--src/test/ui/did_you_mean/issue-34126.stderr2
-rw-r--r--src/test/ui/did_you_mean/issue-34337.rs1
-rw-r--r--src/test/ui/did_you_mean/issue-34337.stderr2
-rw-r--r--src/test/ui/did_you_mean/issue-37139.rs1
-rw-r--r--src/test/ui/did_you_mean/issue-37139.stderr2
-rw-r--r--src/test/ui/nll/issue-51191.rs7
-rw-r--r--src/test/ui/nll/issue-51191.stderr16
15 files changed, 46 insertions, 24 deletions
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 336f48bde55..f08208a1214 100644
--- a/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs
+++ b/compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs
@@ -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
@@ -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] }
diff --git a/src/test/ui/borrowck/issue-33819.rs b/src/test/ui/borrowck/issue-33819.rs
index b73e85974a8..fff5015cdc1 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 8bc2d82cd3f..f77fdbf2b6b 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 59b541a24d1..3f092846dd4 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 8710f204698..cb7355b2335 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 d321d64a312..95ccf2a4c89 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 b9eb8dd236d..838e81043db 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 4989577dbb6..53516f4f247 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 0843df29b5c..669684fb3dd 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 bb699609b3d..e89eda33f8c 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<String> = 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 81f7b6dbf1b..1f18ea8923b 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 07d855d0969..6a19d85ff79 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 163817dd9bf..dc1bdfaaed5 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 747bfe3a8a5..836587d93b8 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 4e2e4c20a02..450993425e2 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