1
1
use if_chain:: if_chain;
2
- use rustc_hir:: def:: Res ;
3
- use rustc_hir:: intravisit:: { walk_path, NestedVisitorMap , Visitor } ;
4
- use rustc_hir:: { HirId , Impl , ImplItem , ImplItemKind , ItemKind , Path } ;
2
+ use rustc_hir:: { Impl , ImplItem , ImplItemKind , ItemKind } ;
5
3
use rustc_lint:: { LateContext , LateLintPass } ;
6
- use rustc_middle:: hir:: map:: Map ;
7
4
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
8
5
9
6
use crate :: utils:: span_lint_and_help;
7
+ use crate :: utils:: visitors:: LocalUsedVisitor ;
10
8
11
9
declare_clippy_lint ! {
12
10
/// **What it does:** Checks methods that contain a `self` argument but don't use it
@@ -57,13 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
57
55
then {
58
56
let self_param = & body. params[ 0 ] ;
59
57
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 {
58
+ if !LocalUsedVisitor :: new( cx, self_hir_id) . check_body( body) {
67
59
span_lint_and_help(
68
60
cx,
69
61
UNUSED_SELF ,
@@ -78,28 +70,3 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
78
70
}
79
71
}
80
72
}
81
-
82
- struct UnusedSelfVisitor < ' a , ' tcx > {
83
- cx : & ' a LateContext < ' tcx > ,
84
- uses_self : bool ,
85
- self_hir_id : & ' a HirId ,
86
- }
87
-
88
- impl < ' a , ' tcx > Visitor < ' tcx > for UnusedSelfVisitor < ' a , ' tcx > {
89
- type Map = Map < ' tcx > ;
90
-
91
- fn visit_path ( & mut self , path : & ' tcx Path < ' _ > , _id : HirId ) {
92
- if self . uses_self {
93
- // This function already uses `self`
94
- return ;
95
- }
96
- if let Res :: Local ( hir_id) = & path. res {
97
- self . uses_self = self . self_hir_id == hir_id
98
- }
99
- walk_path ( self , path) ;
100
- }
101
-
102
- fn nested_visit_map ( & mut self ) -> NestedVisitorMap < Self :: Map > {
103
- NestedVisitorMap :: OnlyBodies ( self . cx . tcx . hir ( ) )
104
- }
105
- }
0 commit comments