@@ -87,59 +87,42 @@ const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
8787
8888impl < ' tcx > LateLintPass < ' tcx > for UseSelf {
8989 fn check_item ( & mut self , cx : & LateContext < ' _ > , item : & Item < ' _ > ) {
90+ if !is_item_interesting ( item) {
91+ // This does two things:
92+ // 1) Reduce needless churn on `self.stack`
93+ // 2) Don't push `StackItem::NoCheck` when entering `ItemKind::OpaqueTy`,
94+ // in order to lint `foo() -> impl <..>`
95+ return ;
96+ }
9097 // We push the self types of `impl`s on a stack here. Only the top type on the stack is
9198 // relevant for linting, since this is the self type of the `impl` we're currently in. To
9299 // avoid linting on nested items, we push `StackItem::NoCheck` on the stack to signal, that
93100 // we're in an `impl` or nested item, that we don't want to lint
94- //
95- // NB: If you push something on the stack in this method, remember to also pop it in the
96- // `check_item_post` method.
97- match & item. kind {
98- ItemKind :: Impl ( Impl {
99- self_ty : hir_self_ty,
100- of_trait,
101- ..
102- } ) => {
103- let should_check = if let TyKind :: Path ( QPath :: Resolved ( _, item_path) ) = hir_self_ty. kind {
104- let parameters = & item_path. segments . last ( ) . expect ( SEGMENTS_MSG ) . args ;
105- parameters. as_ref ( ) . map_or ( true , |params| {
106- !params. parenthesized && !params. args . iter ( ) . any ( |arg| matches ! ( arg, GenericArg :: Lifetime ( _) ) )
107- } )
108- } else {
109- false
110- } ;
101+ let stack_item = if_chain ! {
102+ if let ItemKind :: Impl ( Impl { self_ty, ref of_trait, .. } ) = item. kind;
103+ if let TyKind :: Path ( QPath :: Resolved ( _, item_path) ) = self_ty. kind;
104+ let parameters = & item_path. segments. last( ) . expect( SEGMENTS_MSG ) . args;
105+ if parameters. as_ref( ) . map_or( true , |params| {
106+ !params. parenthesized && !params. args. iter( ) . any( |arg| matches!( arg, GenericArg :: Lifetime ( _) ) )
107+ } ) ;
108+ then {
111109 let impl_trait_ref_def_id = of_trait. as_ref( ) . map( |_| cx. tcx. hir( ) . local_def_id( item. hir_id( ) ) ) ;
112- if should_check {
113- self . stack . push ( StackItem :: Check {
114- hir_id : hir_self_ty. hir_id ,
115- impl_trait_ref_def_id,
116- types_to_lint : Vec :: new ( ) ,
117- types_to_skip : Vec :: new ( ) ,
118- } ) ;
119- } else {
120- self . stack . push ( StackItem :: NoCheck ) ;
110+ StackItem :: Check {
111+ hir_id: self_ty. hir_id,
112+ impl_trait_ref_def_id,
113+ types_to_lint: Vec :: new( ) ,
114+ types_to_skip: Vec :: new( ) ,
121115 }
122- } ,
123- ItemKind :: Static ( ..)
124- | ItemKind :: Const ( ..)
125- | ItemKind :: Fn ( ..)
126- | ItemKind :: Enum ( ..)
127- | ItemKind :: Struct ( ..)
128- | ItemKind :: Union ( ..)
129- | ItemKind :: Trait ( ..) => {
130- self . stack . push ( StackItem :: NoCheck ) ;
131- } ,
132- _ => ( ) ,
133- }
116+ } else {
117+ StackItem :: NoCheck
118+ }
119+ } ;
120+ self . stack . push ( stack_item) ;
134121 }
135122
136123 fn check_item_post ( & mut self , _: & LateContext < ' _ > , item : & Item < ' _ > ) {
137- use ItemKind :: { Const , Enum , Fn , Impl , Static , Struct , Trait , Union } ;
138- match item. kind {
139- Impl { .. } | Static ( ..) | Const ( ..) | Fn ( ..) | Enum ( ..) | Struct ( ..) | Union ( ..) | Trait ( ..) => {
140- self . stack . pop ( ) ;
141- } ,
142- _ => ( ) ,
124+ if is_item_interesting ( item) {
125+ self . stack . pop ( ) ;
143126 }
144127 }
145128
@@ -359,6 +342,14 @@ fn lint_path_to_variant(cx: &LateContext<'_>, path: &Path<'_>) {
359342 }
360343}
361344
345+ fn is_item_interesting ( item : & Item < ' _ > ) -> bool {
346+ use rustc_hir:: ItemKind :: { Const , Enum , Fn , Impl , Static , Struct , Trait , Union } ;
347+ matches ! (
348+ item. kind,
349+ Impl { .. } | Static ( ..) | Const ( ..) | Fn ( ..) | Enum ( ..) | Struct ( ..) | Union ( ..) | Trait ( ..)
350+ )
351+ }
352+
362353fn ty_from_hir_id < ' tcx > ( cx : & LateContext < ' tcx > , hir_id : HirId ) -> Ty < ' tcx > {
363354 if let Some ( Node :: Ty ( hir_ty) ) = cx. tcx . hir ( ) . find ( hir_id) {
364355 hir_ty_to_ty ( cx. tcx , hir_ty)
0 commit comments