Skip to content

Commit 954137e

Browse files
authored
Rollup merge of #87453 - ibraheemdev:i-68697, r=wesleywiser
Suggest removing unnecessary &mut as help message Closes #68697
2 parents 287a252 + df5e516 commit 954137e

15 files changed

+46
-24
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
242242
.unwrap_or(false) =>
243243
{
244244
err.span_label(span, format!("cannot {ACT}", ACT = act));
245-
err.span_label(span, "try removing `&mut` here");
245+
err.span_suggestion(
246+
span,
247+
"try removing `&mut` here",
248+
String::new(),
249+
Applicability::MaybeIncorrect,
250+
);
246251
}
247252

248253
// We want to suggest users use `let mut` for local (user
@@ -324,7 +329,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
324329
} =>
325330
{
326331
err.span_label(span, format!("cannot {ACT}", ACT = act));
327-
err.span_label(span, "try removing `&mut` here");
332+
err.span_suggestion(
333+
span,
334+
"try removing `&mut` here",
335+
String::new(),
336+
Applicability::MaybeIncorrect,
337+
);
328338
}
329339

330340
PlaceRef { local, projection: [ProjectionElem::Deref] }

src/test/ui/borrowck/issue-33819.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ fn main() {
33
match op {
44
Some(ref v) => { let a = &mut v; },
55
//~^ ERROR cannot borrow `v` as mutable, as it is not declared as mutable
6+
//~| HELP try removing `&mut` here
67
None => {},
78
}
89
}

src/test/ui/borrowck/issue-33819.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | Some(ref v) => { let a = &mut v; },
55
| ^^^^^^
66
| |
77
| cannot borrow as mutable
8-
| try removing `&mut` here
8+
| help: try removing `&mut` here
99

1010
error: aborting due to previous error
1111

src/test/ui/borrowck/mut-borrow-of-mut-ref.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
pub fn f(b: &mut i32) {
55
g(&mut b);
66
//~^ ERROR cannot borrow
7+
//~| HELP try removing `&mut` here
78
g(&mut &mut b);
89
//~^ ERROR cannot borrow
10+
//~| HELP try removing `&mut` here
911
}
1012

1113
pub fn g(_: &mut i32) {}

src/test/ui/borrowck/mut-borrow-of-mut-ref.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ LL | g(&mut b);
55
| ^^^^^^
66
| |
77
| cannot borrow as mutable
8-
| try removing `&mut` here
8+
| help: try removing `&mut` here
99

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

1919
error: aborting due to 2 previous errors
2020

src/test/ui/did_you_mean/issue-31424.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ struct Struct;
55
impl Struct {
66
fn foo(&mut self) {
77
(&mut self).bar(); //~ ERROR cannot borrow
8+
//~^ HELP try removing `&mut` here
89
}
910

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

src/test/ui/did_you_mean/issue-31424.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ LL | (&mut self).bar();
55
| ^^^^^^^^^^^
66
| |
77
| cannot borrow as mutable
8-
| try removing `&mut` here
8+
| help: try removing `&mut` here
99

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

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

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

src/test/ui/did_you_mean/issue-34126.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ impl Z {
55
fn start(&mut self) {
66
self.run(&mut self); //~ ERROR cannot borrow
77
//~| ERROR cannot borrow
8+
//~| HELP try removing `&mut` here
89
}
910
}
1011

src/test/ui/did_you_mean/issue-34126.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | self.run(&mut self);
55
| ^^^^^^^^^
66
| |
77
| cannot borrow as mutable
8-
| try removing `&mut` here
8+
| help: try removing `&mut` here
99

1010
error[E0502]: cannot borrow `self` as mutable because it is also borrowed as immutable
1111
--> $DIR/issue-34126.rs:6:18

src/test/ui/did_you_mean/issue-34337.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ fn main() {
44
let mut v: Vec<String> = Vec::new();
55
let ref mut key = v[0];
66
get(&mut key); //~ ERROR cannot borrow
7+
//~| HELP try removing `&mut` here
78
}

0 commit comments

Comments
 (0)