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

+12-2
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

+1
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

+1-1
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

+2
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

+3-3
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

+3
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

+5-5
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

+1
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

+1-1
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

+1
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

+1-1
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

+1
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

+1-1
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

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ struct Struct;
33
impl Struct {
44
fn bar(self: &mut Self) {
55
//~^ WARN function cannot return without recursing
6+
//~^^ HELP a `loop` may express intention better if this is on purpose
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

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,50 @@ warning: function cannot return without recursing
33
|
44
LL | fn bar(self: &mut Self) {
55
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
6-
LL |
6+
...
77
LL | (&mut self).bar();
88
| ----------------- recursive call site
99
|
1010
= note: `#[warn(unconditional_recursion)]` on by default
1111
= help: a `loop` may express intention better if this is on purpose
1212

1313
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
14-
--> $DIR/issue-51191.rs:6:9
14+
--> $DIR/issue-51191.rs:7:9
1515
|
1616
LL | (&mut self).bar();
1717
| ^^^^^^^^^^^
1818
| |
1919
| cannot borrow as mutable
20-
| try removing `&mut` here
20+
| help: try removing `&mut` here
2121

2222
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
23-
--> $DIR/issue-51191.rs:11:9
23+
--> $DIR/issue-51191.rs:13:9
2424
|
2525
LL | fn imm(self) {
2626
| ---- help: consider changing this to be mutable: `mut self`
2727
LL | (&mut self).bar();
2828
| ^^^^^^^^^^^ cannot borrow as mutable
2929

3030
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
31-
--> $DIR/issue-51191.rs:20:9
31+
--> $DIR/issue-51191.rs:22:9
3232
|
3333
LL | (&mut self).bar();
3434
| ^^^^^^^^^^^ cannot borrow as mutable
3535

3636
error[E0596]: cannot borrow data in a `&` reference as mutable
37-
--> $DIR/issue-51191.rs:20:9
37+
--> $DIR/issue-51191.rs:22:9
3838
|
3939
LL | (&mut self).bar();
4040
| ^^^^^^^^^^^ cannot borrow as mutable
4141

4242
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
43-
--> $DIR/issue-51191.rs:26:9
43+
--> $DIR/issue-51191.rs:28:9
4444
|
4545
LL | (&mut self).bar();
4646
| ^^^^^^^^^^^
4747
| |
4848
| cannot borrow as mutable
49-
| try removing `&mut` here
49+
| help: try removing `&mut` here
5050

5151
error: aborting due to 5 previous errors; 1 warning emitted
5252

0 commit comments

Comments
 (0)