@@ -794,27 +794,32 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
794794 } else {
795795 ".clone()" . to_owned ( )
796796 } ;
797- if let Some ( clone_trait_def) = tcx. lang_items ( ) . clone_trait ( )
798- && self
799- . infcx
800- . type_implements_trait ( clone_trait_def, [ ty] , self . param_env )
801- . must_apply_modulo_regions ( )
797+ let mut sugg = Vec :: with_capacity ( 2 ) ;
798+ let mut inner_expr = expr;
799+ while let hir:: ExprKind :: AddrOf ( .., inner) | hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , inner) =
800+ & inner_expr. kind
802801 {
803- let msg = if let ty:: Adt ( def, _) = ty. kind ( )
804- && [ tcx. get_diagnostic_item ( sym:: Arc ) , tcx. get_diagnostic_item ( sym:: Rc ) ]
805- . contains ( & Some ( def. did ( ) ) )
806- {
807- "clone the value to increment its reference count"
808- } else {
809- "consider cloning the value if the performance cost is acceptable"
810- } ;
811- err. span_suggestion_verbose (
812- span. shrink_to_hi ( ) ,
813- msg,
814- suggestion,
815- Applicability :: MachineApplicable ,
816- ) ;
802+ inner_expr = inner;
817803 }
804+ if inner_expr. span . lo ( ) != expr. span . lo ( ) {
805+ sugg. push ( ( expr. span . with_hi ( inner_expr. span . lo ( ) ) , String :: new ( ) ) ) ;
806+ }
807+ let span = if inner_expr. span . hi ( ) != expr. span . hi ( ) {
808+ // Account for `(*x)` to suggest `x.clone()`.
809+ expr. span . with_lo ( inner_expr. span . hi ( ) )
810+ } else {
811+ span. shrink_to_hi ( )
812+ } ;
813+ sugg. push ( ( span, suggestion) ) ;
814+ let msg = if let ty:: Adt ( def, _) = ty. kind ( )
815+ && [ tcx. get_diagnostic_item ( sym:: Arc ) , tcx. get_diagnostic_item ( sym:: Rc ) ]
816+ . contains ( & Some ( def. did ( ) ) )
817+ {
818+ "clone the value to increment its reference count"
819+ } else {
820+ "consider cloning the value if the performance cost is acceptable"
821+ } ;
822+ err. multipart_suggestion_verbose ( msg, sugg, Applicability :: MachineApplicable ) ;
818823 }
819824
820825 fn suggest_adding_bounds ( & self , err : & mut Diag < ' _ > , ty : Ty < ' tcx > , def_id : DefId , span : Span ) {
0 commit comments