@@ -4,7 +4,7 @@ use rustc_hir as hir;
4
4
use rustc_hir:: def:: DefKind ;
5
5
use rustc_hir:: intravisit:: { walk_expr, walk_impl_item, walk_ty, NestedVisitorMap , Visitor } ;
6
6
use rustc_hir:: {
7
- def, Expr , ExprKind , FnDecl , FnRetTy , FnSig , GenericArg , ImplItem , ImplItemKind , Item , ItemKind , Node , Path , QPath ,
7
+ def, Expr , ExprKind , FnDecl , FnRetTy , FnSig , GenericArg , ImplItem , ImplItemKind , ItemKind , Node , Path , QPath ,
8
8
TyKind ,
9
9
} ;
10
10
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
@@ -101,7 +101,6 @@ fn truncate_last_segment<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, sp: Span) -> Span
101
101
102
102
struct ImplVisitor < ' a , ' tcx > {
103
103
cx : & ' a LateContext < ' a , ' tcx > ,
104
- item : & ' tcx Item < ' tcx > ,
105
104
self_ty : Ty < ' tcx > ,
106
105
}
107
106
@@ -238,58 +237,52 @@ impl<'a, 'tcx> Visitor<'tcx> for ImplVisitor<'a, 'tcx> {
238
237
}
239
238
walk_expr ( self , expr) ;
240
239
}
241
-
242
- fn visit_impl_item ( & mut self , ii : & ' tcx ImplItem < ' tcx > ) {
243
- let tcx = self . cx . tcx ;
244
- let impl_def_id = tcx. hir ( ) . local_def_id ( self . item . hir_id ) ;
245
- let impl_trait_ref = tcx. impl_trait_ref ( impl_def_id) ;
246
- if_chain ! {
247
- if let Some ( impl_trait_ref) = impl_trait_ref;
248
- if let ImplItemKind :: Fn ( FnSig { decl: impl_decl, .. } , impl_body_id) = & ii. kind;
249
- then {
250
- self . check_trait_method_impl_decl( ii, impl_decl, impl_trait_ref) ;
251
- let body = tcx. hir( ) . body( * impl_body_id) ;
252
- self . visit_body( body) ;
253
- } else {
254
- walk_impl_item( self , ii)
255
- }
256
- }
257
- }
258
240
}
259
241
260
242
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UseSelf {
261
- fn check_item ( & mut self , cx : & LateContext < ' a , ' tcx > , item : & ' tcx Item < ' _ > ) {
262
- if in_external_macro ( cx. sess ( ) , item . span ) {
243
+ fn check_impl_item ( & mut self , cx : & LateContext < ' a , ' tcx > , impl_item : & ' tcx ImplItem < ' _ > ) {
244
+ if in_external_macro ( cx. sess ( ) , impl_item . span ) {
263
245
return ;
264
246
}
247
+
248
+ let parent_id = cx. tcx . hir ( ) . get_parent_item ( impl_item. hir_id ) ;
249
+ let imp = cx. tcx . hir ( ) . expect_item ( parent_id) ;
250
+
265
251
if_chain ! {
266
- if let ItemKind :: Impl { self_ty: ref item_type , items : refs , .. } = item . kind;
267
- if let TyKind :: Path ( QPath :: Resolved ( _, ref item_path) ) = item_type . kind;
252
+ if let ItemKind :: Impl { self_ty: hir_self_ty , .. } = imp . kind;
253
+ if let TyKind :: Path ( QPath :: Resolved ( _, ref item_path) ) = hir_self_ty . kind;
268
254
then {
269
255
let parameters = & item_path. segments. last( ) . expect( SEGMENTS_MSG ) . args;
270
256
let should_check = if let Some ( ref params) = * parameters {
271
- !params. parenthesized && !params. args. iter( ) . any( |arg| match arg {
272
- GenericArg :: Lifetime ( _) => true ,
273
- _ => false ,
274
- } )
257
+ !params. parenthesized
258
+ && !params. args. iter( ) . any( |arg| match arg {
259
+ GenericArg :: Lifetime ( _) => true ,
260
+ _ => false ,
261
+ } )
275
262
} else {
276
263
true
277
264
} ;
278
265
279
266
// TODO: don't short-circuit upon lifetime parameters
280
267
if should_check {
281
- let self_ty= hir_ty_to_ty( cx. tcx, item_type) ;
282
- let visitor = & mut ImplVisitor {
283
- cx,
284
- item,
285
- self_ty,
286
- } ;
268
+ let self_ty = hir_ty_to_ty( cx. tcx, hir_self_ty) ;
269
+ let visitor = & mut ImplVisitor { cx, self_ty } ;
287
270
288
- for impl_item_ref in refs {
289
- let impl_item = cx. tcx. hir( ) . impl_item( impl_item_ref. id) ;
290
- visitor. visit_impl_item( impl_item) ;
271
+ let tcx = cx. tcx;
272
+ let impl_def_id = tcx. hir( ) . local_def_id( imp. hir_id) ;
273
+ let impl_trait_ref = tcx. impl_trait_ref( impl_def_id) ;
274
+ if_chain! {
275
+ if let Some ( impl_trait_ref) = impl_trait_ref;
276
+ if let ImplItemKind :: Fn ( FnSig { decl: impl_decl, .. } , impl_body_id) = & impl_item. kind;
277
+ then {
278
+ visitor. check_trait_method_impl_decl( impl_item, impl_decl, impl_trait_ref) ;
279
+ let body = tcx. hir( ) . body( * impl_body_id) ;
280
+ visitor. visit_body( body) ;
281
+ } else {
282
+ walk_impl_item( visitor, impl_item)
283
+ }
291
284
}
292
- }
285
+ }
293
286
}
294
287
}
295
288
}
0 commit comments