@@ -425,19 +425,29 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
425425 }
426426 }
427427 }
428- // If this expression had a clone call, when suggesting borrowing, we
429- // want to suggest removing it
430- let sugg_expr = sugg_expr. trim_end_matches ( ".clone()" ) ;
428+
429+ let mut sugg = sugg_expr. as_str ( ) ;
430+ if let hir:: ExprKind :: MethodCall ( _segment, _sp, _args) = & expr. node {
431+ let clone_path = "std::clone::Clone::clone" ;
432+ if let Some ( true ) = self . tables . borrow ( )
433+ . type_dependent_def_id ( expr. hir_id )
434+ . map ( |did| self . tcx . def_path_str ( did) . as_str ( ) == clone_path)
435+ {
436+ // If this expression had a clone call when suggesting borrowing
437+ // we want to suggest removing it because it'd now be unecessary.
438+ sugg = sugg_expr. trim_end_matches ( ".clone()" ) ;
439+ }
440+ }
431441 return Some ( match mutability {
432442 hir:: Mutability :: MutMutable => (
433443 sp,
434444 "consider mutably borrowing here" ,
435- format ! ( "{}&mut {}" , field_name, sugg_expr ) ,
445+ format ! ( "{}&mut {}" , field_name, sugg ) ,
436446 ) ,
437447 hir:: Mutability :: MutImmutable => (
438448 sp,
439449 "consider borrowing here" ,
440- format ! ( "{}&{}" , field_name, sugg_expr ) ,
450+ format ! ( "{}&{}" , field_name, sugg ) ,
441451 ) ,
442452 } ) ;
443453 }
0 commit comments