Skip to content

Commit 1f3d9a4

Browse files
committed
Streamline visibility_print_with_space.
Moving the visibility stuff into the `from_fn` avoids the `Cow` and makes the code a little shorter and simpler.
1 parent 8dde6d5 commit 1f3d9a4

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

src/librustdoc/html/format.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//! some of them support an alternate format that emits text, but that should
88
//! not be used external to this module.
99
10-
use std::borrow::Cow;
1110
use std::cmp::Ordering;
1211
use std::fmt::{self, Display, Write};
1312
use std::iter::{self, once};
@@ -1390,50 +1389,47 @@ impl clean::FnDecl {
13901389
}
13911390

13921391
pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) -> impl Display {
1393-
use std::fmt::Write as _;
1394-
let vis: Cow<'static, str> = match item.visibility(cx.tcx()) {
1395-
None => "".into(),
1396-
Some(ty::Visibility::Public) => "pub ".into(),
1397-
Some(ty::Visibility::Restricted(vis_did)) => {
1398-
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1399-
// However, rustdoc currently never displays a module's
1400-
// visibility, so it shouldn't matter.
1401-
let parent_module = find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id());
1402-
1403-
if vis_did.is_crate_root() {
1404-
"pub(crate) ".into()
1405-
} else if parent_module == Some(vis_did) {
1406-
// `pub(in foo)` where `foo` is the parent module
1407-
// is the same as no visibility modifier
1408-
"".into()
1409-
} else if parent_module.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1410-
== Some(vis_did)
1411-
{
1412-
"pub(super) ".into()
1413-
} else {
1414-
let path = cx.tcx().def_path(vis_did);
1415-
debug!("path={path:?}");
1416-
// modified from `resolved_path()` to work with `DefPathData`
1417-
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1418-
let anchor = print_anchor(vis_did, last_name, cx);
1419-
1420-
let mut s = "pub(in ".to_owned();
1421-
for seg in &path.data[..path.data.len() - 1] {
1422-
let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
1423-
}
1424-
let _ = write!(s, "{anchor}) ");
1425-
s.into()
1426-
}
1427-
}
1428-
};
1429-
1430-
let is_doc_hidden = item.is_doc_hidden();
14311392
fmt::from_fn(move |f| {
1432-
if is_doc_hidden {
1393+
if item.is_doc_hidden() {
14331394
f.write_str("#[doc(hidden)] ")?;
14341395
}
14351396

1436-
f.write_str(&vis)
1397+
match item.visibility(cx.tcx()) {
1398+
None => {}
1399+
Some(ty::Visibility::Public) => f.write_str("pub ")?,
1400+
Some(ty::Visibility::Restricted(vis_did)) => {
1401+
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1402+
// However, rustdoc currently never displays a module's
1403+
// visibility, so it shouldn't matter.
1404+
let parent_module =
1405+
find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id());
1406+
1407+
if vis_did.is_crate_root() {
1408+
f.write_str("pub(crate) ")?;
1409+
} else if parent_module == Some(vis_did) {
1410+
// `pub(in foo)` where `foo` is the parent module
1411+
// is the same as no visibility modifier; do nothing
1412+
} else if parent_module
1413+
.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1414+
== Some(vis_did)
1415+
{
1416+
f.write_str("pub(super) ")?;
1417+
} else {
1418+
let path = cx.tcx().def_path(vis_did);
1419+
debug!("path={path:?}");
1420+
// modified from `resolved_path()` to work with `DefPathData`
1421+
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1422+
let anchor = print_anchor(vis_did, last_name, cx);
1423+
1424+
f.write_str("pub(in ")?;
1425+
for seg in &path.data[..path.data.len() - 1] {
1426+
write!(f, "{}::", seg.data.get_opt_name().unwrap())?;
1427+
}
1428+
write!(f, "{anchor}) ")?;
1429+
}
1430+
}
1431+
}
1432+
Ok(())
14371433
})
14381434
}
14391435

0 commit comments

Comments
 (0)