@@ -2,8 +2,6 @@ use rustc_hir::{def::DefKind, Body, Item, ItemKind, Node, Path, QPath, TyKind};
2
2
use rustc_span:: def_id:: { DefId , LOCAL_CRATE } ;
3
3
use rustc_span:: { sym, symbol:: kw, ExpnKind , MacroKind } ;
4
4
5
- use smallvec:: { smallvec, SmallVec } ;
6
-
7
5
use crate :: lints:: { NonLocalDefinitionsCargoUpdateNote , NonLocalDefinitionsDiag } ;
8
6
use crate :: { LateContext , LateLintPass , LintContext } ;
9
7
@@ -85,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
85
83
if let Some ( def_id) = oexpn. macro_def_id
86
84
&& let ExpnKind :: Macro ( macro_kind, macro_name) = oexpn. kind
87
85
&& def_id. krate != LOCAL_CRATE
88
- && std :: env :: var_os ( "CARGO" ) . is_some ( )
86
+ && rustc_session :: utils :: was_invoked_from_cargo ( )
89
87
{
90
88
Some ( NonLocalDefinitionsCargoUpdateNote {
91
89
macro_kind : macro_kind. descr ( ) ,
@@ -114,25 +112,25 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
114
112
// is using local items and so we don't lint on it.
115
113
116
114
// We also ignore anon-const in item by including the anon-const
117
- // parent as well; and since it's quite uncommon, we use smallvec
118
- // to avoid unnecessary heap allocations.
119
- let local_parents: SmallVec < [ DefId ; 1 ] > = if parent_def_kind == DefKind :: Const
115
+ // parent as well.
116
+ let parent_parent = if parent_def_kind == DefKind :: Const
120
117
&& parent_opt_item_name == Some ( kw:: Underscore )
121
118
{
122
- smallvec ! [ parent , cx. tcx. parent( parent) ]
119
+ Some ( cx. tcx . parent ( parent) )
123
120
} else {
124
- smallvec ! [ parent ]
121
+ None
125
122
} ;
126
123
127
124
let self_ty_has_local_parent = match impl_. self_ty . kind {
128
125
TyKind :: Path ( QPath :: Resolved ( _, ty_path) ) => {
129
- path_has_local_parent ( ty_path, cx, & * local_parents )
126
+ path_has_local_parent ( ty_path, cx, parent , parent_parent )
130
127
}
131
128
TyKind :: TraitObject ( [ principle_poly_trait_ref, ..] , _, _) => {
132
129
path_has_local_parent (
133
130
principle_poly_trait_ref. trait_ref . path ,
134
131
cx,
135
- & * local_parents,
132
+ parent,
133
+ parent_parent,
136
134
)
137
135
}
138
136
TyKind :: TraitObject ( [ ] , _, _)
@@ -154,7 +152,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
154
152
155
153
let of_trait_has_local_parent = impl_
156
154
. of_trait
157
- . map ( |of_trait| path_has_local_parent ( of_trait. path , cx, & * local_parents ) )
155
+ . map ( |of_trait| path_has_local_parent ( of_trait. path , cx, parent , parent_parent ) )
158
156
. unwrap_or ( false ) ;
159
157
160
158
// If none of them have a local parent (LOGICAL NOR) this means that
@@ -218,6 +216,16 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
218
216
/// std::convert::PartialEq<Foo<Bar>>
219
217
/// ^^^^^^^^^^^^^^^^^^^^^^^
220
218
/// ```
221
- fn path_has_local_parent ( path : & Path < ' _ > , cx : & LateContext < ' _ > , local_parents : & [ DefId ] ) -> bool {
222
- path. res . opt_def_id ( ) . is_some_and ( |did| local_parents. contains ( & cx. tcx . parent ( did) ) )
219
+ fn path_has_local_parent (
220
+ path : & Path < ' _ > ,
221
+ cx : & LateContext < ' _ > ,
222
+ impl_parent : DefId ,
223
+ impl_parent_parent : Option < DefId > ,
224
+ ) -> bool {
225
+ path. res . opt_def_id ( ) . is_some_and ( |did| {
226
+ did. is_local ( ) && {
227
+ let res_parent = cx. tcx . parent ( did) ;
228
+ res_parent == impl_parent || Some ( res_parent) == impl_parent_parent
229
+ }
230
+ } )
223
231
}
0 commit comments