@@ -2,8 +2,6 @@ use rustc_hir::{def::DefKind, Body, Item, ItemKind, Node, Path, QPath, TyKind};
22use rustc_span:: def_id:: { DefId , LOCAL_CRATE } ;
33use rustc_span:: { sym, symbol:: kw, ExpnKind , MacroKind } ;
44
5- use smallvec:: { smallvec, SmallVec } ;
6-
75use crate :: lints:: { NonLocalDefinitionsCargoUpdateNote , NonLocalDefinitionsDiag } ;
86use crate :: { LateContext , LateLintPass , LintContext } ;
97
@@ -115,23 +113,20 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
115113 // We also ignore anon-const in item by including the anon-const
116114 // parent as well; and since it's quite uncommon, we use smallvec
117115 // to avoid unnecessary heap allocations.
118- let local_parents: SmallVec < [ DefId ; 1 ] > = if parent_def_kind == DefKind :: Const
119- && parent_opt_item_name == Some ( kw:: Underscore )
120- {
121- smallvec ! [ parent, cx. tcx. parent( parent) ]
122- } else {
123- smallvec ! [ parent]
124- } ;
116+ let parent_parent = ( parent_def_kind == DefKind :: Const
117+ && parent_opt_item_name == Some ( kw:: Underscore ) )
118+ . then ( || cx. tcx . parent ( parent) ) ;
125119
126120 let self_ty_has_local_parent = match impl_. self_ty . kind {
127121 TyKind :: Path ( QPath :: Resolved ( _, ty_path) ) => {
128- path_has_local_parent ( ty_path, cx, & * local_parents )
122+ path_has_local_parent ( ty_path, cx, parent , parent_parent )
129123 }
130124 TyKind :: TraitObject ( [ principle_poly_trait_ref, ..] , _, _) => {
131125 path_has_local_parent (
132126 principle_poly_trait_ref. trait_ref . path ,
133127 cx,
134- & * local_parents,
128+ parent,
129+ parent_parent,
135130 )
136131 }
137132 TyKind :: TraitObject ( [ ] , _, _)
@@ -153,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
153148
154149 let of_trait_has_local_parent = impl_
155150 . of_trait
156- . map ( |of_trait| path_has_local_parent ( of_trait. path , cx, & * local_parents ) )
151+ . map ( |of_trait| path_has_local_parent ( of_trait. path , cx, parent , parent_parent ) )
157152 . unwrap_or ( false ) ;
158153
159154 // If none of them have a local parent (LOGICAL NOR) this means that
@@ -217,6 +212,14 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
217212/// std::convert::PartialEq<Foo<Bar>>
218213/// ^^^^^^^^^^^^^^^^^^^^^^^
219214/// ```
220- fn path_has_local_parent ( path : & Path < ' _ > , cx : & LateContext < ' _ > , local_parents : & [ DefId ] ) -> bool {
221- path. res . opt_def_id ( ) . is_some_and ( |did| local_parents. contains ( & cx. tcx . parent ( did) ) )
215+ fn path_has_local_parent (
216+ path : & Path < ' _ > ,
217+ cx : & LateContext < ' _ > ,
218+ impl_parent : DefId ,
219+ impl_parent_parent : Option < DefId > ,
220+ ) -> bool {
221+ path. res . opt_def_id ( ) . is_some_and ( |did| {
222+ let res_parent = cx. tcx . parent ( did) ;
223+ res_parent == impl_parent || Some ( res_parent) == impl_parent_parent
224+ } )
222225}
0 commit comments