@@ -65,26 +65,14 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
6565 return ;
6666 }
6767
68- if !matches ! ( item. kind, ItemKind :: Impl ( _) | ItemKind :: Macro ( _, _) ) {
69- return ;
70- }
71-
72- let Some ( ( _, parent_node) ) = cx. tcx . hir ( ) . parent_owner_iter ( item. hir_id ( ) ) . next ( ) else {
73- return ;
68+ let mut parent_node = {
69+ let mut parent_node_cache = None ;
70+ move || {
71+ * parent_node_cache. get_or_insert_with ( || {
72+ cx. tcx . hir ( ) . parent_owner_iter ( item. hir_id ( ) ) . next ( ) . unwrap ( ) . 1
73+ } )
74+ }
7475 } ;
75- let parent_is_anon_const = matches ! (
76- parent_node,
77- OwnerNode :: Item ( Item {
78- ident: Ident { name: kw:: Underscore , .. } ,
79- kind: ItemKind :: Const ( ..) ,
80- ..
81- } )
82- ) ;
83-
84- // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module.
85- if self . body_depth == 1 && parent_is_anon_const {
86- return ;
87- }
8876
8977 let cargo_update = || {
9078 let oexpn = item. span . ctxt ( ) . outer_expn_data ( ) ;
@@ -119,9 +107,21 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
119107 // If that's the case this means that this impl block declaration
120108 // is using local items and so we don't lint on it.
121109
122- // We also ignore anon-const in item by including the anon-const
123- // parent as well; and since it's quite uncommon, we use smallvec
124- // to avoid unnecessary heap allocations.
110+ let mut parent_node_is_anon_const = {
111+ let mut parent_node_is_anon_const = None ;
112+ move || {
113+ * parent_node_is_anon_const. get_or_insert_with ( || {
114+ matches ! (
115+ parent_node( ) ,
116+ OwnerNode :: Item ( Item {
117+ ident: Ident { name: kw:: Underscore , .. } ,
118+ kind: ItemKind :: Const ( ..) ,
119+ ..
120+ } )
121+ )
122+ } )
123+ }
124+ } ;
125125 let mut local_parent = {
126126 let mut local_parent_cache = None ;
127127 move || {
@@ -132,8 +132,9 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
132132 let mut extra_local_parent = {
133133 let mut extra_parent_cache = None ;
134134 move |did| {
135- * extra_parent_cache
136- . get_or_insert_with ( || parent_is_anon_const. then ( || cx. tcx . parent ( did) ) )
135+ * extra_parent_cache. get_or_insert_with ( || {
136+ parent_node_is_anon_const ( ) . then ( || cx. tcx . parent ( did) )
137+ } )
137138 }
138139 } ;
139140
@@ -184,8 +185,13 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
184185 // If none of them have a local parent (LOGICAL NOR) this means that
185186 // this impl definition is a non-local definition and so we lint on it.
186187 if !( self_ty_has_local_parent || of_trait_has_local_parent) {
188+ // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module.
189+ if parent_node_is_anon_const ( ) && self . body_depth == 1 {
190+ return ;
191+ }
192+
187193 let const_anon = if self . body_depth == 1
188- && let OwnerNode :: Item ( item) = parent_node
194+ && let OwnerNode :: Item ( item) = parent_node ( )
189195 && let ItemKind :: Const ( ty, _, _) = item. kind
190196 && let TyKind :: Tup ( & [ ] ) = ty. kind
191197 {
@@ -200,7 +206,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
200206 NonLocalDefinitionsDiag :: Impl {
201207 depth : self . body_depth ,
202208 body_kind_descr : "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */ ,
203- body_name : parent_node
209+ body_name : parent_node ( )
204210 . ident ( )
205211 . map ( |s| s. name . to_ident_string ( ) )
206212 . unwrap_or_else ( || "<unnameable>" . to_string ( ) ) ,
@@ -219,7 +225,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
219225 NonLocalDefinitionsDiag :: MacroRules {
220226 depth : self . body_depth ,
221227 body_kind_descr : "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */ ,
222- body_name : parent_node
228+ body_name : parent_node ( )
223229 . ident ( )
224230 . map ( |s| s. name . to_ident_string ( ) )
225231 . unwrap_or_else ( || "<unnameable>" . to_string ( ) ) ,
0 commit comments