Skip to content

Commit

Permalink
Use more named format args
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 16, 2023
1 parent a1a94b1 commit 16b34bf
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 85 deletions.
6 changes: 3 additions & 3 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ where
fn get_lifetime(region: Region<'_>, names_map: &FxHashMap<Symbol, Lifetime>) -> Lifetime {
region_name(region)
.map(|name| {
names_map.get(&name).unwrap_or_else(|| {
panic!("Missing lifetime with name {:?} for {region:?}", name.as_str())
})
names_map
.get(&name)
.unwrap_or_else(|| panic!("Missing lifetime with name {name:?} for {region:?}"))
})
.unwrap_or(&Lifetime::statik())
.clone()
Expand Down
24 changes: 15 additions & 9 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,12 @@ pub(super) fn render_macro_arms<'a>(
) -> String {
let mut out = String::new();
for matcher in matchers {
writeln!(out, " {} => {{ ... }}{arm_delim}", render_macro_matcher(tcx, matcher))
.unwrap();
writeln!(
out,
" {matcher} => {{ ... }}{arm_delim}",
matcher = render_macro_matcher(tcx, matcher),
)
.unwrap();
}
out
}
Expand All @@ -618,19 +622,21 @@ pub(super) fn display_macro_source(
let matchers = def.body.tokens.chunks(4).map(|arm| &arm[0]);

if def.macro_rules {
format!("macro_rules! {name} {{\n{}}}", render_macro_arms(cx.tcx, matchers, ";"))
format!("macro_rules! {name} {{\n{arms}}}", arms = render_macro_arms(cx.tcx, matchers, ";"))
} else {
if matchers.len() <= 1 {
format!(
"{}macro {name}{} {{\n ...\n}}",
visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
matchers.map(|matcher| render_macro_matcher(cx.tcx, matcher)).collect::<String>(),
"{vis}macro {name}{matchers} {{\n ...\n}}",
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
matchers = matchers
.map(|matcher| render_macro_matcher(cx.tcx, matcher))
.collect::<String>(),
)
} else {
format!(
"{}macro {name} {{\n{}}}",
visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
render_macro_arms(cx.tcx, matchers, ","),
"{vis}macro {name} {{\n{arms}}}",
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
arms = render_macro_arms(cx.tcx, matchers, ","),
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/docfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ impl DocFS {
let sender = self.errors.clone().expect("can't write after closing");
self.pool.execute(move || {
fs::write(&path, contents).unwrap_or_else(|e| {
sender.send(format!("\"{}\": {e}", path.display())).unwrap_or_else(|_| {
panic!("failed to send error on \"{}\"", path.display())
})
sender.send(format!("\"{path}\": {e}", path = path.display())).unwrap_or_else(
|_| panic!("failed to send error on \"{}\"", path.display()),
)
});
});
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/librustdoc/externalfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ pub(crate) fn load_string<P: AsRef<Path>>(
let contents = match fs::read(file_path) {
Ok(bytes) => bytes,
Err(e) => {
diag.struct_err(format!("error reading `{}`: {e}", file_path.display())).emit();
diag.struct_err(format!(
"error reading `{file_path}`: {e}",
file_path = file_path.display()
))
.emit();
return Err(LoadStringError::ReadFail);
}
};
Expand Down
51 changes: 32 additions & 19 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ impl Buffer {
self.buffer
}

pub(crate) fn push(&mut self, c: char) {
self.buffer.push(c);
}

pub(crate) fn push_str(&mut self, s: &str) {
self.buffer.push_str(s);
}
Expand Down Expand Up @@ -451,9 +455,9 @@ impl clean::GenericBound {
hir::TraitBoundModifier::MaybeConst => "",
};
if f.alternate() {
write!(f, "{modifier_str}{:#}", ty.print(cx))
write!(f, "{modifier_str}{ty:#}", ty = ty.print(cx))
} else {
write!(f, "{modifier_str}{}", ty.print(cx))
write!(f, "{modifier_str}{ty}", ty = ty.print(cx))
}
}
})
Expand Down Expand Up @@ -631,14 +635,14 @@ fn generate_macro_def_id_path(
let url = match cache.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
// `ExternalLocation::Remote` always end with a `/`.
format!("{s}{}", path.iter().map(|p| p.as_str()).join("/"))
format!("{s}{path}", path = path.iter().map(|p| p.as_str()).join("/"))
}
ExternalLocation::Local => {
// `root_path` always end with a `/`.
format!(
"{}{crate_name}/{}",
root_path.unwrap_or(""),
path.iter().map(|p| p.as_str()).join("/")
"{root_path}{crate_name}/{path}",
root_path = root_path.unwrap_or(""),
path = path.iter().map(|p| p.as_str()).join("/")
)
}
ExternalLocation::Unknown => {
Expand Down Expand Up @@ -827,17 +831,17 @@ fn resolved_path<'cx>(
let path = if use_absolute {
if let Ok((_, _, fqp)) = href(did, cx) {
format!(
"{}::{}",
join_with_double_colon(&fqp[..fqp.len() - 1]),
anchor(did, *fqp.last().unwrap(), cx)
"{path}::{anchor}",
path = join_with_double_colon(&fqp[..fqp.len() - 1]),
anchor = anchor(did, *fqp.last().unwrap(), cx)
)
} else {
last.name.to_string()
}
} else {
anchor(did, last.name, cx).to_string()
};
write!(w, "{path}{}", last.args.print(cx))?;
write!(w, "{path}{args}", args = last.args.print(cx))?;
}
Ok(())
}
Expand Down Expand Up @@ -945,8 +949,8 @@ pub(crate) fn anchor<'a, 'cx: 'a>(
if let Ok((url, short_ty, fqp)) = parts {
write!(
f,
r#"<a class="{short_ty}" href="{url}" title="{short_ty} {}">{text}</a>"#,
join_with_double_colon(&fqp),
r#"<a class="{short_ty}" href="{url}" title="{short_ty} {path}">{text}</a>"#,
path = join_with_double_colon(&fqp),
)
} else {
f.write_str(text.as_str())
Expand Down Expand Up @@ -1080,9 +1084,9 @@ fn fmt_type<'cx>(

if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
let text = if f.alternate() {
format!("*{m} {:#}", t.print(cx))
format!("*{m} {ty:#}", ty = t.print(cx))
} else {
format!("*{m} {}", t.print(cx))
format!("*{m} {ty}", ty = t.print(cx))
};
primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
} else {
Expand Down Expand Up @@ -1440,11 +1444,20 @@ impl clean::FnDecl {
clean::SelfValue => {
write!(f, "self")?;
}
clean::SelfBorrowed(Some(ref lt), mtbl) => {
write!(f, "{amp}{} {}self", lt.print(), mtbl.print_with_space())?;
clean::SelfBorrowed(Some(ref lt), mutability) => {
write!(
f,
"{amp}{lifetime} {mutability}self",
lifetime = lt.print(),
mutability = mutability.print_with_space(),
)?;
}
clean::SelfBorrowed(None, mtbl) => {
write!(f, "{amp}{}self", mtbl.print_with_space())?;
clean::SelfBorrowed(None, mutability) => {
write!(
f,
"{amp}{mutability}self",
mutability = mutability.print_with_space(),
)?;
}
clean::SelfExplicit(ref typ) => {
write!(f, "self: ")?;
Expand Down Expand Up @@ -1627,7 +1640,7 @@ impl clean::Import {
if name == self.source.path.last() {
write!(f, "use {};", self.source.print(cx))
} else {
write!(f, "use {} as {name};", self.source.print(cx))
write!(f, "use {source} as {name};", source = self.source.print(cx))
}
}
clean::ImportKind::Glob => {
Expand Down
12 changes: 8 additions & 4 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ fn string_without_closing_tag<T: Display>(
write!(out, "{text}").unwrap();
return None;
}
write!(out, "<span class=\"{}\">{text}", klass.as_html()).unwrap();
write!(out, "<span class=\"{klass}\">{text}", klass = klass.as_html()).unwrap();
return Some("</span>");
};

Expand All @@ -947,11 +947,15 @@ fn string_without_closing_tag<T: Display>(
match t {
"self" | "Self" => write!(
&mut path,
"<span class=\"{}\">{t}</span>",
Class::Self_(DUMMY_SP).as_html(),
"<span class=\"{klass}\">{t}</span>",
klass = Class::Self_(DUMMY_SP).as_html(),
),
"crate" | "super" => {
write!(&mut path, "<span class=\"{}\">{t}</span>", Class::KeyWord.as_html())
write!(
&mut path,
"<span class=\"{klass}\">{t}</span>",
klass = Class::KeyWord.as_html(),
)
}
t => write!(&mut path, "{t}"),
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
return Some(Event::Html(
format!(
"<div class=\"example-wrap\">\
<pre class=\"language-{lang}\"><code>{}</code></pre>\
<pre class=\"language-{lang}\"><code>{text}</code></pre>\
</div>",
Escape(&original_text),
text = Escape(&original_text),
)
.into(),
));
Expand Down Expand Up @@ -308,7 +308,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
// insert newline to clearly separate it from the
// previous block so we can shorten the html output
let mut s = Buffer::new();
s.push_str("\n");
s.push('\n');

highlight::render_example_with_highlighting(
&text,
Expand Down Expand Up @@ -394,7 +394,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
l.href == link.href
&& Some(&**text) == l.original_text.get(1..l.original_text.len() - 1)
}) {
debug!("replacing {text} with {}", link.new_text);
debug!("replacing {text} with {new_text}", new_text = link.new_text);
*text = CowStr::Borrowed(&link.new_text);
}
}
Expand All @@ -410,7 +410,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, I> {
.iter()
.find(|l| l.href == link.href && **text == *l.original_text)
{
debug!("replacing {text} with {}", link.new_text);
debug!("replacing {text} with {new_text}", new_text = link.new_text);
*text = CowStr::Borrowed(&link.new_text);
}
}
Expand Down Expand Up @@ -1038,7 +1038,7 @@ impl MarkdownWithToc<'_> {
html::push_html(&mut s, p);
}

format!("<nav id=\"TOC\">{}</nav>{s}", toc.into_toc().print())
format!("<nav id=\"TOC\">{toc}</nav>{s}", toc = toc.into_toc().print())
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ impl<'tcx> Context<'tcx> {
format!("API documentation for the Rust `{}` crate.", self.shared.layout.krate)
} else {
format!(
"API documentation for the Rust `{}` {tyname} in crate `{}`.",
it.name.as_ref().unwrap(),
self.shared.layout.krate
"API documentation for the Rust `{name}` {tyname} in crate `{krate}`.",
name = it.name.as_ref().unwrap(),
krate = self.shared.layout.krate,
)
};
let name;
Expand Down Expand Up @@ -263,7 +263,12 @@ impl<'tcx> Context<'tcx> {
current_path.push_str(&item_path(ty, names.last().unwrap().as_str()));
redirections.borrow_mut().insert(current_path, path);
}
None => return layout::redirect(&format!("{}{path}", self.root_path())),
None => {
return layout::redirect(&format!(
"{root}{path}",
root = self.root_path()
));
}
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,10 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<Strin
};

debug!(
"Portability {:?} {:?} (parent: {parent:?}) - {:?} = {cfg:?}",
item.name,
item.cfg,
parent.and_then(|p| p.cfg.as_ref()),
"Portability {name:?} {item_cfg:?} (parent: {parent:?}) - {parent_cfg:?} = {cfg:?}",
name = item.name,
item_cfg = item.cfg,
parent_cfg = parent.and_then(|p| p.cfg.as_ref()),
);

Some(cfg?.render_long_html())
Expand Down Expand Up @@ -1243,7 +1243,10 @@ fn render_deref_methods(
_ => None,
})
.expect("Expected associated type binding");
debug!("Render deref methods for {:#?}, target {target:#?}", impl_.inner_impl().for_);
debug!(
"Render deref methods for {for_:#?}, target {target:#?}",
for_ = impl_.inner_impl().for_
);
let what =
AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut };
if let Some(did) = target.def_id(cache) {
Expand Down
Loading

0 comments on commit 16b34bf

Please sign in to comment.