Skip to content

Commit 2a326bc

Browse files
committed
fix clippy perf lints
1 parent 678059f commit 2a326bc

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

src/librustdoc/html/format.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! assume that HTML output is desired, although it may be possible to redesign
66
//! them in the future to instead emit any format desired.
77
8+
use std::borrow::Cow;
89
use std::cell::Cell;
910
use std::fmt;
1011
use std::iter;
@@ -1295,26 +1296,28 @@ impl clean::Visibility {
12951296
item_did: ItemId,
12961297
cx: &'a Context<'tcx>,
12971298
) -> impl fmt::Display + 'a + Captures<'tcx> {
1298-
let to_print = match self {
1299-
clean::Public => "pub ".to_owned(),
1300-
clean::Inherited => String::new(),
1299+
use std::fmt::Write as _;
1300+
1301+
let to_print: Cow<'static, str> = match self {
1302+
clean::Public => "pub ".into(),
1303+
clean::Inherited => "".into(),
13011304
clean::Visibility::Restricted(vis_did) => {
13021305
// FIXME(camelid): This may not work correctly if `item_did` is a module.
13031306
// However, rustdoc currently never displays a module's
13041307
// visibility, so it shouldn't matter.
13051308
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
13061309

13071310
if vis_did.is_crate_root() {
1308-
"pub(crate) ".to_owned()
1311+
"pub(crate) ".into()
13091312
} else if parent_module == Some(vis_did) {
13101313
// `pub(in foo)` where `foo` is the parent module
13111314
// is the same as no visibility modifier
1312-
String::new()
1315+
"".into()
13131316
} else if parent_module
13141317
.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
13151318
== Some(vis_did)
13161319
{
1317-
"pub(super) ".to_owned()
1320+
"pub(super) ".into()
13181321
} else {
13191322
let path = cx.tcx().def_path(vis_did);
13201323
debug!("path={:?}", path);
@@ -1324,14 +1327,14 @@ impl clean::Visibility {
13241327

13251328
let mut s = "pub(in ".to_owned();
13261329
for seg in &path.data[..path.data.len() - 1] {
1327-
s.push_str(&format!("{}::", seg.data.get_opt_name().unwrap()));
1330+
let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
13281331
}
1329-
s.push_str(&format!("{}) ", anchor));
1330-
s
1332+
let _ = write!(s, "{}) ", anchor);
1333+
s.into()
13311334
}
13321335
}
13331336
};
1334-
display_fn(move |f| f.write_str(&to_print))
1337+
display_fn(move |f| write!(f, "{}", to_print))
13351338
}
13361339

13371340
/// This function is the same as print_with_space, except that it renders no links.

src/librustdoc/html/render/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,8 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
25372537
}
25382538

25392539
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
2540+
use std::fmt::Write as _;
2541+
25402542
let mut sidebar = String::new();
25412543

25422544
let item_sections_in_use: FxHashSet<_> = items
@@ -2554,7 +2556,7 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
25542556
.map(|it| item_ty_to_section(it.type_()))
25552557
.collect();
25562558
for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
2557-
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
2559+
let _ = write!(sidebar, "<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name());
25582560
}
25592561

25602562
if !sidebar.is_empty() {

src/librustdoc/html/render/search_index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ pub(crate) fn build_index<'tcx>(
182182
})
183183
.expect("failed serde conversion")
184184
// All these `replace` calls are because we have to go through JS string for JSON content.
185-
.replace(r#"\"#, r"\\")
186-
.replace(r#"'"#, r"\'")
185+
.replace('\\', r"\\")
186+
.replace('\'', r"\'")
187187
// We need to escape double quotes for the JSON.
188188
.replace("\\\"", "\\\\\"")
189189
)

src/librustdoc/html/toc.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,18 @@ impl TocBuilder {
163163

164164
impl Toc {
165165
fn print_inner(&self, v: &mut String) {
166+
use std::fmt::Write as _;
167+
166168
v.push_str("<ul>");
167169
for entry in &self.entries {
168170
// recursively format this table of contents
169-
v.push_str(&format!(
171+
let _ = write!(
172+
v,
170173
"\n<li><a href=\"#{id}\">{num} {name}</a>",
171174
id = entry.id,
172175
num = entry.sec_number,
173176
name = entry.name
174-
));
177+
);
175178
entry.children.print_inner(&mut *v);
176179
v.push_str("</li>");
177180
}

0 commit comments

Comments
 (0)