@@ -134,18 +134,10 @@ 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
- euv:: ExprUseVisitor :: new (
140
- & mut ctx,
141
- cx. tcx ,
142
- fn_def_id,
143
- cx. param_env ,
144
- region_scope_tree,
145
- cx. tables ,
146
- None ,
147
- )
148
- . consume_body ( body) ;
139
+ euv:: ExprUseVisitor :: new ( & mut ctx, cx. tcx , fn_def_id, cx. param_env , region_scope_tree, cx. tables )
140
+ . consume_body ( body) ;
149
141
ctx
150
142
} ;
151
143
@@ -325,115 +317,34 @@ fn requires_exact_signature(attrs: &[Attribute]) -> bool {
325
317
} )
326
318
}
327
319
328
- struct MovedVariablesCtxt < ' a , ' tcx > {
329
- cx : & ' a LateContext < ' a , ' tcx > ,
320
+ # [ derive ( Default ) ]
321
+ struct MovedVariablesCtxt {
330
322
moved_vars : FxHashSet < HirId > ,
331
323
/// Spans which need to be prefixed with `*` for dereferencing the
332
324
/// suggested additional reference.
333
325
spans_need_deref : FxHashMap < HirId , FxHashSet < Span > > ,
334
326
}
335
327
336
- impl < ' a , ' tcx > MovedVariablesCtxt < ' a , ' tcx > {
337
- fn new ( cx : & ' a LateContext < ' a , ' tcx > ) -> Self {
338
- Self {
339
- cx,
340
- moved_vars : FxHashSet :: default ( ) ,
341
- spans_need_deref : FxHashMap :: default ( ) ,
342
- }
343
- }
344
-
345
- fn move_common ( & mut self , _consume_id : HirId , _span : Span , cmt : & mc:: cmt_ < ' tcx > ) {
328
+ impl MovedVariablesCtxt {
329
+ fn move_common ( & mut self , cmt : & mc:: cmt_ < ' _ > ) {
346
330
let cmt = unwrap_downcast_or_interior ( cmt) ;
347
331
348
332
if let mc:: Categorization :: Local ( vid) = cmt. cat {
349
333
self . moved_vars . insert ( vid) ;
350
334
}
351
335
}
352
-
353
- fn non_moving_pat ( & mut self , matched_pat : & Pat , cmt : & mc:: cmt_ < ' tcx > ) {
354
- let cmt = unwrap_downcast_or_interior ( cmt) ;
355
-
356
- if let mc:: Categorization :: Local ( vid) = cmt. cat {
357
- let mut id = matched_pat. hir_id ;
358
- loop {
359
- let parent = self . cx . tcx . hir ( ) . get_parent_node ( id) ;
360
- if id == parent {
361
- // no parent
362
- return ;
363
- }
364
- id = parent;
365
-
366
- if let Some ( node) = self . cx . tcx . hir ( ) . find ( id) {
367
- match node {
368
- Node :: Expr ( e) => {
369
- // `match` and `if let`
370
- if let ExprKind :: Match ( ref c, ..) = e. kind {
371
- self . spans_need_deref
372
- . entry ( vid)
373
- . or_insert_with ( FxHashSet :: default)
374
- . insert ( c. span ) ;
375
- }
376
- } ,
377
-
378
- Node :: Stmt ( s) => {
379
- // `let <pat> = x;`
380
- if_chain ! {
381
- if let StmtKind :: Local ( ref local) = s. kind;
382
- then {
383
- self . spans_need_deref
384
- . entry( vid)
385
- . or_insert_with( FxHashSet :: default )
386
- . insert( local. init
387
- . as_ref( )
388
- . map( |e| e. span)
389
- . expect( "`let` stmt without init aren't caught by match_pat" ) ) ;
390
- }
391
- }
392
- } ,
393
-
394
- _ => { } ,
395
- }
396
- }
397
- }
398
- }
399
- }
400
336
}
401
337
402
- impl < ' a , ' tcx > euv:: Delegate < ' tcx > for MovedVariablesCtxt < ' a , ' tcx > {
403
- fn consume ( & mut self , consume_id : HirId , consume_span : Span , cmt : & mc:: cmt_ < ' tcx > , mode : euv:: ConsumeMode ) {
404
- if let euv:: ConsumeMode :: Move ( _) = mode {
405
- self . move_common ( consume_id, consume_span, cmt) ;
406
- }
407
- }
408
-
409
- fn matched_pat ( & mut self , matched_pat : & Pat , cmt : & mc:: cmt_ < ' tcx > , mode : euv:: MatchMode ) {
410
- if let euv:: MatchMode :: MovingMatch = mode {
411
- self . move_common ( matched_pat. hir_id , matched_pat. span , cmt) ;
412
- } else {
413
- self . non_moving_pat ( matched_pat, cmt) ;
414
- }
415
- }
416
-
417
- fn consume_pat ( & mut self , consume_pat : & Pat , cmt : & mc:: cmt_ < ' tcx > , mode : euv:: ConsumeMode ) {
418
- if let euv:: ConsumeMode :: Move ( _) = mode {
419
- self . move_common ( consume_pat. hir_id , consume_pat. span , cmt) ;
338
+ impl < ' tcx > euv:: Delegate < ' tcx > for MovedVariablesCtxt {
339
+ fn consume ( & mut self , cmt : & mc:: cmt_ < ' tcx > , mode : euv:: ConsumeMode ) {
340
+ if let euv:: ConsumeMode :: Move = mode {
341
+ self . move_common ( cmt) ;
420
342
}
421
343
}
422
344
423
- fn borrow (
424
- & mut self ,
425
- _: HirId ,
426
- _: Span ,
427
- _: & mc:: cmt_ < ' tcx > ,
428
- _: ty:: Region < ' _ > ,
429
- _: ty:: BorrowKind ,
430
- _: euv:: LoanCause ,
431
- ) {
432
- }
433
-
434
- fn mutate ( & mut self , _: HirId , _: Span , _: & mc:: cmt_ < ' tcx > , _: euv:: MutateMode ) { }
345
+ fn borrow ( & mut self , _: & mc:: cmt_ < ' tcx > , _: ty:: BorrowKind ) { }
435
346
436
- fn decl_without_init ( & mut self , _: HirId , _ : Span ) { }
347
+ fn mutate ( & mut self , _: & mc :: cmt_ < ' tcx > ) { }
437
348
}
438
349
439
350
fn unwrap_downcast_or_interior < ' a , ' tcx > ( mut cmt : & ' a mc:: cmt_ < ' tcx > ) -> mc:: cmt_ < ' tcx > {
0 commit comments