@@ -2,7 +2,7 @@ use reexport::*;
2
2
use rustc:: lint:: * ;
3
3
use rustc:: hir:: def:: Def ;
4
4
use rustc:: hir:: * ;
5
- use rustc:: hir:: intravisit:: { walk_fn_decl , walk_generics , walk_ty , walk_ty_param_bound , NestedVisitorMap , Visitor } ;
5
+ use rustc:: hir:: intravisit:: * ;
6
6
use std:: collections:: { HashMap , HashSet } ;
7
7
use syntax:: codemap:: Span ;
8
8
use utils:: { in_external_macro, last_path_segment, span_lint} ;
@@ -101,7 +101,7 @@ fn check_fn_inner<'a, 'tcx>(
101
101
}
102
102
103
103
let mut bounds_lts = Vec :: new ( ) ;
104
- for typ in & generics. ty_params {
104
+ for typ in generics. ty_params ( ) {
105
105
for bound in & typ. bounds {
106
106
if let TraitTyParamBound ( ref trait_ref, _) = * bound {
107
107
let params = & trait_ref
@@ -122,7 +122,7 @@ fn check_fn_inner<'a, 'tcx>(
122
122
}
123
123
}
124
124
}
125
- if could_use_elision ( cx, decl, body, & generics. lifetimes , bounds_lts) {
125
+ if could_use_elision ( cx, decl, body, & generics. params , bounds_lts) {
126
126
span_lint (
127
127
cx,
128
128
NEEDLESS_LIFETIMES ,
@@ -137,7 +137,7 @@ fn could_use_elision<'a, 'tcx: 'a>(
137
137
cx : & LateContext < ' a , ' tcx > ,
138
138
func : & ' tcx FnDecl ,
139
139
body : Option < BodyId > ,
140
- named_lts : & ' tcx [ LifetimeDef ] ,
140
+ named_generics : & ' tcx [ GenericParam ] ,
141
141
bounds_lts : Vec < & ' tcx Lifetime > ,
142
142
) -> bool {
143
143
// There are two scenarios where elision works:
@@ -147,7 +147,7 @@ fn could_use_elision<'a, 'tcx: 'a>(
147
147
// level of the current item.
148
148
149
149
// check named LTs
150
- let allowed_lts = allowed_lts_from ( named_lts ) ;
150
+ let allowed_lts = allowed_lts_from ( named_generics ) ;
151
151
152
152
// these will collect all the lifetimes for references in arg/return types
153
153
let mut input_visitor = RefVisitor :: new ( cx) ;
@@ -222,11 +222,13 @@ fn could_use_elision<'a, 'tcx: 'a>(
222
222
}
223
223
}
224
224
225
- fn allowed_lts_from ( named_lts : & [ LifetimeDef ] ) -> HashSet < RefLt > {
225
+ fn allowed_lts_from ( named_generics : & [ GenericParam ] ) -> HashSet < RefLt > {
226
226
let mut allowed_lts = HashSet :: new ( ) ;
227
- for lt in named_lts {
228
- if lt. bounds . is_empty ( ) {
229
- allowed_lts. insert ( RefLt :: Named ( lt. lifetime . name . name ( ) ) ) ;
227
+ for par in named_generics. iter ( ) {
228
+ if let GenericParam :: Lifetime ( ref lt) = * par {
229
+ if lt. bounds . is_empty ( ) {
230
+ allowed_lts. insert ( RefLt :: Named ( lt. lifetime . name . name ( ) ) ) ;
231
+ }
230
232
}
231
233
}
232
234
allowed_lts. insert ( RefLt :: Unnamed ) ;
@@ -370,7 +372,7 @@ fn has_where_lifetimes<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, where_clause: &
370
372
return true ;
371
373
}
372
374
// if the bounds define new lifetimes, they are fine to occur
373
- let allowed_lts = allowed_lts_from ( & pred. bound_lifetimes ) ;
375
+ let allowed_lts = allowed_lts_from ( & pred. bound_generic_params ) ;
374
376
// now walk the bounds
375
377
for bound in pred. bounds . iter ( ) {
376
378
walk_ty_param_bound ( & mut visitor, bound) ;
@@ -408,12 +410,15 @@ impl<'tcx> Visitor<'tcx> for LifetimeChecker {
408
410
self . map . remove ( & lifetime. name . name ( ) ) ;
409
411
}
410
412
411
- fn visit_lifetime_def ( & mut self , _ : & ' tcx LifetimeDef ) {
413
+ fn visit_generic_param ( & mut self , param : & ' tcx GenericParam ) {
412
414
// don't actually visit `<'a>` or `<'a: 'b>`
413
415
// we've already visited the `'a` declarations and
414
416
// don't want to spuriously remove them
415
417
// `'b` in `'a: 'b` is useless unless used elsewhere in
416
418
// a non-lifetime bound
419
+ if param. is_type_param ( ) {
420
+ walk_generic_param ( self , param)
421
+ }
417
422
}
418
423
fn nested_visit_map < ' this > ( & ' this mut self ) -> NestedVisitorMap < ' this , ' tcx > {
419
424
NestedVisitorMap :: None
@@ -422,8 +427,7 @@ impl<'tcx> Visitor<'tcx> for LifetimeChecker {
422
427
423
428
fn report_extra_lifetimes < ' a , ' tcx : ' a > ( cx : & LateContext < ' a , ' tcx > , func : & ' tcx FnDecl , generics : & ' tcx Generics ) {
424
429
let hs = generics
425
- . lifetimes
426
- . iter ( )
430
+ . lifetimes ( )
427
431
. map ( |lt| ( lt. lifetime . name . name ( ) , lt. lifetime . span ) )
428
432
. collect ( ) ;
429
433
let mut checker = LifetimeChecker { map : hs } ;
0 commit comments