Skip to content

Commit a6af6d6

Browse files
committed
Provide structured suggestion for removal of &mut
1 parent e4368de commit a6af6d6

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::{
1212
};
1313
use rustc_span::source_map::DesugaringKind;
1414
use rustc_span::symbol::{kw, Symbol};
15-
use rustc_span::Span;
15+
use rustc_span::{BytePos, Span};
1616

1717
use crate::borrow_check::diagnostics::BorrowedContentSource;
1818
use crate::borrow_check::MirBorrowckCtxt;
@@ -278,7 +278,25 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
278278
);
279279
}
280280
}
281-
err.span_help(source_info.span, "try removing `&mut` here");
281+
if let Ok(snippet) =
282+
self.infcx.tcx.sess.source_map().span_to_snippet(source_info.span)
283+
{
284+
if snippet.starts_with("&mut ") {
285+
// We don't have access to the HIR to get accurate spans, but we can
286+
// give a best effort structured suggestion.
287+
err.span_suggestion_verbose(
288+
source_info.span.with_hi(source_info.span.lo() + BytePos(5)),
289+
"try removing `&mut` here",
290+
String::new(),
291+
Applicability::MachineApplicable,
292+
);
293+
} else {
294+
// This can occur with things like `(&mut self).foo()`.
295+
err.span_help(source_info.span, "try removing `&mut` here");
296+
}
297+
} else {
298+
err.span_help(source_info.span, "try removing `&mut` here");
299+
}
282300
} else if decl.mutability == Mutability::Not
283301
&& !matches!(
284302
decl.local_info,

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ note: the binding is already a mutable borrow
1010
LL | pub fn f(b: &mut i32) {
1111
| ^^^^^^^^
1212
help: try removing `&mut` here
13-
--> $DIR/mut-borrow-of-mut-ref.rs:7:7
1413
|
15-
LL | h(&mut b);
16-
| ^^^^^^
14+
LL | h(b);
15+
| --
1716

1817
error[E0596]: cannot borrow `b` as mutable, as it is not declared as mutable
1918
--> $DIR/mut-borrow-of-mut-ref.rs:11:12
@@ -27,10 +26,9 @@ note: the binding is already a mutable borrow
2726
LL | pub fn f(b: &mut i32) {
2827
| ^^^^^^^^
2928
help: try removing `&mut` here
30-
--> $DIR/mut-borrow-of-mut-ref.rs:11:12
3129
|
32-
LL | g(&mut &mut b);
33-
| ^^^^^^
30+
LL | g(&mut b);
31+
| --
3432

3533
error[E0596]: cannot borrow `b` as mutable, as it is not declared as mutable
3634
--> $DIR/mut-borrow-of-mut-ref.rs:18:12
@@ -44,10 +42,9 @@ note: the binding is already a mutable borrow
4442
LL | pub fn g(b: &mut i32) {
4543
| ^^^^^^^^
4644
help: try removing `&mut` here
47-
--> $DIR/mut-borrow-of-mut-ref.rs:18:12
4845
|
49-
LL | h(&mut &mut b);
50-
| ^^^^^^
46+
LL | h(&mut b);
47+
| --
5148

5249
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
5350
--> $DIR/mut-borrow-of-mut-ref.rs:35:5

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ note: the binding is already a mutable borrow
1010
LL | fn start(&mut self) {
1111
| ^^^^^^^^^
1212
help: try removing `&mut` here
13-
--> $DIR/issue-34126.rs:6:18
1413
|
15-
LL | self.run(&mut self);
16-
| ^^^^^^^^^
14+
LL | self.run(self);
15+
| --
1716

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

0 commit comments

Comments
 (0)