@@ -134,7 +134,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
134
134
spans_need_deref,
135
135
..
136
136
} = {
137
- let mut ctx = MovedVariablesCtxt :: new ( cx ) ;
137
+ let mut ctx = MovedVariablesCtxt :: default ( ) ;
138
138
let region_scope_tree = & cx. tcx . region_scope_tree ( fn_def_id) ;
139
139
euv:: ExprUseVisitor :: new (
140
140
& mut ctx,
@@ -324,98 +324,28 @@ fn requires_exact_signature(attrs: &[Attribute]) -> bool {
324
324
} )
325
325
}
326
326
327
- struct MovedVariablesCtxt < ' a , ' tcx > {
328
- cx : & ' a LateContext < ' a , ' tcx > ,
327
+ # [ derive ( Default ) ]
328
+ struct MovedVariablesCtxt {
329
329
moved_vars : FxHashSet < HirId > ,
330
330
/// Spans which need to be prefixed with `*` for dereferencing the
331
331
/// suggested additional reference.
332
332
spans_need_deref : FxHashMap < HirId , FxHashSet < Span > > ,
333
333
}
334
334
335
- impl < ' a , ' tcx > MovedVariablesCtxt < ' a , ' tcx > {
336
- fn new ( cx : & ' a LateContext < ' a , ' tcx > ) -> Self {
337
- Self {
338
- cx,
339
- moved_vars : FxHashSet :: default ( ) ,
340
- spans_need_deref : FxHashMap :: default ( ) ,
341
- }
342
- }
343
-
344
- fn move_common ( & mut self , _consume_id : HirId , _span : Span , cmt : & mc:: cmt_ < ' tcx > ) {
335
+ impl MovedVariablesCtxt {
336
+ fn move_common ( & mut self , cmt : & mc:: cmt_ < ' _ > ) {
345
337
let cmt = unwrap_downcast_or_interior ( cmt) ;
346
338
347
339
if let mc:: Categorization :: Local ( vid) = cmt. cat {
348
340
self . moved_vars . insert ( vid) ;
349
341
}
350
342
}
351
-
352
- fn non_moving_pat ( & mut self , matched_pat : & Pat , cmt : & mc:: cmt_ < ' tcx > ) {
353
- let cmt = unwrap_downcast_or_interior ( cmt) ;
354
-
355
- if let mc:: Categorization :: Local ( vid) = cmt. cat {
356
- let mut id = matched_pat. hir_id ;
357
- loop {
358
- let parent = self . cx . tcx . hir ( ) . get_parent_node ( id) ;
359
- if id == parent {
360
- // no parent
361
- return ;
362
- }
363
- id = parent;
364
-
365
- if let Some ( node) = self . cx . tcx . hir ( ) . find ( id) {
366
- match node {
367
- Node :: Expr ( e) => {
368
- // `match` and `if let`
369
- if let ExprKind :: Match ( ref c, ..) = e. kind {
370
- self . spans_need_deref
371
- . entry ( vid)
372
- . or_insert_with ( FxHashSet :: default)
373
- . insert ( c. span ) ;
374
- }
375
- } ,
376
-
377
- Node :: Stmt ( s) => {
378
- // `let <pat> = x;`
379
- if_chain ! {
380
- if let StmtKind :: Local ( ref local) = s. kind;
381
- then {
382
- self . spans_need_deref
383
- . entry( vid)
384
- . or_insert_with( FxHashSet :: default )
385
- . insert( local. init
386
- . as_ref( )
387
- . map( |e| e. span)
388
- . expect( "`let` stmt without init aren't caught by match_pat" ) ) ;
389
- }
390
- }
391
- } ,
392
-
393
- _ => { } ,
394
- }
395
- }
396
- }
397
- }
398
- }
399
343
}
400
344
401
- impl < ' a , ' tcx > euv:: Delegate < ' tcx > for MovedVariablesCtxt < ' a , ' tcx > {
345
+ impl < ' tcx > euv:: Delegate < ' tcx > for MovedVariablesCtxt {
402
346
fn consume ( & mut self , cmt : & mc:: cmt_ < ' tcx > , mode : euv:: ConsumeMode ) {
403
347
if let euv:: ConsumeMode :: Move = mode {
404
- self . move_common ( cmt. hir_id , cmt. span , cmt) ;
405
- }
406
- }
407
-
408
- fn matched_pat ( & mut self , matched_pat : & Pat , cmt : & mc:: cmt_ < ' tcx > , mode : euv:: MatchMode ) {
409
- if let euv:: MatchMode :: MovingMatch = mode {
410
- self . move_common ( matched_pat. hir_id , matched_pat. span , cmt) ;
411
- } else {
412
- self . non_moving_pat ( matched_pat, cmt) ;
413
- }
414
- }
415
-
416
- fn consume_pat ( & mut self , consume_pat : & Pat , cmt : & mc:: cmt_ < ' tcx > , mode : euv:: ConsumeMode ) {
417
- if let euv:: ConsumeMode :: Move ( _) = mode {
418
- self . move_common ( consume_pat. hir_id , consume_pat. span , cmt) ;
348
+ self . move_common ( cmt) ;
419
349
}
420
350
}
421
351
0 commit comments