Skip to content

Commit 078e902

Browse files
Improve code readability
1 parent d67a31f commit 078e902

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
@@ -1505,24 +1505,25 @@ fn first_not_private(
15051505
hir_id: hir::HirId,
15061506
path: &hir::Path<'_>,
15071507
) -> Option<Path> {
1508-
if path.segments.is_empty() {
1509-
return None;
1510-
}
1511-
let parent_def_id = if path.segments.len() == 1 {
1512-
// Then it's available in the same scope as the owner.
1513-
hir_id.owner.def_id
1514-
} else {
1515-
// It's not available in the same scope, so we start from the parent of the item.
1516-
path.segments[path.segments.len() - 2].res.opt_def_id()?.as_local()?
1508+
let (parent_def_id, mut ident) = match &path.segments[..] {
1509+
[] => return None,
1510+
// Relative paths are available in the same scope as the owner.
1511+
[leaf] => (cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident),
1512+
// So are self paths.
1513+
[parent, leaf] if parent.ident.name == kw::SelfLower => {
1514+
(cx.tcx.local_parent(hir_id.owner.def_id), leaf.ident)
1515+
}
1516+
// Crate paths are not. We start from the crate root.
1517+
[parent, leaf] if parent.ident.name == kw::Crate => {
1518+
(LOCAL_CRATE.as_def_id().as_local()?, leaf.ident)
1519+
}
1520+
// Absolute paths are not. We start from the parent of the item.
1521+
[.., parent, leaf] => (parent.res.opt_def_id()?.as_local()?, leaf.ident),
15171522
};
15181523
let target_def_id = path.res.opt_def_id()?;
1519-
let mut ident = path.segments.last().unwrap().ident;
15201524
// First we try to get the `DefId` of the item.
1521-
for child in cx
1522-
.tcx
1523-
.module_children_local(cx.tcx.local_parent(parent_def_id))
1524-
.iter()
1525-
.filter(move |c| c.ident == ident)
1525+
for child in
1526+
cx.tcx.module_children_local(parent_def_id).iter().filter(move |c| c.ident == ident)
15261527
{
15271528
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = child.res {
15281529
continue;
@@ -1535,26 +1536,20 @@ fn first_not_private(
15351536
let Some(local_use_def_id) = use_def_id.as_local()
15361537
{
15371538
let hir = cx.tcx.hir();
1538-
// let parent_mod = hir.local_def_id_to_hir_id();
15391539
for item_id in hir.module_items(cx.tcx.local_parent(local_use_def_id)) {
15401540
let item = hir.item(item_id);
1541-
if item.ident == ident {
1542-
match item.kind {
1543-
hir::ItemKind::Use(path, _) => {
1544-
for res in &path.res {
1545-
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
1546-
continue;
1547-
}
1548-
if !cx.tcx.is_doc_hidden(use_def_id) &&
1549-
cx.tcx.local_visibility(local_use_def_id).is_public() {
1550-
break 'reexps;
1551-
}
1552-
ident = path.segments.last().unwrap().ident;
1553-
last_path_res = Some((path, res));
1554-
continue 'reexps;
1555-
}
1541+
if item.ident == ident && let hir::ItemKind::Use(path, _) = item.kind {
1542+
for res in &path.res {
1543+
if let Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) = res {
1544+
continue;
1545+
}
1546+
if !cx.tcx.is_doc_hidden(use_def_id) &&
1547+
cx.tcx.local_visibility(local_use_def_id).is_public() {
1548+
break 'reexps;
15561549
}
1557-
_ => {}
1550+
ident = path.segments.last().unwrap().ident;
1551+
last_path_res = Some((path, res));
1552+
continue 'reexps;
15581553
}
15591554
}
15601555
}

0 commit comments

Comments
 (0)