Skip to content

Commit 52a7615

Browse files
Improve code readability
1 parent e3b7035 commit 52a7615

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,24 +1488,25 @@ fn first_not_private(
14881488
hir_id: hir::HirId,
14891489
path: &hir::Path<'_>,
14901490
) -> Option<Path> {
1491-
if path.segments.is_empty() {
1492-
return None;
1493-
}
1494-
let parent_def_id = if path.segments.len() == 1 {
1495-
// Then it's available in the same scope as the owner.
1496-
hir_id.owner.def_id
1497-
} else {
1498-
// It's not available in the same scope, so we start from the parent of the item.
1499-
path.segments[path.segments.len() - 2].res.opt_def_id()?.as_local()?
1491+
let (parent_def_id, mut ident) = match &path.segments[..] {
1492+
[] => return None,
1493+
// Relative paths are available in the same scope as the owner.
1494+
[leaf] => (cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident),
1495+
// So are self paths.
1496+
[parent, leaf] if parent.ident.name == kw::SelfLower => {
1497+
(cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident)
1498+
}
1499+
// Crate paths are not. We start from the crate root.
1500+
[parent, leaf] if parent.ident.name == kw::Crate => {
1501+
(LOCAL_CRATE.as_def_id().as_local()?, leaf.ident)
1502+
}
1503+
// Absolute paths are not. We start from the parent of the item.
1504+
[.., parent, leaf] => (parent.res.opt_def_id()?.as_local()?, leaf.ident),
15001505
};
15011506
let target_def_id = path.res.opt_def_id()?;
1502-
let mut ident = path.segments.last().unwrap().ident;
15031507
// First we try to get the `DefId` of the item.
1504-
for child in cx
1505-
.tcx
1506-
.module_children_local(cx.tcx.local_parent(parent_def_id))
1507-
.iter()
1508-
.filter(move |c| c.ident == ident)
1508+
for child in
1509+
cx.tcx.module_children_local(parent_def_id).iter().filter(move |c| c.ident == ident)
15091510
{
15101511
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = child.res {
15111512
continue;
@@ -1518,26 +1519,20 @@ fn first_not_private(
15181519
let Some(local_use_def_id) = use_def_id.as_local()
15191520
{
15201521
let hir = cx.tcx.hir();
1521-
// let parent_mod = hir.local_def_id_to_hir_id();
15221522
for item_id in hir.module_items(cx.tcx.local_parent(local_use_def_id)) {
15231523
let item = hir.item(item_id);
1524-
if item.ident == ident {
1525-
match item.kind {
1526-
hir::ItemKind::Use(path, _) => {
1527-
for res in &path.res {
1528-
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
1529-
continue;
1530-
}
1531-
if !cx.tcx.is_doc_hidden(use_def_id) &&
1532-
cx.tcx.local_visibility(local_use_def_id).is_public() {
1533-
break 'reexps;
1534-
}
1535-
ident = path.segments.last().unwrap().ident;
1536-
last_path_res = Some((path, res));
1537-
continue 'reexps;
1538-
}
1524+
if item.ident == ident && let hir::ItemKind::Use(path, _) = item.kind {
1525+
for res in &path.res {
1526+
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
1527+
continue;
1528+
}
1529+
if !cx.tcx.is_doc_hidden(use_def_id) &&
1530+
cx.tcx.local_visibility(local_use_def_id).is_public() {
1531+
break 'reexps;
15391532
}
1540-
_ => {}
1533+
ident = path.segments.last().unwrap().ident;
1534+
last_path_res = Some((path, res));
1535+
continue 'reexps;
15411536
}
15421537
}
15431538
}

0 commit comments

Comments
 (0)