Skip to content

Commit e04bcaf

Browse files
committed
rustdoc: Some more lazy formatting
Mostly replaces a few `format!` calls with `format_args!`
1 parent 09a3713 commit e04bcaf

3 files changed

Lines changed: 32 additions & 34 deletions

File tree

src/librustdoc/html/render/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,7 +2955,7 @@ fn render_call_locations<W: fmt::Write>(
29552955
fn render_attributes_in_code(
29562956
w: &mut impl fmt::Write,
29572957
item: &clean::Item,
2958-
prefix: &str,
2958+
prefix: impl fmt::Display,
29592959
cx: &Context<'_>,
29602960
) -> fmt::Result {
29612961
render_attributes_in_code_with_options(w, item, prefix, cx, true, "")
@@ -2964,14 +2964,14 @@ fn render_attributes_in_code(
29642964
pub(super) fn render_attributes_in_code_with_options(
29652965
w: &mut impl fmt::Write,
29662966
item: &clean::Item,
2967-
prefix: &str,
2967+
prefix: impl fmt::Display,
29682968
cx: &Context<'_>,
29692969
render_doc_hidden: bool,
29702970
open_tag: &str,
29712971
) -> fmt::Result {
29722972
w.write_str(open_tag)?;
29732973
if render_doc_hidden && item.is_doc_hidden() {
2974-
render_code_attribute(prefix, "#[doc(hidden)]", w)?;
2974+
render_code_attribute(&prefix, "#[doc(hidden)]", w)?;
29752975
}
29762976
for attr in &item.attrs.other_attrs {
29772977
let hir::Attribute::Parsed(kind) = attr else { continue };
@@ -2986,7 +2986,7 @@ pub(super) fn render_attributes_in_code_with_options(
29862986
AttributeKind::NonExhaustive(..) => Cow::Borrowed("#[non_exhaustive]"),
29872987
_ => continue,
29882988
};
2989-
render_code_attribute(prefix, attr.as_ref(), w)?;
2989+
render_code_attribute(&prefix, attr.as_ref(), w)?;
29902990
}
29912991

29922992
if let Some(def_id) = item.def_id()
@@ -3008,7 +3008,11 @@ fn render_repr_attribute_in_code(
30083008
Ok(())
30093009
}
30103010

3011-
fn render_code_attribute(prefix: &str, attr: &str, w: &mut impl fmt::Write) -> fmt::Result {
3011+
fn render_code_attribute(
3012+
prefix: impl fmt::Display,
3013+
attr: impl fmt::Display,
3014+
w: &mut impl fmt::Write,
3015+
) -> fmt::Result {
30123016
write!(w, "<div class=\"code-attribute\">{prefix}{attr}</div>")
30133017
}
30143018

src/librustdoc/html/render/print_item.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::cmp::Ordering;
23
use std::fmt::{self, Display, Write as _};
34
use std::iter;
@@ -395,25 +396,26 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
395396
let (stab_tags, deprecation) = match import.source.did {
396397
Some(import_def_id) => {
397398
let stab_tags =
398-
print_extra_info_tags(tcx, myitem, item, Some(import_def_id))
399-
.to_string();
399+
print_extra_info_tags(tcx, myitem, item, Some(import_def_id));
400400
let deprecation = tcx
401401
.lookup_deprecation(import_def_id)
402402
.is_some_and(|deprecation| deprecation.is_in_effect());
403-
(stab_tags, deprecation)
403+
(Some(stab_tags), deprecation)
404404
}
405-
None => (String::new(), item.is_deprecated(tcx)),
405+
None => (None, item.is_deprecated(tcx)),
406406
};
407407
let visibility_and_hidden = visibility_and_hidden(myitem);
408408
let id = match import.kind {
409-
clean::ImportKind::Simple(s) => {
410-
format!(" id=\"{}\"", cx.derive_id(format!("reexport.{s}")))
411-
}
412-
clean::ImportKind::Glob => String::new(),
409+
clean::ImportKind::Simple(s) => Some(format_args!(
410+
" id=\"{}\"",
411+
cx.derive_id(format!("reexport.{s}"))
412+
)),
413+
clean::ImportKind::Glob => None,
413414
};
414415
write!(
415416
w,
416417
"<dt{id}{deprecation_attr}><code>",
418+
id = id.maybe_display(),
417419
deprecation_attr = deprecation_class_attr(deprecation)
418420
)?;
419421
write!(
@@ -423,6 +425,7 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
423425
vis = visibility_print_with_space(myitem, cx),
424426
imp = print_import(import, cx),
425427
visibility_and_hidden = visibility_and_hidden,
428+
stab_tags = stab_tags.maybe_display(),
426429
)?;
427430
}
428431
_ => {
@@ -512,18 +515,14 @@ fn print_extra_info_tags(
512515
write!(f, "{}", tag_html("unstable", "", "Experimental"))?;
513516
}
514517

518+
debug!(name = ?item.name, cfg = ?item.cfg, parent_cfg = ?parent.cfg, "Portability");
519+
515520
let cfg = match (&item.cfg, parent.cfg.as_ref()) {
516-
(Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg),
517-
(cfg, _) => cfg.as_deref().cloned(),
521+
(Some(cfg), Some(parent_cfg)) => cfg.simplify_with(parent_cfg).map(Cow::Owned),
522+
(cfg, _) => cfg.as_deref().map(Cow::Borrowed),
518523
};
519524

520-
debug!(
521-
"Portability name={name:?} {cfg:?} - {parent_cfg:?} = {cfg:?}",
522-
name = item.name,
523-
cfg = item.cfg,
524-
parent_cfg = parent.cfg
525-
);
526-
if let Some(ref cfg) = cfg {
525+
if let Some(cfg) = cfg {
527526
write!(
528527
f,
529528
"{}",
@@ -976,7 +975,7 @@ fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt:
976975
"Dyn Compatibility",
977976
"dyn-compatibility",
978977
None,
979-
format!(
978+
format_args!(
980979
"<div class=\"dyn-compatibility-info\"><p>This trait {} \
981980
<a href=\"{base}/reference/items/traits.html#dyn-compatibility\">dyn compatible</a>.</p>\
982981
<p><i>In older versions of Rust, dyn compatibility was called \"object safety\".</i></p></div>",
@@ -1775,10 +1774,10 @@ fn item_variants(
17751774
w,
17761775
"{}",
17771776
write_section_heading(
1778-
&format!("Variants{}", document_non_exhaustive_header(it)),
1777+
format_args!("Variants{}", document_non_exhaustive_header(it)),
17791778
"variants",
17801779
Some("variants"),
1781-
format!("{}<div class=\"variants\">", document_non_exhaustive(it)),
1780+
format_args!("{}<div class=\"variants\">", document_non_exhaustive(it)),
17821781
),
17831782
)?;
17841783

@@ -2105,20 +2104,15 @@ fn item_fields(
21052104
if let None | Some(CtorKind::Fn) = ctor_kind
21062105
&& fields.peek().is_some()
21072106
{
2108-
let title = format!(
2107+
let title = format_args!(
21092108
"{}{}",
21102109
if ctor_kind.is_none() { "Fields" } else { "Tuple Fields" },
21112110
document_non_exhaustive_header(it),
21122111
);
21132112
write!(
21142113
w,
21152114
"{}",
2116-
write_section_heading(
2117-
&title,
2118-
"fields",
2119-
Some("fields"),
2120-
document_non_exhaustive(it)
2121-
)
2115+
write_section_heading(title, "fields", Some("fields"), document_non_exhaustive(it))
21222116
)?;
21232117
for (index, (field, ty)) in fields.enumerate() {
21242118
let field_name =
@@ -2554,7 +2548,7 @@ fn render_struct_fields(
25542548
}
25552549
for field in fields {
25562550
if let clean::StructFieldItem(ref ty) = field.kind {
2557-
render_attributes_in_code(w, field, &format!("{tab} "), cx)?;
2551+
render_attributes_in_code(w, field, format_args!("{tab} "), cx)?;
25582552
writeln!(
25592553
w,
25602554
"{tab} {vis}{name}: {ty},",

src/librustdoc/html/render/write_shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl CratesIndexPart {
413413
let layout = &cx.shared.layout;
414414
let style_files = &cx.shared.style_files;
415415
const DELIMITER: &str = "\u{FFFC}"; // users are being naughty if they have this
416-
let content = format!(
416+
let content = format_args!(
417417
"<div class=\"main-heading\">\
418418
<h1>List of all crates</h1>\
419419
<rustdoc-toolbar></rustdoc-toolbar>\

0 commit comments

Comments
 (0)