@@ -1024,27 +1024,32 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10241024 } else {
10251025 ".clone()" . to_owned ( )
10261026 } ;
1027- if let Some ( clone_trait_def) = tcx. lang_items ( ) . clone_trait ( )
1028- && self
1029- . infcx
1030- . type_implements_trait ( clone_trait_def, [ ty] , self . param_env )
1031- . must_apply_modulo_regions ( )
1027+ let mut sugg = Vec :: with_capacity ( 2 ) ;
1028+ let mut inner_expr = expr;
1029+ while let hir:: ExprKind :: AddrOf ( .., inner) | hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , inner) =
1030+ & inner_expr. kind
10321031 {
1033- let msg = if let ty:: Adt ( def, _) = ty. kind ( )
1034- && [ tcx. get_diagnostic_item ( sym:: Arc ) , tcx. get_diagnostic_item ( sym:: Rc ) ]
1035- . contains ( & Some ( def. did ( ) ) )
1036- {
1037- "clone the value to increment its reference count"
1038- } else {
1039- "consider cloning the value if the performance cost is acceptable"
1040- } ;
1041- err. span_suggestion_verbose (
1042- span. shrink_to_hi ( ) ,
1043- msg,
1044- suggestion,
1045- Applicability :: MachineApplicable ,
1046- ) ;
1032+ inner_expr = inner;
10471033 }
1034+ if inner_expr. span . lo ( ) != expr. span . lo ( ) {
1035+ sugg. push ( ( expr. span . with_hi ( inner_expr. span . lo ( ) ) , String :: new ( ) ) ) ;
1036+ }
1037+ let span = if inner_expr. span . hi ( ) != expr. span . hi ( ) {
1038+ // Account for `(*x)` to suggest `x.clone()`.
1039+ expr. span . with_lo ( inner_expr. span . hi ( ) )
1040+ } else {
1041+ span. shrink_to_hi ( )
1042+ } ;
1043+ sugg. push ( ( span, suggestion) ) ;
1044+ let msg = if let ty:: Adt ( def, _) = ty. kind ( )
1045+ && [ tcx. get_diagnostic_item ( sym:: Arc ) , tcx. get_diagnostic_item ( sym:: Rc ) ]
1046+ . contains ( & Some ( def. did ( ) ) )
1047+ {
1048+ "clone the value to increment its reference count"
1049+ } else {
1050+ "consider cloning the value if the performance cost is acceptable"
1051+ } ;
1052+ err. multipart_suggestion_verbose ( msg, sugg, Applicability :: MachineApplicable ) ;
10481053 }
10491054
10501055 fn suggest_adding_bounds ( & self , err : & mut Diag < ' _ > , ty : Ty < ' tcx > , def_id : DefId , span : Span ) {
0 commit comments