Skip to content

Commit

Permalink
Make move error suggestions clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtneoi committed Aug 15, 2018
1 parent b05e9a7 commit 701c74e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 82 deletions.
7 changes: 5 additions & 2 deletions src/librustc_mir/borrow_check/move_errors.rs
Expand Up @@ -354,7 +354,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
// `*IndexMut::index_mut(&mut a, b)`.
err.span_suggestion(
span,
"consider removing this dereference operator",
"consider removing the `*`",
snippet[1..].to_owned(),
);
} else {
Expand Down Expand Up @@ -400,16 +400,19 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
if pat_snippet.starts_with('&') {
let pat_snippet = pat_snippet[1..].trim_left();
let suggestion;
let to_remove;
if pat_snippet.starts_with("mut")
&& pat_snippet["mut".len()..].starts_with(Pattern_White_Space)
{
suggestion = pat_snippet["mut".len()..].trim_left();
to_remove = "&mut";
} else {
suggestion = pat_snippet;
to_remove = "&";
}
err.span_suggestion(
pat_span,
"consider removing this borrow operator",
&format!("consider removing the `{}`", to_remove),
suggestion.to_owned(),
);
}
Expand Down
80 changes: 40 additions & 40 deletions src/test/ui/suggestions/dont-suggest-ref.rs
Expand Up @@ -46,26 +46,26 @@ pub fn main() {

let X(_t) = *s;
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION s
if let Either::One(_t) = *r { }
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION r
while let Either::One(_t) = *r { }
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION r
match *r {
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION r
Either::One(_t)
| Either::Two(_t) => (),
}
match *r {
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION r
Either::One(_t) => (),
Either::Two(ref _t) => (),
Expand All @@ -74,34 +74,34 @@ pub fn main() {

let X(_t) = *sm;
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION sm
if let Either::One(_t) = *rm { }
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION rm
while let Either::One(_t) = *rm { }
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION rm
match *rm {
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION rm
Either::One(_t)
| Either::Two(_t) => (),
}
match *rm {
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION rm
Either::One(_t) => (),
Either::Two(ref _t) => (),
// TODO: should suggest removing `ref` too
}
match *rm {
//~^ ERROR cannot move
//~| HELP consider removing this dereference operator
//~| HELP consider removing the `*`
//~| SUGGESTION rm
Either::One(_t) => (),
Either::Two(ref mut _t) => (),
Expand Down Expand Up @@ -176,165 +176,165 @@ pub fn main() {

let &X(_t) = s;
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&`
//~| SUGGESTION X(_t)
if let &Either::One(_t) = r { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
while let &Either::One(_t) = r { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
match r {
//~^ ERROR cannot move
&Either::One(_t)
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
| &Either::Two(_t) => (),
// TODO: would really like a suggestion here too
}
match r {
//~^ ERROR cannot move
&Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
&Either::Two(ref _t) => (),
}
match r {
//~^ ERROR cannot move
&Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
Either::Two(_t) => (),
}
fn f1(&X(_t): &X) { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&`
//~| SUGGESTION X(_t)

let &mut X(_t) = sm;
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&mut`
//~| SUGGESTION X(_t)
if let &mut Either::One(_t) = rm { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
while let &mut Either::One(_t) = rm { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
match rm {
//~^ ERROR cannot move
&mut Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
&mut Either::Two(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::Two(_t)
}
match rm {
//~^ ERROR cannot move
&mut Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
&mut Either::Two(ref _t) => (),
}
match rm {
//~^ ERROR cannot move
&mut Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
&mut Either::Two(ref mut _t) => (),
}
match rm {
//~^ ERROR cannot move
&mut Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
Either::Two(_t) => (),
}
fn f2(&mut X(_t): &mut X) { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&mut`
//~| SUGGESTION X(_t)

// --------

let &X(_t) = &x;
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&`
//~| SUGGESTION X(_t)
if let &Either::One(_t) = &e { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
while let &Either::One(_t) = &e { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
match &e {
//~^ ERROR cannot move
&Either::One(_t)
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
| &Either::Two(_t) => (),
// TODO: would really like a suggestion here too
}
match &e {
//~^ ERROR cannot move
&Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
&Either::Two(ref _t) => (),
}
match &e {
//~^ ERROR cannot move
&Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&`
//~| SUGGESTION Either::One(_t)
Either::Two(_t) => (),
}

let &mut X(_t) = &mut xm;
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&mut`
//~| SUGGESTION X(_t)
if let &mut Either::One(_t) = &mut em { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
while let &mut Either::One(_t) = &mut em { }
//~^ ERROR cannot move
//~| HELP consider removing this borrow operator
//~| HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
match &mut em {
//~^ ERROR cannot move
&mut Either::One(_t)
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
| &mut Either::Two(_t) => (),
// TODO: would really like a suggestion here too
}
match &mut em {
//~^ ERROR cannot move
&mut Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
&mut Either::Two(ref _t) => (),
}
match &mut em {
//~^ ERROR cannot move
&mut Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
&mut Either::Two(ref mut _t) => (),
}
match &mut em {
//~^ ERROR cannot move
&mut Either::One(_t) => (),
//~^ HELP consider removing this borrow operator
//~^ HELP consider removing the `&mut`
//~| SUGGESTION Either::One(_t)
Either::Two(_t) => (),
}
Expand Down

0 comments on commit 701c74e

Please sign in to comment.