Be more specific in the suggestion filtering
This commit is contained in:
parent
0e7e9382fa
commit
742b48dc39
|
@ -380,23 +380,59 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||||
if let PatKind::Binding(..) = inner.node {
|
if let PatKind::Binding(..) = inner.node {
|
||||||
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
|
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
|
||||||
let parent = tcx.hir().get_by_hir_id(parent_id);
|
let parent = tcx.hir().get_by_hir_id(parent_id);
|
||||||
|
debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
|
||||||
match parent {
|
match parent {
|
||||||
hir::Node::Item(_) |
|
hir::Node::Item(hir::Item {
|
||||||
hir::Node::ForeignItem(_) |
|
node: hir::ItemKind::Fn(..), ..
|
||||||
hir::Node::TraitItem(_) |
|
}) |
|
||||||
hir::Node::ImplItem(_) => { // this pat is an argument
|
hir::Node::ForeignItem(hir::ForeignItem {
|
||||||
|
node: hir::ForeignItemKind::Fn(..), ..
|
||||||
|
}) |
|
||||||
|
hir::Node::TraitItem(hir::TraitItem {
|
||||||
|
node: hir::TraitItemKind::Method(..), ..
|
||||||
|
}) |
|
||||||
|
hir::Node::ImplItem(hir::ImplItem {
|
||||||
|
node: hir::ImplItemKind::Method(..), ..
|
||||||
|
}) => { // this pat is likely an argument
|
||||||
if let Ok(snippet) = tcx.sess.source_map()
|
if let Ok(snippet) = tcx.sess.source_map()
|
||||||
.span_to_snippet(pat.span)
|
.span_to_snippet(inner.span)
|
||||||
{ // FIXME: turn into structured suggestion, will need
|
{ // FIXME: turn into structured suggestion, will need
|
||||||
// a span that also includes the the type.
|
// a span that also includes the the arg's type.
|
||||||
err.help(&format!(
|
err.help(&format!(
|
||||||
"did you mean `{}: &{}`?",
|
"did you mean `{}: &{}`?",
|
||||||
&snippet[1..],
|
snippet,
|
||||||
expected,
|
expected,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {} // don't provide the suggestion from above #55175
|
hir::Node::Expr(hir::Expr {
|
||||||
|
node: hir::ExprKind::Match(..), ..
|
||||||
|
}) => { // rely on match ergonomics
|
||||||
|
if let Ok(snippet) = tcx.sess.source_map()
|
||||||
|
.span_to_snippet(inner.span)
|
||||||
|
{
|
||||||
|
err.span_suggestion(
|
||||||
|
pat.span,
|
||||||
|
"you can rely on match ergonomics and remove \
|
||||||
|
the explicit borrow",
|
||||||
|
snippet,
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hir::Node::Pat(_) => { // nested `&&pat`
|
||||||
|
if let Ok(snippet) = tcx.sess.source_map()
|
||||||
|
.span_to_snippet(inner.span)
|
||||||
|
{
|
||||||
|
err.span_suggestion(
|
||||||
|
pat.span,
|
||||||
|
"you can probaly remove the explicit borrow",
|
||||||
|
snippet,
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {} // don't provide suggestions in other cases #55175
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err.emit();
|
err.emit();
|
||||||
|
|
|
@ -20,7 +20,10 @@ error[E0308]: mismatched types
|
||||||
--> $DIR/destructure-trait-ref.rs:31:10
|
--> $DIR/destructure-trait-ref.rs:31:10
|
||||||
|
|
|
|
||||||
LL | let &&x = &1isize as &T;
|
LL | let &&x = &1isize as &T;
|
||||||
| ^^ expected trait T, found reference
|
| ^^
|
||||||
|
| |
|
||||||
|
| expected trait T, found reference
|
||||||
|
| help: you can probaly remove the explicit borrow: `x`
|
||||||
|
|
|
|
||||||
= note: expected type `dyn T`
|
= note: expected type `dyn T`
|
||||||
found type `&_`
|
found type `&_`
|
||||||
|
@ -29,7 +32,10 @@ error[E0308]: mismatched types
|
||||||
--> $DIR/destructure-trait-ref.rs:36:11
|
--> $DIR/destructure-trait-ref.rs:36:11
|
||||||
|
|
|
|
||||||
LL | let &&&x = &(&1isize as &T);
|
LL | let &&&x = &(&1isize as &T);
|
||||||
| ^^ expected trait T, found reference
|
| ^^
|
||||||
|
| |
|
||||||
|
| expected trait T, found reference
|
||||||
|
| help: you can probaly remove the explicit borrow: `x`
|
||||||
|
|
|
|
||||||
= note: expected type `dyn T`
|
= note: expected type `dyn T`
|
||||||
found type `&_`
|
found type `&_`
|
||||||
|
|
|
@ -12,7 +12,10 @@ error[E0308]: mismatched types
|
||||||
--> $DIR/issue-38371.rs:18:9
|
--> $DIR/issue-38371.rs:18:9
|
||||||
|
|
|
|
||||||
LL | fn agh(&&bar: &u32) {
|
LL | fn agh(&&bar: &u32) {
|
||||||
| ^^^^ expected u32, found reference
|
| ^^^^
|
||||||
|
| |
|
||||||
|
| expected u32, found reference
|
||||||
|
| help: you can probaly remove the explicit borrow: `bar`
|
||||||
|
|
|
|
||||||
= note: expected type `u32`
|
= note: expected type `u32`
|
||||||
found type `&_`
|
found type `&_`
|
||||||
|
|
Loading…
Reference in New Issue