Skip to content

Commit e8238a7

Browse files
committed
suggest removing unnecessary \&mut as help message
1 parent e1d49aa commit e8238a7

15 files changed

+47
-37
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
@@ -240,7 +240,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
240240
.unwrap_or(false) =>
241241
{
242242
err.span_label(span, format!("cannot {ACT}", ACT = act));
243-
err.span_label(span, "try removing `&mut` here");
243+
err.span_suggestion(
244+
span,
245+
"try removing `&mut` here",
246+
String::new(),
247+
Applicability::MaybeIncorrect,
248+
);
244249
}
245250

246251
// We want to suggest users use `let mut` for local (user
@@ -322,7 +327,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
322327
} =>
323328
{
324329
err.span_label(span, format!("cannot {ACT}", ACT = act));
325-
err.span_label(span, "try removing `&mut` here");
330+
err.span_suggestion(
331+
span,
332+
"try removing `&mut` here",
333+
String::new(),
334+
Applicability::MaybeIncorrect,
335+
);
326336
}
327337

328338
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
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | get(&mut key);
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/did_you_mean/issue-37139.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ fn main() {
1010
match x {
1111
TestEnum::Item(ref mut x) => {
1212
test(&mut x); //~ ERROR cannot borrow `x` as mutable, as it is not declared as mutable
13+
//~| HELP try removing `&mut` here
1314
}
1415
}
1516
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | test(&mut x);
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/nll/issue-51191.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
#![allow(unconditional_recursion)]
2+
13
struct Struct;
24

35
impl Struct {
46
fn bar(self: &mut Self) {
5-
//~^ WARN function cannot return without recursing
67
(&mut self).bar();
78
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
9+
//~^^ HELP try removing `&mut` here
810
}
911

10-
fn imm(self) {
12+
fn imm(self) { //~ HELP consider changing this to be mutable
1113
(&mut self).bar();
1214
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
1315
}
@@ -25,7 +27,8 @@ impl Struct {
2527
fn mtblref(&mut self) {
2628
(&mut self).bar();
2729
//~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable [E0596]
30+
//~^^ HELP try removing `&mut` here
2831
}
2932
}
3033

31-
fn main () {}
34+
fn main() {}

src/test/ui/nll/issue-51191.stderr

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
1-
warning: function cannot return without recursing
2-
--> $DIR/issue-51191.rs:4:5
3-
|
4-
LL | fn bar(self: &mut Self) {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
6-
LL |
7-
LL | (&mut self).bar();
8-
| ----------------- recursive call site
9-
|
10-
= note: `#[warn(unconditional_recursion)]` on by default
11-
= help: a `loop` may express intention better if this is on purpose
12-
131
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
14-
--> $DIR/issue-51191.rs:6:9
2+
--> $DIR/issue-51191.rs:7:9
153
|
164
LL | (&mut self).bar();
175
| ^^^^^^^^^^^
186
| |
197
| cannot borrow as mutable
20-
| try removing `&mut` here
8+
| help: try removing `&mut` here
219

2210
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
23-
--> $DIR/issue-51191.rs:11:9
11+
--> $DIR/issue-51191.rs:13:9
2412
|
2513
LL | fn imm(self) {
2614
| ---- help: consider changing this to be mutable: `mut self`
2715
LL | (&mut self).bar();
2816
| ^^^^^^^^^^^ cannot borrow as mutable
2917

3018
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
31-
--> $DIR/issue-51191.rs:20:9
19+
--> $DIR/issue-51191.rs:22:9
3220
|
3321
LL | (&mut self).bar();
3422
| ^^^^^^^^^^^ cannot borrow as mutable
3523

3624
error[E0596]: cannot borrow data in a `&` reference as mutable
37-
--> $DIR/issue-51191.rs:20:9
25+
--> $DIR/issue-51191.rs:22:9
3826
|
3927
LL | (&mut self).bar();
4028
| ^^^^^^^^^^^ cannot borrow as mutable
4129

4230
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
43-
--> $DIR/issue-51191.rs:26:9
31+
--> $DIR/issue-51191.rs:28:9
4432
|
4533
LL | (&mut self).bar();
4634
| ^^^^^^^^^^^
4735
| |
4836
| cannot borrow as mutable
49-
| try removing `&mut` here
37+
| help: try removing `&mut` here
5038

51-
error: aborting due to 5 previous errors; 1 warning emitted
39+
error: aborting due to 5 previous errors
5240

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

0 commit comments

Comments
 (0)