Skip to content

Commit 408dc50

Browse files
committed
In add_without_unwanted_attributes, return an Iterator instead of receving &mut Vec to populate
Also, don't clone `AttrItem`s if they don't need to be modified
1 parent 12ea832 commit 408dc50

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn generate_item_with_correct_attrs(
198198
|| (is_glob_import(cx.tcx, import_id)
199199
&& (cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id)));
200200
let mut attrs = get_all_import_attributes(cx, import_id, def_id, is_inline);
201-
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
201+
attrs.extend(add_without_unwanted_attributes(target_attrs, is_inline, None));
202202
attrs
203203
} else {
204204
// We only keep the item's attributes.
@@ -2640,7 +2640,7 @@ fn get_all_import_attributes<'hir>(
26402640
first = false;
26412641
// We don't add attributes of an intermediate re-export if it has `#[doc(hidden)]`.
26422642
} else if cx.render_options.document_hidden || !cx.tcx.is_doc_hidden(def_id) {
2643-
add_without_unwanted_attributes(&mut attrs, import_attrs, is_inline, Some(def_id));
2643+
attrs.extend(add_without_unwanted_attributes(import_attrs, is_inline, Some(def_id)));
26442644
}
26452645
}
26462646
attrs
@@ -2719,34 +2719,32 @@ fn filter_doc_attr(args: &mut hir::AttrArgs, is_inline: bool) {
27192719
/// * `doc(no_inline)`
27202720
/// * `doc(hidden)`
27212721
fn add_without_unwanted_attributes<'hir>(
2722-
attrs: &mut Vec<(Cow<'hir, hir::Attribute>, Option<DefId>)>,
27232722
new_attrs: &'hir [hir::Attribute],
27242723
is_inline: bool,
27252724
import_parent: Option<DefId>,
2726-
) {
2727-
for attr in new_attrs {
2725+
) -> impl Iterator<Item = (Cow<'hir, hir::Attribute>, Option<DefId>)> {
2726+
new_attrs.iter().filter_map(move |attr| {
27282727
if attr.is_doc_comment() {
2729-
attrs.push((Cow::Borrowed(attr), import_parent));
2730-
continue;
2728+
return Some((Cow::Borrowed(attr), import_parent));
27312729
}
2732-
let mut attr = attr.clone();
27332730
match attr {
2734-
hir::Attribute::Unparsed(ref mut normal) if let [ident] = &*normal.path.segments => {
2731+
hir::Attribute::Unparsed(normal) if let [ident] = &*normal.path.segments => {
27352732
let ident = ident.name;
27362733
if ident == sym::doc {
2734+
let mut normal = normal.clone();
27372735
filter_doc_attr(&mut normal.args, is_inline);
2738-
attrs.push((Cow::Owned(attr), import_parent));
2736+
Some((Cow::Owned(hir::Attribute::Unparsed(normal)), import_parent))
27392737
} else if is_inline || ident != sym::cfg_trace {
27402738
// If it's not a `cfg()` attribute, we keep it.
2741-
attrs.push((Cow::Owned(attr), import_parent));
2739+
Some((Cow::Borrowed(attr), import_parent))
2740+
} else {
2741+
None
27422742
}
27432743
}
2744-
hir::Attribute::Parsed(..) if is_inline => {
2745-
attrs.push((Cow::Owned(attr), import_parent));
2746-
}
2747-
_ => {}
2744+
hir::Attribute::Parsed(..) if is_inline => Some((Cow::Borrowed(attr), import_parent)),
2745+
_ => None,
27482746
}
2749-
}
2747+
})
27502748
}
27512749

27522750
fn clean_maybe_renamed_item<'tcx>(

0 commit comments

Comments
 (0)