Skip to content

Commit 8f76773

Browse files
Return impl Display instead of String in fragment
1 parent a5f972e commit 8f76773

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/librustdoc/clean/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Write;
12
use std::hash::Hash;
23
use std::path::PathBuf;
34
use std::sync::{Arc, OnceLock as OnceCell};
@@ -524,7 +525,8 @@ impl Item {
524525
debug!(?url);
525526
match fragment {
526527
Some(UrlFragment::Item(def_id)) => {
527-
url.push_str(&crate::html::format::fragment(*def_id, cx.tcx()))
528+
write!(url, "{}", crate::html::format::fragment(*def_id, cx.tcx()))
529+
.unwrap();
528530
}
529531
Some(UrlFragment::UserWritten(raw)) => {
530532
url.push('#');

src/librustdoc/html/format.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -833,26 +833,27 @@ fn print_higher_ranked_params_with_space(
833833
})
834834
}
835835

836-
pub(crate) fn fragment(did: DefId, tcx: TyCtxt<'_>) -> String {
837-
let def_kind = tcx.def_kind(did);
838-
match def_kind {
839-
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
840-
let item_type = ItemType::from_def_id(did, tcx);
841-
format!("#{}.{}", item_type.as_str(), tcx.item_name(did))
842-
}
843-
DefKind::Field => {
844-
let parent_def_id = tcx.parent(did);
845-
let s;
846-
let kind = if tcx.def_kind(parent_def_id) == DefKind::Variant {
847-
s = format!("variant.{}.field", tcx.item_name(parent_def_id).as_str());
848-
&s
849-
} else {
850-
"structfield"
851-
};
852-
format!("#{kind}.{}", tcx.item_name(did))
836+
pub(crate) fn fragment(did: DefId, tcx: TyCtxt<'_>) -> impl Display {
837+
fmt::from_fn(move |f| {
838+
let def_kind = tcx.def_kind(did);
839+
match def_kind {
840+
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
841+
let item_type = ItemType::from_def_id(did, tcx);
842+
write!(f, "#{}.{}", item_type.as_str(), tcx.item_name(did))
843+
}
844+
DefKind::Field => {
845+
let parent_def_id = tcx.parent(did);
846+
f.write_char('#')?;
847+
if tcx.def_kind(parent_def_id) == DefKind::Variant {
848+
write!(f, "variant.{}.field", tcx.item_name(parent_def_id).as_str())?;
849+
} else {
850+
f.write_str("structfield")?;
851+
};
852+
write!(f, ".{}", tcx.item_name(did))
853+
}
854+
_ => Ok(()),
853855
}
854-
_ => String::new(),
855-
}
856+
})
856857
}
857858

858859
pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display {

0 commit comments

Comments
 (0)