@@ -671,7 +671,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
671671 trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
672672 ) -> bool {
673673 // It only make sense when suggesting dereferences for arguments
674- let ObligationCauseCode :: FunctionArgumentObligation { .. } = obligation. cause . code ( ) else {
674+ let ObligationCauseCode :: FunctionArgumentObligation { arg_hir_id , .. } = obligation. cause . code ( ) else {
675675 return false ;
676676 } ;
677677 let param_env = obligation. param_env ;
@@ -702,19 +702,22 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
702702 Some ( steps) . filter ( |_| self . predicate_may_hold ( & obligation) )
703703 } ) {
704704 if steps > 0 {
705- if let Ok ( src) = self . tcx . sess . source_map ( ) . span_to_snippet ( span) {
706- // Don't care about `&mut` because `DerefMut` is used less
707- // often and user will not expect autoderef happens.
708- if src. starts_with ( '&' ) && !src. starts_with ( "&mut " ) {
709- let derefs = "*" . repeat ( steps) ;
710- err. span_suggestion (
711- span,
712- "consider dereferencing here" ,
713- format ! ( "&{}{}" , derefs, & src[ 1 ..] ) ,
714- Applicability :: MachineApplicable ,
715- ) ;
716- return true ;
717- }
705+ // Don't care about `&mut` because `DerefMut` is used less
706+ // often and user will not expect autoderef happens.
707+ if let Some ( hir:: Node :: Expr ( hir:: Expr {
708+ kind :
709+ hir:: ExprKind :: AddrOf ( hir:: BorrowKind :: Ref , hir:: Mutability :: Not , expr) ,
710+ ..
711+ } ) ) = self . tcx . hir ( ) . find ( * arg_hir_id)
712+ {
713+ let derefs = "*" . repeat ( steps) ;
714+ err. span_suggestion_verbose (
715+ expr. span . shrink_to_lo ( ) ,
716+ "consider dereferencing here" ,
717+ derefs,
718+ Applicability :: MachineApplicable ,
719+ ) ;
720+ return true ;
718721 }
719722 }
720723 } else if real_trait_pred != trait_pred {
0 commit comments