Skip to content

Commit 1288b80

Browse files
committed
rustc_metadata: Optimize and document module children decoding
1 parent c34e3f0 commit 1288b80

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

clippy_utils/src/lib.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ use rustc_hir::{
8282
TraitItemKind, TraitRef, TyKind, UnOp, ArrayLen
8383
};
8484
use rustc_lint::{LateContext, Level, Lint, LintContext};
85-
use rustc_middle::hir::exports::Export;
8685
use rustc_middle::hir::map::Map;
8786
use rustc_middle::hir::place::PlaceBase;
8887
use rustc_middle::ty as rustc_ty;
@@ -523,10 +522,21 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
523522
}
524523
};
525524
}
526-
fn item_child_by_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, name: &str) -> Option<&'tcx Export> {
527-
tcx.item_children(def_id)
528-
.iter()
529-
.find(|item| item.ident.name.as_str() == name)
525+
fn item_child_by_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, name: &str) -> Option<Res> {
526+
match tcx.def_kind(def_id) {
527+
DefKind::Mod | DefKind::Enum | DefKind::Trait => tcx
528+
.item_children(def_id)
529+
.iter()
530+
.find(|item| item.ident.name.as_str() == name)
531+
.map(|child| child.res.expect_non_local()),
532+
DefKind::Impl => tcx
533+
.associated_item_def_ids(def_id)
534+
.iter()
535+
.copied()
536+
.find(|assoc_def_id| tcx.item_name(*assoc_def_id).as_str() == name)
537+
.map(|assoc_def_id| Res::Def(tcx.def_kind(assoc_def_id), assoc_def_id)),
538+
_ => None,
539+
}
530540
}
531541

532542
let (krate, first, path) = match *path {
@@ -543,15 +553,12 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
543553
let last = path
544554
.iter()
545555
.copied()
546-
// `get_def_path` seems to generate these empty segments for extern blocks.
547-
// We can just ignore them.
548-
.filter(|segment| !segment.is_empty())
549556
// for each segment, find the child item
550-
.try_fold(first, |item, segment| {
551-
let def_id = item.res.def_id();
557+
.try_fold(first, |res, segment| {
558+
let def_id = res.def_id();
552559
if let Some(item) = item_child_by_name(tcx, def_id, segment) {
553560
Some(item)
554-
} else if matches!(item.res, Res::Def(DefKind::Enum | DefKind::Struct, _)) {
561+
} else if matches!(res, Res::Def(DefKind::Enum | DefKind::Struct, _)) {
555562
// it is not a child item so check inherent impl items
556563
tcx.inherent_impls(def_id)
557564
.iter()
@@ -560,7 +567,7 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
560567
None
561568
}
562569
});
563-
try_res!(last).res.expect_non_local()
570+
try_res!(last).expect_non_local()
564571
}
565572

566573
/// Convenience function to get the `DefId` of a trait by path.

0 commit comments

Comments
 (0)