rustdoc: Stop using write! in UrlFragment::render

This commit is contained in:
Vadim Petrochenkov 2022-05-21 15:19:53 +03:00
parent f7521db243
commit c5449f57c1
2 changed files with 16 additions and 14 deletions

View File

@ -525,7 +525,7 @@ impl Item {
if let Ok((mut href, ..)) = href(*did, cx) { if let Ok((mut href, ..)) = href(*did, cx) {
debug!(?href); debug!(?href);
if let Some(ref fragment) = *fragment { if let Some(ref fragment) = *fragment {
fragment.render(&mut href, cx.tcx()).unwrap() fragment.render(&mut href, cx.tcx())
} }
Some(RenderedLink { Some(RenderedLink {
original_text: s.clone(), original_text: s.clone(),

View File

@ -20,7 +20,6 @@ use rustc_span::BytePos;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt::Write;
use std::mem; use std::mem;
use std::ops::Range; use std::ops::Range;
@ -226,34 +225,37 @@ pub(crate) enum UrlFragment {
impl UrlFragment { impl UrlFragment {
/// Render the fragment, including the leading `#`. /// Render the fragment, including the leading `#`.
pub(crate) fn render(&self, s: &mut String, tcx: TyCtxt<'_>) -> std::fmt::Result { pub(crate) fn render(&self, s: &mut String, tcx: TyCtxt<'_>) {
s.push('#'); s.push('#');
match self { match self {
&UrlFragment::Item(def_id) => { &UrlFragment::Item(def_id) => {
let name = tcx.item_name(def_id); let kind = match tcx.def_kind(def_id) {
match tcx.def_kind(def_id) {
DefKind::AssocFn => { DefKind::AssocFn => {
if tcx.associated_item(def_id).defaultness.has_value() { if tcx.associated_item(def_id).defaultness.has_value() {
write!(s, "method.{}", name) "method."
} else { } else {
write!(s, "tymethod.{}", name) "tymethod."
} }
} }
DefKind::AssocConst => write!(s, "associatedconstant.{}", name), DefKind::AssocConst => "associatedconstant.",
DefKind::AssocTy => write!(s, "associatedtype.{}", name), DefKind::AssocTy => "associatedtype.",
DefKind::Variant => write!(s, "variant.{}", name), DefKind::Variant => "variant.",
DefKind::Field => { DefKind::Field => {
let parent_id = tcx.parent(def_id); let parent_id = tcx.parent(def_id);
if tcx.def_kind(parent_id) == DefKind::Variant { if tcx.def_kind(parent_id) == DefKind::Variant {
write!(s, "variant.{}.field.{}", tcx.item_name(parent_id), name) s.push_str("variant.");
s.push_str(tcx.item_name(parent_id).as_str());
".field."
} else { } else {
write!(s, "structfield.{}", name) "structfield."
} }
} }
kind => bug!("unexpected associated item kind: {:?}", kind), kind => bug!("unexpected associated item kind: {:?}", kind),
};
s.push_str(kind);
s.push_str(tcx.item_name(def_id).as_str());
} }
} UrlFragment::UserWritten(raw) => s.push_str(&raw),
UrlFragment::UserWritten(raw) => Ok(s.push_str(&raw)),
} }
} }
} }