1
Fork 0

fix clippy perf lints

This commit is contained in:
klensy 2022-05-24 13:35:54 -04:00
parent 678059f7d0
commit 2a326bcc74
4 changed files with 23 additions and 15 deletions

View file

@ -5,6 +5,7 @@
//! assume that HTML output is desired, although it may be possible to redesign
//! them in the future to instead emit any format desired.
use std::borrow::Cow;
use std::cell::Cell;
use std::fmt;
use std::iter;
@ -1295,9 +1296,11 @@ impl clean::Visibility {
item_did: ItemId,
cx: &'a Context<'tcx>,
) -> impl fmt::Display + 'a + Captures<'tcx> {
let to_print = match self {
clean::Public => "pub ".to_owned(),
clean::Inherited => String::new(),
use std::fmt::Write as _;
let to_print: Cow<'static, str> = match self {
clean::Public => "pub ".into(),
clean::Inherited => "".into(),
clean::Visibility::Restricted(vis_did) => {
// FIXME(camelid): This may not work correctly if `item_did` is a module.
// However, rustdoc currently never displays a module's
@ -1305,16 +1308,16 @@ impl clean::Visibility {
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
if vis_did.is_crate_root() {
"pub(crate) ".to_owned()
"pub(crate) ".into()
} else if parent_module == Some(vis_did) {
// `pub(in foo)` where `foo` is the parent module
// is the same as no visibility modifier
String::new()
"".into()
} else if parent_module
.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
== Some(vis_did)
{
"pub(super) ".to_owned()
"pub(super) ".into()
} else {
let path = cx.tcx().def_path(vis_did);
debug!("path={:?}", path);
@ -1324,14 +1327,14 @@ impl clean::Visibility {
let mut s = "pub(in ".to_owned();
for seg in &path.data[..path.data.len() - 1] {
s.push_str(&format!("{}::", seg.data.get_opt_name().unwrap()));
let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
}
s.push_str(&format!("{}) ", anchor));
s
let _ = write!(s, "{}) ", anchor);
s.into()
}
}
};
display_fn(move |f| f.write_str(&to_print))
display_fn(move |f| write!(f, "{}", to_print))
}
/// This function is the same as print_with_space, except that it renders no links.

View file

@ -2537,6 +2537,8 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
}
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
use std::fmt::Write as _;
let mut sidebar = String::new();
let item_sections_in_use: FxHashSet<_> = items
@ -2554,7 +2556,7 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
.map(|it| item_ty_to_section(it.type_()))
.collect();
for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
let _ = write!(sidebar, "<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name());
}
if !sidebar.is_empty() {

View file

@ -182,8 +182,8 @@ pub(crate) fn build_index<'tcx>(
})
.expect("failed serde conversion")
// All these `replace` calls are because we have to go through JS string for JSON content.
.replace(r#"\"#, r"\\")
.replace(r#"'"#, r"\'")
.replace('\\', r"\\")
.replace('\'', r"\'")
// We need to escape double quotes for the JSON.
.replace("\\\"", "\\\\\"")
)

View file

@ -163,15 +163,18 @@ impl TocBuilder {
impl Toc {
fn print_inner(&self, v: &mut String) {
use std::fmt::Write as _;
v.push_str("<ul>");
for entry in &self.entries {
// recursively format this table of contents
v.push_str(&format!(
let _ = write!(
v,
"\n<li><a href=\"#{id}\">{num} {name}</a>",
id = entry.id,
num = entry.sec_number,
name = entry.name
));
);
entry.children.print_inner(&mut *v);
v.push_str("</li>");
}