@@ -65,26 +65,14 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
65
65
return ;
66
66
}
67
67
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
+ }
74
75
} ;
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
- }
88
76
89
77
let cargo_update = || {
90
78
let oexpn = item. span . ctxt ( ) . outer_expn_data ( ) ;
@@ -119,9 +107,21 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
119
107
// If that's the case this means that this impl block declaration
120
108
// is using local items and so we don't lint on it.
121
109
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
+ } ;
125
125
let mut local_parent = {
126
126
let mut local_parent_cache = None ;
127
127
move || {
@@ -132,8 +132,9 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
132
132
let mut extra_local_parent = {
133
133
let mut extra_parent_cache = None ;
134
134
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
+ } )
137
138
}
138
139
} ;
139
140
@@ -184,8 +185,13 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
184
185
// If none of them have a local parent (LOGICAL NOR) this means that
185
186
// this impl definition is a non-local definition and so we lint on it.
186
187
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
+
187
193
let const_anon = if self . body_depth == 1
188
- && let OwnerNode :: Item ( item) = parent_node
194
+ && let OwnerNode :: Item ( item) = parent_node ( )
189
195
&& let ItemKind :: Const ( ty, _, _) = item. kind
190
196
&& let TyKind :: Tup ( & [ ] ) = ty. kind
191
197
{
@@ -200,7 +206,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
200
206
NonLocalDefinitionsDiag :: Impl {
201
207
depth : self . body_depth ,
202
208
body_kind_descr : "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */ ,
203
- body_name : parent_node
209
+ body_name : parent_node ( )
204
210
. ident ( )
205
211
. map ( |s| s. name . to_ident_string ( ) )
206
212
. unwrap_or_else ( || "<unnameable>" . to_string ( ) ) ,
@@ -219,7 +225,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
219
225
NonLocalDefinitionsDiag :: MacroRules {
220
226
depth : self . body_depth ,
221
227
body_kind_descr : "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */ ,
222
- body_name : parent_node
228
+ body_name : parent_node ( )
223
229
. ident ( )
224
230
. map ( |s| s. name . to_ident_string ( ) )
225
231
. unwrap_or_else ( || "<unnameable>" . to_string ( ) ) ,
0 commit comments