@@ -2,7 +2,7 @@ use if_chain::if_chain;
2
2
use rustc:: hir:: map:: Map ;
3
3
use rustc_hir:: def:: Res ;
4
4
use rustc_hir:: intravisit:: { walk_path, NestedVisitorMap , Visitor } ;
5
- use rustc_hir:: { AssocItemKind , HirId , ImplItemKind , ImplItemRef , Item , ItemKind , Path } ;
5
+ use rustc_hir:: { AssocItemKind , HirId , ImplItem , ImplItemKind , ImplItemRef , ItemKind , Path } ;
6
6
use rustc_lint:: { LateContext , LateLintPass } ;
7
7
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
8
8
@@ -40,10 +40,12 @@ declare_clippy_lint! {
40
40
declare_lint_pass ! ( UnusedSelf => [ UNUSED_SELF ] ) ;
41
41
42
42
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UnusedSelf {
43
- fn check_item ( & mut self , cx : & LateContext < ' a , ' tcx > , item : & Item < ' _ > ) {
44
- if item . span . from_expansion ( ) {
43
+ fn check_impl_item ( & mut self , cx : & LateContext < ' a , ' tcx > , impl_item : & ImplItem < ' _ > ) {
44
+ if impl_item . span . from_expansion ( ) {
45
45
return ;
46
46
}
47
+ let parent = cx. tcx . hir ( ) . get_parent_item ( impl_item. hir_id ) ;
48
+ let item = cx. tcx . hir ( ) . expect_item ( parent) ;
47
49
if let ItemKind :: Impl {
48
50
of_trait : None ,
49
51
items : impl_item_refs,
@@ -56,10 +58,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
56
58
kind: AssocItemKind :: Method { has_self: true } ,
57
59
..
58
60
} = impl_item_ref;
59
- let impl_item = cx. tcx. hir( ) . impl_item( impl_item_ref. id) ;
60
61
if let ImplItemKind :: Method ( _, body_id) = & impl_item. kind;
62
+ let body = cx. tcx. hir( ) . body( * body_id) ;
63
+ if !body. params. is_empty( ) ;
61
64
then {
62
- let body = cx. tcx. hir( ) . body( * body_id) ;
63
65
let self_param = & body. params[ 0 ] ;
64
66
let self_hir_id = self_param. pat. hir_id;
65
67
let mut visitor = UnusedSelfVisitor {
@@ -75,7 +77,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
75
77
self_param. span,
76
78
"unused `self` argument" ,
77
79
"consider refactoring to a associated function" ,
78
- )
80
+ ) ;
81
+ return ;
79
82
}
80
83
}
81
84
}
0 commit comments