@@ -425,19 +425,29 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
425
425
}
426
426
}
427
427
}
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
+ }
431
441
return Some ( match mutability {
432
442
hir:: Mutability :: MutMutable => (
433
443
sp,
434
444
"consider mutably borrowing here" ,
435
- format ! ( "{}&mut {}" , field_name, sugg_expr ) ,
445
+ format ! ( "{}&mut {}" , field_name, sugg ) ,
436
446
) ,
437
447
hir:: Mutability :: MutImmutable => (
438
448
sp,
439
449
"consider borrowing here" ,
440
- format ! ( "{}&{}" , field_name, sugg_expr ) ,
450
+ format ! ( "{}&{}" , field_name, sugg ) ,
441
451
) ,
442
452
} ) ;
443
453
}
0 commit comments