@@ -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
@@ -114,25 +112,25 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
114112 // is using local items and so we don't lint on it.
115113
116114 // 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
120117 && parent_opt_item_name == Some ( kw:: Underscore )
121118 {
122- smallvec ! [ parent , cx. tcx. parent( parent) ]
119+ Some ( cx. tcx . parent ( parent) )
123120 } else {
124- smallvec ! [ parent ]
121+ None
125122 } ;
126123
127124 let self_ty_has_local_parent = match impl_. self_ty . kind {
128125 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 )
130127 }
131128 TyKind :: TraitObject ( [ principle_poly_trait_ref, ..] , _, _) => {
132129 path_has_local_parent (
133130 principle_poly_trait_ref. trait_ref . path ,
134131 cx,
135- & * local_parents,
132+ parent,
133+ parent_parent,
136134 )
137135 }
138136 TyKind :: TraitObject ( [ ] , _, _)
@@ -154,7 +152,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
154152
155153 let of_trait_has_local_parent = impl_
156154 . 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 ) )
158156 . unwrap_or ( false ) ;
159157
160158 // If none of them have a local parent (LOGICAL NOR) this means that
@@ -218,6 +216,14 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
218216/// std::convert::PartialEq<Foo<Bar>>
219217/// ^^^^^^^^^^^^^^^^^^^^^^^
220218/// ```
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+ let res_parent = cx. tcx . parent ( did) ;
227+ res_parent == impl_parent || Some ( res_parent) == impl_parent_parent
228+ } )
223229}
0 commit comments