@@ -1034,27 +1034,32 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10341034 } else {
10351035 ".clone()" . to_owned ( )
10361036 } ;
1037- if let Some ( clone_trait_def) = tcx. lang_items ( ) . clone_trait ( )
1038- && self
1039- . infcx
1040- . type_implements_trait ( clone_trait_def, [ ty] , self . param_env )
1041- . must_apply_modulo_regions ( )
1037+ let mut sugg = Vec :: with_capacity ( 2 ) ;
1038+ let mut inner_expr = expr;
1039+ while let hir:: ExprKind :: AddrOf ( .., inner) | hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , inner) =
1040+ & inner_expr. kind
10421041 {
1043- let msg = if let ty:: Adt ( def, _) = ty. kind ( )
1044- && [ tcx. get_diagnostic_item ( sym:: Arc ) , tcx. get_diagnostic_item ( sym:: Rc ) ]
1045- . contains ( & Some ( def. did ( ) ) )
1046- {
1047- "clone the value to increment its reference count"
1048- } else {
1049- "consider cloning the value if the performance cost is acceptable"
1050- } ;
1051- err. span_suggestion_verbose (
1052- span. shrink_to_hi ( ) ,
1053- msg,
1054- suggestion,
1055- Applicability :: MachineApplicable ,
1056- ) ;
1042+ inner_expr = inner;
10571043 }
1044+ if inner_expr. span . lo ( ) != expr. span . lo ( ) {
1045+ sugg. push ( ( expr. span . with_hi ( inner_expr. span . lo ( ) ) , String :: new ( ) ) ) ;
1046+ }
1047+ let span = if inner_expr. span . hi ( ) != expr. span . hi ( ) {
1048+ // Account for `(*x)` to suggest `x.clone()`.
1049+ expr. span . with_lo ( inner_expr. span . hi ( ) )
1050+ } else {
1051+ span. shrink_to_hi ( )
1052+ } ;
1053+ sugg. push ( ( span, suggestion) ) ;
1054+ let msg = if let ty:: Adt ( def, _) = ty. kind ( )
1055+ && [ tcx. get_diagnostic_item ( sym:: Arc ) , tcx. get_diagnostic_item ( sym:: Rc ) ]
1056+ . contains ( & Some ( def. did ( ) ) )
1057+ {
1058+ "clone the value to increment its reference count"
1059+ } else {
1060+ "consider cloning the value if the performance cost is acceptable"
1061+ } ;
1062+ err. multipart_suggestion_verbose ( msg, sugg, Applicability :: MachineApplicable ) ;
10581063 }
10591064
10601065 fn suggest_adding_bounds ( & self , err : & mut Diag < ' _ > , ty : Ty < ' tcx > , def_id : DefId , span : Span ) {
0 commit comments