Dedup suggestions

This commit is contained in:
ashtneoi 2018-08-13 03:17:53 -07:00
parent 40b5118c8a
commit 20ae08bda6
3 changed files with 654 additions and 105 deletions

View file

@ -380,11 +380,14 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
}
}
// FIXME: Move the loop outside this method and add_move_error_labels()
// so they can share it.
fn add_move_error_suggestions(
&self,
err: &mut DiagnosticBuilder<'a>,
binds_to: &[Local],
) {
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
for local in binds_to {
let bind_to = &self.mir.local_decls[*local];
if let Some(
@ -410,14 +413,19 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
suggestion = pat_snippet;
to_remove = "&";
}
err.span_suggestion(
suggestions.push((
pat_span,
&format!("consider removing the `{}`", to_remove),
format!("consider removing the `{}`", to_remove),
suggestion.to_owned(),
);
));
}
}
}
suggestions.sort_unstable_by_key(|&(span, _, _)| span);
suggestions.dedup_by_key(|&mut (span, _, _)| span);
for (span, msg, suggestion) in suggestions {
err.span_suggestion(span, &msg, suggestion);
}
}
fn add_move_error_labels(

View file

@ -10,13 +10,16 @@
#![feature(nll)]
#[derive(Clone)]
enum Either {
One(X),
Two(X),
}
#[derive(Clone)]
struct X(Y);
#[derive(Clone)]
struct Y;
pub fn main() {
@ -338,4 +341,118 @@ pub fn main() {
//~| SUGGESTION Either::One(_t)
Either::Two(_t) => (),
}
// --------
let &(X(_t), X(_u)) = &(x.clone(), x.clone());
//~^ ERROR cannot move
//~| HELP consider removing the `&`
//~| SUGGESTION (X(_t), X(_u))
if let &(Either::One(_t), Either::Two(_u)) = &(e.clone(), e.clone()) { }
//~^ ERROR cannot move
//~| HELP consider removing the `&`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
while let &(Either::One(_t), Either::Two(_u)) = &(e.clone(), e.clone()) { }
//~^ ERROR cannot move
//~| HELP consider removing the `&`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
match &(e.clone(), e.clone()) {
//~^ ERROR cannot move
&(Either::One(_t), Either::Two(_u)) => (),
//~^ HELP consider removing the `&`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
&(Either::Two(_t), Either::One(_u)) => (),
//~^ HELP consider removing the `&`
//~| SUGGESTION (Either::Two(_t), Either::One(_u))
_ => (),
}
match &(e.clone(), e.clone()) {
//~^ ERROR cannot move
&(Either::One(_t), Either::Two(_u))
//~^ HELP consider removing the `&`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
| &(Either::Two(_t), Either::One(_u)) => (),
// FIXME: would really like a suggestion here too
_ => (),
}
match &(e.clone(), e.clone()) {
//~^ ERROR cannot move
&(Either::One(_t), Either::Two(_u)) => (),
//~^ HELP consider removing the `&`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
&(Either::Two(ref _t), Either::One(ref _u)) => (),
_ => (),
}
match &(e.clone(), e.clone()) {
//~^ ERROR cannot move
&(Either::One(_t), Either::Two(_u)) => (),
//~^ HELP consider removing the `&`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
(Either::Two(_t), Either::One(_u)) => (),
_ => (),
}
fn f3(&(X(_t), X(_u)): &(X, X)) { }
//~^ ERROR cannot move
//~| HELP consider removing the `&`
//~| SUGGESTION (X(_t), X(_u))
let &mut (X(_t), X(_u)) = &mut (xm.clone(), xm.clone());
//~^ ERROR cannot move
//~| HELP consider removing the `&mut`
//~| SUGGESTION (X(_t), X(_u))
if let &mut (Either::One(_t), Either::Two(_u)) = &mut (em.clone(), em.clone()) { }
//~^ ERROR cannot move
//~| HELP consider removing the `&mut`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
while let &mut (Either::One(_t), Either::Two(_u)) = &mut (em.clone(), em.clone()) { }
//~^ ERROR cannot move
//~| HELP consider removing the `&mut`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
match &mut (em.clone(), em.clone()) {
//~^ ERROR cannot move
&mut (Either::One(_t), Either::Two(_u)) => (),
//~^ HELP consider removing the `&mut`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
&mut (Either::Two(_t), Either::One(_u)) => (),
//~^ HELP consider removing the `&mut`
//~| SUGGESTION (Either::Two(_t), Either::One(_u))
_ => (),
}
match &mut (em.clone(), em.clone()) {
//~^ ERROR cannot move
&mut (Either::One(_t), Either::Two(_u))
//~^ HELP consider removing the `&mut`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
| &mut (Either::Two(_t), Either::One(_u)) => (),
// FIXME: would really like a suggestion here too
_ => (),
}
match &mut (em.clone(), em.clone()) {
//~^ ERROR cannot move
&mut (Either::One(_t), Either::Two(_u)) => (),
//~^ HELP consider removing the `&mut`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
&mut (Either::Two(ref _t), Either::One(ref _u)) => (),
_ => (),
}
match &mut (em.clone(), em.clone()) {
//~^ ERROR cannot move
&mut (Either::One(_t), Either::Two(_u)) => (),
//~^ HELP consider removing the `&mut`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
&mut (Either::Two(ref mut _t), Either::One(ref mut _u)) => (),
_ => (),
}
match &mut (em.clone(), em.clone()) {
//~^ ERROR cannot move
&mut (Either::One(_t), Either::Two(_u)) => (),
//~^ HELP consider removing the `&mut`
//~| SUGGESTION (Either::One(_t), Either::Two(_u))
(Either::Two(_t), Either::One(_u)) => (),
_ => (),
}
fn f4(&mut (X(_t), X(_u)): &mut (X, X)) { }
//~^ ERROR cannot move
//~| HELP consider removing the `&mut`
//~| SUGGESTION (X(_t), X(_u))
}

File diff suppressed because it is too large Load diff