Skip to content

Commit 742b48d

Browse files
committed
Be more specific in the suggestion filtering
1 parent 0e7e938 commit 742b48d

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,23 +380,59 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
380380
if let PatKind::Binding(..) = inner.node {
381381
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
382382
let parent = tcx.hir().get_by_hir_id(parent_id);
383+
debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
383384
match parent {
384-
hir::Node::Item(_) |
385-
hir::Node::ForeignItem(_) |
386-
hir::Node::TraitItem(_) |
387-
hir::Node::ImplItem(_) => { // this pat is an argument
385+
hir::Node::Item(hir::Item {
386+
node: hir::ItemKind::Fn(..), ..
387+
}) |
388+
hir::Node::ForeignItem(hir::ForeignItem {
389+
node: hir::ForeignItemKind::Fn(..), ..
390+
}) |
391+
hir::Node::TraitItem(hir::TraitItem {
392+
node: hir::TraitItemKind::Method(..), ..
393+
}) |
394+
hir::Node::ImplItem(hir::ImplItem {
395+
node: hir::ImplItemKind::Method(..), ..
396+
}) => { // this pat is likely an argument
388397
if let Ok(snippet) = tcx.sess.source_map()
389-
.span_to_snippet(pat.span)
398+
.span_to_snippet(inner.span)
390399
{ // FIXME: turn into structured suggestion, will need
391-
// a span that also includes the the type.
400+
// a span that also includes the the arg's type.
392401
err.help(&format!(
393402
"did you mean `{}: &{}`?",
394-
&snippet[1..],
403+
snippet,
395404
expected,
396405
));
397406
}
398407
}
399-
_ => {} // don't provide the suggestion from above #55175
408+
hir::Node::Expr(hir::Expr {
409+
node: hir::ExprKind::Match(..), ..
410+
}) => { // rely on match ergonomics
411+
if let Ok(snippet) = tcx.sess.source_map()
412+
.span_to_snippet(inner.span)
413+
{
414+
err.span_suggestion(
415+
pat.span,
416+
"you can rely on match ergonomics and remove \
417+
the explicit borrow",
418+
snippet,
419+
Applicability::MaybeIncorrect,
420+
);
421+
}
422+
}
423+
hir::Node::Pat(_) => { // nested `&&pat`
424+
if let Ok(snippet) = tcx.sess.source_map()
425+
.span_to_snippet(inner.span)
426+
{
427+
err.span_suggestion(
428+
pat.span,
429+
"you can probaly remove the explicit borrow",
430+
snippet,
431+
Applicability::MaybeIncorrect,
432+
);
433+
}
434+
}
435+
_ => {} // don't provide suggestions in other cases #55175
400436
}
401437
}
402438
err.emit();

src/test/ui/destructure-trait-ref.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ error[E0308]: mismatched types
2020
--> $DIR/destructure-trait-ref.rs:31:10
2121
|
2222
LL | let &&x = &1isize as &T;
23-
| ^^ expected trait T, found reference
23+
| ^^
24+
| |
25+
| expected trait T, found reference
26+
| help: you can probaly remove the explicit borrow: `x`
2427
|
2528
= note: expected type `dyn T`
2629
found type `&_`
@@ -29,7 +32,10 @@ error[E0308]: mismatched types
2932
--> $DIR/destructure-trait-ref.rs:36:11
3033
|
3134
LL | let &&&x = &(&1isize as &T);
32-
| ^^ expected trait T, found reference
35+
| ^^
36+
| |
37+
| expected trait T, found reference
38+
| help: you can probaly remove the explicit borrow: `x`
3339
|
3440
= note: expected type `dyn T`
3541
found type `&_`

src/test/ui/mismatched_types/issue-38371.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ error[E0308]: mismatched types
1212
--> $DIR/issue-38371.rs:18:9
1313
|
1414
LL | fn agh(&&bar: &u32) {
15-
| ^^^^ expected u32, found reference
15+
| ^^^^
16+
| |
17+
| expected u32, found reference
18+
| help: you can probaly remove the explicit borrow: `bar`
1619
|
1720
= note: expected type `u32`
1821
found type `&_`

0 commit comments

Comments
 (0)