@@ -10,7 +10,7 @@ use rustc_middle::ty::{self, Instance, Mutability};
10
10
use rustc_session:: impl_lint_pass;
11
11
use rustc_span:: def_id:: DefId ;
12
12
use rustc_span:: symbol:: sym;
13
- use rustc_span:: ExpnKind ;
13
+ use rustc_span:: { ExpnKind , SyntaxContext } ;
14
14
15
15
declare_clippy_lint ! {
16
16
/// ### What it does
@@ -68,7 +68,8 @@ impl_lint_pass!(AssigningClones => [ASSIGNING_CLONES]);
68
68
impl < ' tcx > LateLintPass < ' tcx > for AssigningClones {
69
69
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , assign_expr : & ' tcx Expr < ' _ > ) {
70
70
// Do not fire the lint in macros
71
- let expn_data = assign_expr. span ( ) . ctxt ( ) . outer_expn_data ( ) ;
71
+ let ctxt = assign_expr. span ( ) . ctxt ( ) ;
72
+ let expn_data = ctxt. outer_expn_data ( ) ;
72
73
match expn_data. kind {
73
74
ExpnKind :: AstPass ( _) | ExpnKind :: Desugaring ( _) | ExpnKind :: Macro ( ..) => return ,
74
75
ExpnKind :: Root => { } ,
@@ -83,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
83
84
} ;
84
85
85
86
if is_ok_to_suggest ( cx, lhs, & call, & self . msrv ) {
86
- suggest ( cx, assign_expr, lhs, & call) ;
87
+ suggest ( cx, ctxt , assign_expr, lhs, & call) ;
87
88
}
88
89
}
89
90
@@ -221,14 +222,20 @@ fn is_ok_to_suggest<'tcx>(cx: &LateContext<'tcx>, lhs: &Expr<'tcx>, call: &CallC
221
222
implemented_fns. contains_key ( & provided_fn. def_id )
222
223
}
223
224
224
- fn suggest < ' tcx > ( cx : & LateContext < ' tcx > , assign_expr : & Expr < ' tcx > , lhs : & Expr < ' tcx > , call : & CallCandidate < ' tcx > ) {
225
+ fn suggest < ' tcx > (
226
+ cx : & LateContext < ' tcx > ,
227
+ ctxt : SyntaxContext ,
228
+ assign_expr : & Expr < ' tcx > ,
229
+ lhs : & Expr < ' tcx > ,
230
+ call : & CallCandidate < ' tcx > ,
231
+ ) {
225
232
span_lint_and_then ( cx, ASSIGNING_CLONES , assign_expr. span , call. message ( ) , |diag| {
226
233
let mut applicability = Applicability :: Unspecified ;
227
234
228
235
diag. span_suggestion (
229
236
assign_expr. span ,
230
237
call. suggestion_msg ( ) ,
231
- call. suggested_replacement ( cx, lhs, & mut applicability) ,
238
+ call. suggested_replacement ( cx, ctxt , lhs, & mut applicability) ,
232
239
applicability,
233
240
) ;
234
241
} ) ;
@@ -274,6 +281,7 @@ impl<'tcx> CallCandidate<'tcx> {
274
281
fn suggested_replacement (
275
282
& self ,
276
283
cx : & LateContext < ' tcx > ,
284
+ ctxt : SyntaxContext ,
277
285
lhs : & Expr < ' tcx > ,
278
286
applicability : & mut Applicability ,
279
287
) -> String {
@@ -293,7 +301,7 @@ impl<'tcx> CallCandidate<'tcx> {
293
301
// Determine whether we need to reference the argument to clone_from().
294
302
let clone_receiver_type = cx. typeck_results ( ) . expr_ty ( receiver) ;
295
303
let clone_receiver_adj_type = cx. typeck_results ( ) . expr_ty_adjusted ( receiver) ;
296
- let mut arg_sugg = Sugg :: hir_with_applicability ( cx, receiver, "_" , applicability) ;
304
+ let mut arg_sugg = Sugg :: hir_with_context ( cx, receiver, ctxt , "_" , applicability) ;
297
305
if clone_receiver_type != clone_receiver_adj_type {
298
306
// The receiver may have been a value type, so we need to add an `&` to
299
307
// be sure the argument to clone_from will be a reference.
@@ -311,7 +319,7 @@ impl<'tcx> CallCandidate<'tcx> {
311
319
Sugg :: hir_with_applicability ( cx, lhs, "_" , applicability) . mut_addr ( )
312
320
} ;
313
321
// The RHS had to be exactly correct before the call, there is no auto-deref for function calls.
314
- let rhs_sugg = Sugg :: hir_with_applicability ( cx, self_arg, "_" , applicability) ;
322
+ let rhs_sugg = Sugg :: hir_with_context ( cx, self_arg, ctxt , "_" , applicability) ;
315
323
316
324
format ! ( "Clone::clone_from({self_sugg}, {rhs_sugg})" )
317
325
} ,
@@ -340,11 +348,11 @@ impl<'tcx> CallCandidate<'tcx> {
340
348
341
349
match self . kind {
342
350
CallKind :: MethodCall { receiver } => {
343
- let receiver_sugg = Sugg :: hir_with_applicability ( cx, receiver, "_" , applicability) ;
351
+ let receiver_sugg = Sugg :: hir_with_context ( cx, receiver, ctxt , "_" , applicability) ;
344
352
format ! ( "{receiver_sugg}.clone_into({rhs_sugg})" )
345
353
} ,
346
354
CallKind :: FunctionCall { self_arg, .. } => {
347
- let self_sugg = Sugg :: hir_with_applicability ( cx, self_arg, "_" , applicability) ;
355
+ let self_sugg = Sugg :: hir_with_context ( cx, self_arg, ctxt , "_" , applicability) ;
348
356
format ! ( "ToOwned::clone_into({self_sugg}, {rhs_sugg})" )
349
357
} ,
350
358
}
0 commit comments