@@ -10,7 +10,7 @@ use rustc_hir::FnRetTy::Return;
10
10
use rustc_hir:: {
11
11
BareFnTy , BodyId , FnDecl , GenericArg , GenericBound , GenericParam , GenericParamKind , Generics , Impl , ImplItem ,
12
12
ImplItemKind , Item , ItemKind , LangItem , Lifetime , LifetimeName , ParamName , PolyTraitRef , TraitBoundModifier ,
13
- TraitFn , TraitItem , TraitItemKind , Ty , TyKind , WhereClause , WherePredicate ,
13
+ TraitFn , TraitItem , TraitItemKind , Ty , TyKind , WherePredicate ,
14
14
} ;
15
15
use rustc_lint:: { LateContext , LateLintPass } ;
16
16
use rustc_middle:: hir:: nested_filter as middle_nested_filter;
@@ -85,9 +85,9 @@ declare_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]);
85
85
86
86
impl < ' tcx > LateLintPass < ' tcx > for Lifetimes {
87
87
fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' _ > ) {
88
- if let ItemKind :: Fn ( ref sig, ref generics, id) = item. kind {
88
+ if let ItemKind :: Fn ( ref sig, generics, id) = item. kind {
89
89
check_fn_inner ( cx, sig. decl , Some ( id) , None , generics, item. span , true ) ;
90
- } else if let ItemKind :: Impl ( ref impl_) = item. kind {
90
+ } else if let ItemKind :: Impl ( impl_) = item. kind {
91
91
report_extra_impl_lifetimes ( cx, impl_) ;
92
92
}
93
93
}
@@ -100,7 +100,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes {
100
100
sig. decl ,
101
101
Some ( id) ,
102
102
None ,
103
- & item. generics ,
103
+ item. generics ,
104
104
item. span ,
105
105
report_extra_lifetimes,
106
106
) ;
@@ -113,7 +113,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes {
113
113
TraitFn :: Required ( sig) => ( None , Some ( sig) ) ,
114
114
TraitFn :: Provided ( id) => ( Some ( id) , None ) ,
115
115
} ;
116
- check_fn_inner ( cx, sig. decl , body, trait_sig, & item. generics , item. span , true ) ;
116
+ check_fn_inner ( cx, sig. decl , body, trait_sig, item. generics , item. span , true ) ;
117
117
}
118
118
}
119
119
}
@@ -135,7 +135,7 @@ fn check_fn_inner<'tcx>(
135
135
span : Span ,
136
136
report_extra_lifetimes : bool ,
137
137
) {
138
- if span. from_expansion ( ) || has_where_lifetimes ( cx, & generics. where_clause ) {
138
+ if span. from_expansion ( ) || has_where_lifetimes ( cx, generics) {
139
139
return ;
140
140
}
141
141
@@ -144,28 +144,35 @@ fn check_fn_inner<'tcx>(
144
144
. iter ( )
145
145
. filter ( |param| matches ! ( param. kind, GenericParamKind :: Type { .. } ) ) ;
146
146
for typ in types {
147
- for bound in typ. bounds {
148
- let mut visitor = RefVisitor :: new ( cx) ;
149
- walk_param_bound ( & mut visitor, bound) ;
150
- if visitor. lts . iter ( ) . any ( |lt| matches ! ( lt, RefLt :: Named ( _) ) ) {
151
- return ;
147
+ for pred in generics. bounds_for_param ( cx. tcx . hir ( ) . local_def_id ( typ. hir_id ) ) {
148
+ if pred. in_where_clause {
149
+ // has_where_lifetimes checked that this predicate contains no lifetime.
150
+ continue ;
152
151
}
153
- if let GenericBound :: Trait ( ref trait_ref, _) = * bound {
154
- let params = & trait_ref
155
- . trait_ref
156
- . path
157
- . segments
158
- . last ( )
159
- . expect ( "a path must have at least one segment" )
160
- . args ;
161
- if let Some ( params) = * params {
162
- let lifetimes = params. args . iter ( ) . filter_map ( |arg| match arg {
163
- GenericArg :: Lifetime ( lt) => Some ( lt) ,
164
- _ => None ,
165
- } ) ;
166
- for bound in lifetimes {
167
- if bound. name != LifetimeName :: Static && !bound. is_elided ( ) {
168
- return ;
152
+
153
+ for bound in pred. bounds {
154
+ let mut visitor = RefVisitor :: new ( cx) ;
155
+ walk_param_bound ( & mut visitor, bound) ;
156
+ if visitor. lts . iter ( ) . any ( |lt| matches ! ( lt, RefLt :: Named ( _) ) ) {
157
+ return ;
158
+ }
159
+ if let GenericBound :: Trait ( ref trait_ref, _) = * bound {
160
+ let params = & trait_ref
161
+ . trait_ref
162
+ . path
163
+ . segments
164
+ . last ( )
165
+ . expect ( "a path must have at least one segment" )
166
+ . args ;
167
+ if let Some ( params) = * params {
168
+ let lifetimes = params. args . iter ( ) . filter_map ( |arg| match arg {
169
+ GenericArg :: Lifetime ( lt) => Some ( lt) ,
170
+ _ => None ,
171
+ } ) ;
172
+ for bound in lifetimes {
173
+ if bound. name != LifetimeName :: Static && !bound. is_elided ( ) {
174
+ return ;
175
+ }
169
176
}
170
177
}
171
178
}
@@ -326,9 +333,7 @@ fn allowed_lts_from(named_generics: &[GenericParam<'_>]) -> FxHashSet<RefLt> {
326
333
let mut allowed_lts = FxHashSet :: default ( ) ;
327
334
for par in named_generics. iter ( ) {
328
335
if let GenericParamKind :: Lifetime { .. } = par. kind {
329
- if par. bounds . is_empty ( ) {
330
- allowed_lts. insert ( RefLt :: Named ( par. name . ident ( ) . name ) ) ;
331
- }
336
+ allowed_lts. insert ( RefLt :: Named ( par. name . ident ( ) . name ) ) ;
332
337
}
333
338
}
334
339
allowed_lts. insert ( RefLt :: Unnamed ) ;
@@ -449,8 +454,8 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
449
454
450
455
/// Are any lifetimes mentioned in the `where` clause? If so, we don't try to
451
456
/// reason about elision.
452
- fn has_where_lifetimes < ' tcx > ( cx : & LateContext < ' tcx > , where_clause : & ' tcx WhereClause < ' _ > ) -> bool {
453
- for predicate in where_clause . predicates {
457
+ fn has_where_lifetimes < ' tcx > ( cx : & LateContext < ' tcx > , generics : & ' tcx Generics < ' _ > ) -> bool {
458
+ for predicate in generics . predicates {
454
459
match * predicate {
455
460
WherePredicate :: RegionPredicate ( ..) => return true ,
456
461
WherePredicate :: BoundPredicate ( ref pred) => {
@@ -565,7 +570,7 @@ fn report_extra_impl_lifetimes<'tcx>(cx: &LateContext<'tcx>, impl_: &'tcx Impl<'
565
570
. collect ( ) ;
566
571
let mut checker = LifetimeChecker :: < middle_nested_filter:: All > :: new ( cx, hs) ;
567
572
568
- walk_generics ( & mut checker, & impl_. generics ) ;
573
+ walk_generics ( & mut checker, impl_. generics ) ;
569
574
if let Some ( ref trait_ref) = impl_. of_trait {
570
575
walk_trait_ref ( & mut checker, trait_ref) ;
571
576
}
0 commit comments