Skip to content

Commit 278f4b2

Browse files
committed
Don't clone new_item in after_krate.
We can avoid it by using the `entry` API, which lets us do the `assert_eq` comparison before `new_item` is consumed.
1 parent 79b3c08 commit 278f4b2

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/librustdoc/json/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
217217
/// the hashmap because certain items (traits and types) need to have their mappings for trait
218218
/// implementations filled out before they're inserted.
219219
fn item(&mut self, item: &clean::Item) -> Result<(), Error> {
220+
use std::collections::hash_map::Entry;
221+
220222
let item_type = item.type_();
221223
let item_name = item.name;
222224
trace!("rendering {item_type} {item_name:?}");
@@ -271,18 +273,25 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
271273
| types::ItemEnum::Macro(_)
272274
| types::ItemEnum::ProcMacro(_) => false,
273275
};
274-
let removed = self.index.insert(new_item.id, new_item.clone());
275276

276277
// FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
277278
// to make sure the items are unique. The main place this happens is when an item, is
278279
// reexported in more than one place. See `rustdoc-json/reexport/in_root_and_mod`
279-
if let Some(old_item) = removed {
280-
// In case of generic implementations (like `impl<T> Trait for T {}`), all the
281-
// inner items will be duplicated so we can ignore if they are slightly different.
282-
if !can_be_ignored {
283-
assert_eq!(old_item, new_item);
280+
match self.index.entry(new_item.id) {
281+
Entry::Vacant(entry) => {
282+
entry.insert(new_item);
283+
}
284+
Entry::Occupied(mut entry) => {
285+
// In case of generic implementations (like `impl<T> Trait for T {}`), all the
286+
// inner items will be duplicated so we can ignore if they are slightly
287+
// different.
288+
let old_item = entry.get_mut();
289+
if !can_be_ignored {
290+
assert_eq!(*old_item, new_item);
291+
}
292+
trace!("replaced {old_item:?}\nwith {new_item:?}");
293+
*old_item = new_item;
284294
}
285-
trace!("replaced {old_item:?}\nwith {new_item:?}");
286295
}
287296
}
288297

0 commit comments

Comments
 (0)