@@ -2,7 +2,7 @@ use if_chain::if_chain;
22use rustc:: hir:: map:: Map ;
33use rustc_hir:: def:: Res ;
44use rustc_hir:: intravisit:: { walk_path, NestedVisitorMap , Visitor } ;
5- use rustc_hir:: { AssocItemKind , HirId , ImplItem , ImplItemKind , ImplItemRef , ItemKind , Path } ;
5+ use rustc_hir:: { HirId , ImplItem , ImplItemKind , ItemKind , Path } ;
66use rustc_lint:: { LateContext , LateLintPass } ;
77use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
88
@@ -45,45 +45,36 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
4545 return ;
4646 }
4747 let parent = cx. tcx . hir ( ) . get_parent_item ( impl_item. hir_id ) ;
48- let item = cx. tcx . hir ( ) . expect_item ( parent) ;
49- if let ItemKind :: Impl {
50- of_trait : None ,
51- items : impl_item_refs,
52- ..
53- } = item. kind
54- {
55- for impl_item_ref in impl_item_refs {
56- if_chain ! {
57- if let ImplItemRef {
58- kind: AssocItemKind :: Method { has_self: true } ,
59- ..
60- } = impl_item_ref;
61- if let ImplItemKind :: Fn ( _, body_id) = & impl_item. kind;
62- let body = cx. tcx. hir( ) . body( * body_id) ;
63- if !body. params. is_empty( ) ;
64- then {
65- let self_param = & body. params[ 0 ] ;
66- let self_hir_id = self_param. pat. hir_id;
67- let mut visitor = UnusedSelfVisitor {
68- cx,
69- uses_self: false ,
70- self_hir_id: & self_hir_id,
71- } ;
72- visitor. visit_body( body) ;
73- if !visitor. uses_self {
74- span_lint_and_help(
75- cx,
76- UNUSED_SELF ,
77- self_param. span,
78- "unused `self` argument" ,
79- "consider refactoring to a associated function" ,
80- ) ;
81- return ;
82- }
83- }
48+ let parent_item = cx. tcx . hir ( ) . expect_item ( parent) ;
49+ let def_id = cx. tcx . hir ( ) . local_def_id ( impl_item. hir_id ) ;
50+ let assoc_item = cx. tcx . associated_item ( def_id) ;
51+ if_chain ! {
52+ if let ItemKind :: Impl { of_trait: None , .. } = parent_item. kind;
53+ if assoc_item. method_has_self_argument;
54+ if let ImplItemKind :: Fn ( .., body_id) = & impl_item. kind;
55+ let body = cx. tcx. hir( ) . body( * body_id) ;
56+ if !body. params. is_empty( ) ;
57+ then {
58+ let self_param = & body. params[ 0 ] ;
59+ let self_hir_id = self_param. pat. hir_id;
60+ let mut visitor = UnusedSelfVisitor {
61+ cx,
62+ uses_self: false ,
63+ self_hir_id: & self_hir_id,
64+ } ;
65+ visitor. visit_body( body) ;
66+ if !visitor. uses_self {
67+ span_lint_and_help(
68+ cx,
69+ UNUSED_SELF ,
70+ self_param. span,
71+ "unused `self` argument" ,
72+ "consider refactoring to a associated function" ,
73+ ) ;
74+ return ;
8475 }
8576 }
86- } ;
77+ }
8778 }
8879}
8980
0 commit comments