11use 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 } ;
53use rustc_lint:: { LateContext , LateLintPass } ;
6- use rustc_middle:: hir:: map:: Map ;
74use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
85
96use crate :: utils:: span_lint_and_help;
7+ use crate :: utils:: visitors:: LocalUsedVisitor ;
108
119declare_clippy_lint ! {
1210 /// **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 {
5755 then {
5856 let self_param = & body. params[ 0 ] ;
5957 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) {
6759 span_lint_and_help(
6860 cx,
6961 UNUSED_SELF ,
@@ -78,28 +70,3 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
7870 }
7971 }
8072}
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