From c5449f57c190c2bbe29d6a4c7f0ced7049a46b6f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 21 May 2022 15:19:53 +0300 Subject: [PATCH] rustdoc: Stop using `write!` in `UrlFragment::render` --- src/librustdoc/clean/types.rs | 2 +- .../passes/collect_intra_doc_links.rs | 28 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 3123ece3773..6ee80a3476c 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -525,7 +525,7 @@ impl Item { if let Ok((mut href, ..)) = href(*did, cx) { debug!(?href); if let Some(ref fragment) = *fragment { - fragment.render(&mut href, cx.tcx()).unwrap() + fragment.render(&mut href, cx.tcx()) } Some(RenderedLink { original_text: s.clone(), diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 976d89bec3b..b20402e532a 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -20,7 +20,6 @@ use rustc_span::BytePos; use smallvec::{smallvec, SmallVec}; use std::borrow::Cow; -use std::fmt::Write; use std::mem; use std::ops::Range; @@ -226,34 +225,37 @@ pub(crate) enum UrlFragment { impl UrlFragment { /// 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('#'); match self { &UrlFragment::Item(def_id) => { - let name = tcx.item_name(def_id); - match tcx.def_kind(def_id) { + let kind = match tcx.def_kind(def_id) { DefKind::AssocFn => { if tcx.associated_item(def_id).defaultness.has_value() { - write!(s, "method.{}", name) + "method." } else { - write!(s, "tymethod.{}", name) + "tymethod." } } - DefKind::AssocConst => write!(s, "associatedconstant.{}", name), - DefKind::AssocTy => write!(s, "associatedtype.{}", name), - DefKind::Variant => write!(s, "variant.{}", name), + DefKind::AssocConst => "associatedconstant.", + DefKind::AssocTy => "associatedtype.", + DefKind::Variant => "variant.", DefKind::Field => { let parent_id = tcx.parent(def_id); 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 { - write!(s, "structfield.{}", name) + "structfield." } } kind => bug!("unexpected associated item kind: {:?}", kind), - } + }; + s.push_str(kind); + s.push_str(tcx.item_name(def_id).as_str()); } - UrlFragment::UserWritten(raw) => Ok(s.push_str(&raw)), + UrlFragment::UserWritten(raw) => s.push_str(&raw), } } }