@@ -379,7 +379,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
379
379
}
380
380
} ;
381
381
if self . can_coerce ( ref_ty, expected) {
382
- if let Ok ( src) = cm. span_to_snippet ( sp) {
382
+ let mut sugg_sp = sp;
383
+ if let hir:: ExprKind :: MethodCall ( _segment, _sp, args) = & expr. node {
384
+ let clone_path = "std::clone::Clone::clone" ;
385
+ if let ( [ arg] , Some ( true ) ) = ( & args[ ..] , self . tables . borrow ( )
386
+ . type_dependent_def_id ( expr. hir_id )
387
+ . map ( |did| self . tcx . def_path_str ( did) . as_str ( ) == clone_path) )
388
+ {
389
+ // If this expression had a clone call when suggesting borrowing
390
+ // we want to suggest removing it because it'd now be unecessary.
391
+ sugg_sp = arg. span ;
392
+ }
393
+ }
394
+ if let Ok ( src) = cm. span_to_snippet ( sugg_sp) {
383
395
let needs_parens = match expr. node {
384
396
// parenthesize if needed (Issue #46756)
385
397
hir:: ExprKind :: Cast ( _, _) |
@@ -426,28 +438,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
426
438
}
427
439
}
428
440
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
- }
441
441
return Some ( match mutability {
442
442
hir:: Mutability :: MutMutable => (
443
443
sp,
444
444
"consider mutably borrowing here" ,
445
- format ! ( "{}&mut {}" , field_name, sugg ) ,
445
+ format ! ( "{}&mut {}" , field_name, sugg_expr ) ,
446
446
) ,
447
447
hir:: Mutability :: MutImmutable => (
448
448
sp,
449
449
"consider borrowing here" ,
450
- format ! ( "{}&{}" , field_name, sugg ) ,
450
+ format ! ( "{}&{}" , field_name, sugg_expr ) ,
451
451
) ,
452
452
} ) ;
453
453
}
0 commit comments