diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index f3cabced34df..715b82183a76 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -378,12 +378,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // `fn foo(foo: &u32)` if let Some(mut err) = err { if let PatKind::Binding(..) = inner.node { - if let Ok(snippet) = tcx.sess.source_map() - .span_to_snippet(pat.span) - { - err.help(&format!("did you mean `{}: &{}`?", - &snippet[1..], - expected)); + let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id); + let parent = tcx.hir().get_by_hir_id(parent_id); + match parent { + hir::Node::Item(_) | + hir::Node::ForeignItem(_) | + hir::Node::TraitItem(_) | + hir::Node::ImplItem(_) => { // this pat is an argument + if let Ok(snippet) = tcx.sess.source_map() + .span_to_snippet(pat.span) + { // FIXME: turn into structured suggestion, will need + // a span that also includes the the type. + err.help(&format!( + "did you mean `{}: &{}`?", + &snippet[1..], + expected, + )); + } + } + _ => {} // don't provide the suggestion from above #55175 } } err.emit(); diff --git a/src/test/ui/destructure-trait-ref.stderr b/src/test/ui/destructure-trait-ref.stderr index 34dd213e2b39..7f389299afba 100644 --- a/src/test/ui/destructure-trait-ref.stderr +++ b/src/test/ui/destructure-trait-ref.stderr @@ -24,7 +24,6 @@ LL | let &&x = &1isize as &T; | = note: expected type `dyn T` found type `&_` - = help: did you mean `x: &dyn T`? error[E0308]: mismatched types --> $DIR/destructure-trait-ref.rs:36:11 @@ -34,7 +33,6 @@ LL | let &&&x = &(&1isize as &T); | = note: expected type `dyn T` found type `&_` - = help: did you mean `x: &dyn T`? error[E0308]: mismatched types --> $DIR/destructure-trait-ref.rs:41:13 diff --git a/src/test/ui/mismatched_types/issue-38371.stderr b/src/test/ui/mismatched_types/issue-38371.stderr index 236f742db3f7..30da48ba4a8a 100644 --- a/src/test/ui/mismatched_types/issue-38371.stderr +++ b/src/test/ui/mismatched_types/issue-38371.stderr @@ -16,7 +16,6 @@ LL | fn agh(&&bar: &u32) { | = note: expected type `u32` found type `&_` - = help: did you mean `bar: &u32`? error[E0308]: mismatched types --> $DIR/issue-38371.rs:21:8