Skip to content

Commit b7de7a0

Browse files
Return an iterator instead of a Vec in rustdoc Item::attributes
1 parent be181dd commit b7de7a0

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

src/librustdoc/clean/types.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -759,47 +759,45 @@ impl Item {
759759
Some(tcx.visibility(def_id))
760760
}
761761

762-
pub(crate) fn attributes(&self, tcx: TyCtxt<'_>, cache: &Cache, is_json: bool) -> Vec<String> {
762+
pub(crate) fn attributes(
763+
&self,
764+
tcx: TyCtxt<'_>,
765+
cache: &Cache,
766+
is_json: bool,
767+
) -> impl Iterator<Item = String> {
763768
const ALLOWED_ATTRIBUTES: &[Symbol] =
764769
&[sym::export_name, sym::link_section, sym::no_mangle, sym::non_exhaustive];
765770

766771
use rustc_abi::IntegerType;
767772

768-
let mut attrs: Vec<String> = self
769-
.attrs
770-
.other_attrs
771-
.iter()
772-
.filter_map(|attr| {
773-
if is_json {
774-
match attr {
775-
hir::Attribute::Parsed(AttributeKind::Deprecation { .. }) => {
776-
// rustdoc-json stores this in `Item::deprecation`, so we
777-
// don't want it it `Item::attrs`.
778-
None
779-
}
780-
rustc_hir::Attribute::Parsed(rustc_attr_parsing::AttributeKind::Repr(
781-
..,
782-
)) => {
783-
// We have separate pretty-printing logic for `#[repr(..)]` attributes.
784-
// For example, there are circumstances where `#[repr(transparent)]`
785-
// is applied but should not be publicly shown in rustdoc
786-
// because it isn't public API.
787-
None
788-
}
789-
_ => Some(rustc_hir_pretty::attribute_to_string(&tcx, attr)),
773+
let attrs = self.attrs.other_attrs.iter().filter_map(move |attr| {
774+
if is_json {
775+
match attr {
776+
hir::Attribute::Parsed(AttributeKind::Deprecation { .. }) => {
777+
// rustdoc-json stores this in `Item::deprecation`, so we
778+
// don't want it it `Item::attrs`.
779+
None
790780
}
791-
} else if attr.has_any_name(ALLOWED_ATTRIBUTES) {
792-
Some(
793-
rustc_hir_pretty::attribute_to_string(&tcx, attr)
794-
.replace("\\\n", "")
795-
.replace('\n', "")
796-
.replace(" ", " "),
797-
)
798-
} else {
799-
None
781+
rustc_hir::Attribute::Parsed(rustc_attr_parsing::AttributeKind::Repr(..)) => {
782+
// We have separate pretty-printing logic for `#[repr(..)]` attributes.
783+
// For example, there are circumstances where `#[repr(transparent)]`
784+
// is applied but should not be publicly shown in rustdoc
785+
// because it isn't public API.
786+
None
787+
}
788+
_ => Some(rustc_hir_pretty::attribute_to_string(&tcx, attr)),
800789
}
801-
})
802-
.collect();
790+
} else if attr.has_any_name(ALLOWED_ATTRIBUTES) {
791+
Some(
792+
rustc_hir_pretty::attribute_to_string(&tcx, attr)
793+
.replace("\\\n", "")
794+
.replace('\n', "")
795+
.replace(" ", " "),
796+
)
797+
} else {
798+
None
799+
}
800+
});
803801

804802
// Add #[repr(...)]
805803
if let Some(def_id) = self.def_id()
@@ -860,10 +858,10 @@ impl Item {
860858
out.push(&int_s);
861859
}
862860
if !out.is_empty() {
863-
attrs.push(format!("#[repr({})]", out.join(", ")));
861+
return attrs.chain(vec![format!("#[repr({})]", out.join(", "))].into_iter());
864862
}
865863
}
866-
attrs
864+
attrs.chain(Vec::<String>::new().into_iter())
867865
}
868866

869867
pub fn is_doc_hidden(&self) -> bool {

src/librustdoc/json/conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl JsonRenderer<'_> {
4040
})
4141
.collect();
4242
let docs = item.opt_doc_value();
43-
let attrs = item.attributes(self.tcx, self.cache(), true);
43+
let attrs = item.attributes(self.tcx, self.cache(), true).collect::<Vec<_>>();
4444
let span = item.span(self.tcx);
4545
let visibility = item.visibility(self.tcx);
4646
let clean::ItemInner { name, item_id, .. } = *item.inner;

0 commit comments

Comments
 (0)