Skip to content

Commit a8be6e0

Browse files
committed
Auto merge of #114204 - GuillaumeGomez:remove-unneeded-clone-calls, r=notriddle
[rustdoc] Remove unneeded `clone()` calls for `derive_id` I realized we were cloning values before passing them to `derive_id` where they are cloned again, which isn't great. Since they'll be cloned anyway, let's allow to pass both by reference and by value. r? `@notriddle`
2 parents 89acdae + 148a0c1 commit a8be6e0

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

src/librustdoc/html/markdown.rs

-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,6 @@ impl<'a, 'b, 'ids, I: Iterator<Item = SpannedEvent<'a>>> Iterator
532532
let start_tags = format!(
533533
"<h{level} id=\"{id}\">\
534534
<a href=\"#{id}\">",
535-
id = id,
536-
level = level
537535
);
538536
return Some((Event::Html(start_tags.into()), 0..0));
539537
}

src/librustdoc/html/markdown/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn test_unique_id() {
3838
];
3939

4040
let mut map = IdMap::new();
41-
let actual: Vec<String> = input.iter().map(|s| map.derive(s.to_string())).collect();
41+
let actual: Vec<String> = input.iter().map(|s| map.derive(s)).collect();
4242
assert_eq!(&actual[..], expected);
4343
}
4444

src/librustdoc/html/render/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'tcx> Context<'tcx> {
162162
self.shared.tcx.sess
163163
}
164164

165-
pub(super) fn derive_id(&mut self, id: String) -> String {
165+
pub(super) fn derive_id<S: AsRef<str> + ToString>(&mut self, id: S) -> String {
166166
self.id_map.derive(id)
167167
}
168168

src/librustdoc/html/render/mod.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1153,9 +1153,7 @@ fn render_assoc_items_inner(
11531153
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
11541154
let id =
11551155
cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx))));
1156-
if let Some(def_id) = type_.def_id(cx.cache()) {
1157-
cx.deref_id_map.insert(def_id, id.clone());
1158-
}
1156+
let derived_id = cx.derive_id(&id);
11591157
write_impl_section_heading(
11601158
&mut tmp_buf,
11611159
&format!(
@@ -1165,11 +1163,10 @@ fn render_assoc_items_inner(
11651163
),
11661164
&id,
11671165
);
1168-
(
1169-
RenderMode::ForDeref { mut_: deref_mut_ },
1170-
cx.derive_id(id),
1171-
r#" class="impl-items""#,
1172-
)
1166+
if let Some(def_id) = type_.def_id(cx.cache()) {
1167+
cx.deref_id_map.insert(def_id, id);
1168+
}
1169+
(RenderMode::ForDeref { mut_: deref_mut_ }, derived_id, r#" class="impl-items""#)
11731170
}
11741171
};
11751172
let mut impls_buf = Buffer::html();
@@ -1579,7 +1576,7 @@ fn render_impl(
15791576
kind @ (clean::TyAssocConstItem(generics, ty)
15801577
| clean::AssocConstItem(generics, ty, _)) => {
15811578
let source_id = format!("{item_type}.{name}");
1582-
let id = cx.derive_id(source_id.clone());
1579+
let id = cx.derive_id(&source_id);
15831580
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
15841581
render_rightside(w, cx, item, containing_item, render_mode);
15851582
if trait_.is_some() {
@@ -1605,7 +1602,7 @@ fn render_impl(
16051602
}
16061603
clean::TyAssocTypeItem(generics, bounds) => {
16071604
let source_id = format!("{item_type}.{name}");
1608-
let id = cx.derive_id(source_id.clone());
1605+
let id = cx.derive_id(&source_id);
16091606
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
16101607
if trait_.is_some() {
16111608
// Anchors are only used on trait impls.
@@ -1626,7 +1623,7 @@ fn render_impl(
16261623
}
16271624
clean::AssocTypeItem(tydef, _bounds) => {
16281625
let source_id = format!("{item_type}.{name}");
1629-
let id = cx.derive_id(source_id.clone());
1626+
let id = cx.derive_id(&source_id);
16301627
write!(w, "<section id=\"{id}\" class=\"{item_type}{in_trait_class}\">");
16311628
if trait_.is_some() {
16321629
// Anchors are only used on trait impls.

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
433433
<a href=\"#{id}\">{name}</a>\
434434
</h2>{}",
435435
ITEM_TABLE_OPEN,
436-
id = cx.derive_id(my_section.id().to_owned()),
436+
id = cx.derive_id(my_section.id()),
437437
name = my_section.name(),
438438
);
439439
}

0 commit comments

Comments
 (0)