Provide structured suggestion for removal of &mut
This commit is contained in:
parent
e4368de7b1
commit
a6af6d6506
3 changed files with 28 additions and 14 deletions
|
@ -12,7 +12,7 @@ use rustc_middle::{
|
|||
};
|
||||
use rustc_span::source_map::DesugaringKind;
|
||||
use rustc_span::symbol::{kw, Symbol};
|
||||
use rustc_span::Span;
|
||||
use rustc_span::{BytePos, Span};
|
||||
|
||||
use crate::borrow_check::diagnostics::BorrowedContentSource;
|
||||
use crate::borrow_check::MirBorrowckCtxt;
|
||||
|
@ -278,7 +278,25 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||
);
|
||||
}
|
||||
}
|
||||
err.span_help(source_info.span, "try removing `&mut` here");
|
||||
if let Ok(snippet) =
|
||||
self.infcx.tcx.sess.source_map().span_to_snippet(source_info.span)
|
||||
{
|
||||
if snippet.starts_with("&mut ") {
|
||||
// We don't have access to the HIR to get accurate spans, but we can
|
||||
// give a best effort structured suggestion.
|
||||
err.span_suggestion_verbose(
|
||||
source_info.span.with_hi(source_info.span.lo() + BytePos(5)),
|
||||
"try removing `&mut` here",
|
||||
String::new(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
} else {
|
||||
// This can occur with things like `(&mut self).foo()`.
|
||||
err.span_help(source_info.span, "try removing `&mut` here");
|
||||
}
|
||||
} else {
|
||||
err.span_help(source_info.span, "try removing `&mut` here");
|
||||
}
|
||||
} else if decl.mutability == Mutability::Not
|
||||
&& !matches!(
|
||||
decl.local_info,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue