Skip to content

Provide better borrow checker error message #48708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
};

self.note_and_explain_mutbl_error(&mut db, &err, &error_span);
self.note_immutability_blame(&mut db, err.cmt.immutability_blame());
self.note_immutability_blame(&mut db, err.cmt.immutability_blame(), error_span);
db.emit();
}
err_out_of_scope(super_scope, sub_scope, cause) => {
Expand Down Expand Up @@ -1102,7 +1102,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
Origin::Ast)
}
};
self.note_immutability_blame(&mut err, blame);
self.note_immutability_blame(&mut err, blame, span);

if is_closure {
err.help("closures behind references must be called via `&mut`");
Expand Down Expand Up @@ -1181,15 +1181,26 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {

fn note_immutability_blame(&self,
db: &mut DiagnosticBuilder,
blame: Option<ImmutabilityBlame>) {
blame: Option<ImmutabilityBlame>,
error_span: Span) {
match blame {
None => {}
Some(ImmutabilityBlame::ClosureEnv(_)) => {}
Some(ImmutabilityBlame::ImmLocal(node_id)) => {
let let_span = self.tcx.hir.span(node_id);
if let ty::BindByValue(..) = self.local_binding_mode(node_id) {
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(let_span) {
let (_, is_implicit_self) = self.local_ty(node_id);
if let ty::BindByValue(bind_value_mut) = self.local_binding_mode(node_id) {
let (local_node_ty, is_implicit_self) = self.local_ty(node_id);
if let Some(local_node_ty) = local_node_ty {
match (local_node_ty.node, self.tcx.sess.codemap().span_to_snippet(let_span)) {
(hir::Ty_::TyPtr(ref ty_ptr_mutability), Ok(snippet)) if ty_ptr_mutability.mutbl == bind_value_mut => {
db.span_suggestion(
error_span,
"consider removing the `&mut`, as it is an immutable binding to a mutable reference",
snippet);
},
_ => {}
}
} else if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(let_span) {
if is_implicit_self && snippet != "self" {
// avoid suggesting `mut &self`.
return
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/unnecessary_mut_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
struct User {
name: String,
}

fn main() {
let mut user1 = User{
name: String::from("Kurtis"),
};
mutator_part1(&mut user1);
}

fn mutator_part1(user: &mut User) {
mutator_part2(&mut user);
}

fn mutator_part2(user: &mut User) {
user.name = String::from("Steve");
}
9 changes: 9 additions & 0 deletions src/test/ui/unnecessary_mut_reference.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0596]: cannot borrow immutable argument `user` as mutable
--> unnecessary_mut_reference.rs:13:24
|
12 | fn mutator_part1(user: &mut User) {
13 | mutator_part2(&mut user);
| ^^^^ cannot borrow mutably
| --------- consider removing the `&mut`, as it is an immutable binding to a mutable reference

error: aborting due to previous error