@@ -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 ( ) ;
0 commit comments