Skip to content

Commit 5ccd6c0

Browse files
committed
rustdoc: Extract AttributesExt::lists trait method as function
The two implementations were identical, so there's no need to use a trait method.
1 parent e2fcdb8 commit 5ccd6c0

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

src/librustdoc/clean/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ fn generate_item_with_correct_attrs(
186186
// For glob re-exports the item may or may not exist to be re-exported (potentially the cfgs
187187
// on the path up until the glob can be removed, and only cfgs on the globbed item itself
188188
// matter), for non-inlined re-exports see #85043.
189-
let is_inline = inline::load_attrs(cx, import_id.to_def_id())
190-
.lists(sym::doc)
189+
let is_inline = hir_attr_lists(inline::load_attrs(cx, import_id.to_def_id()), sym::doc)
191190
.get_word_attr(sym::inline)
192191
.is_some()
193192
|| (is_glob_import(cx.tcx, import_id)
@@ -979,13 +978,14 @@ fn clean_proc_macro<'tcx>(
979978
) -> ItemKind {
980979
let attrs = cx.tcx.hir().attrs(item.hir_id());
981980
if kind == MacroKind::Derive
982-
&& let Some(derive_name) = attrs.lists(sym::proc_macro_derive).find_map(|mi| mi.ident())
981+
&& let Some(derive_name) =
982+
hir_attr_lists(attrs, sym::proc_macro_derive).find_map(|mi| mi.ident())
983983
{
984984
*name = derive_name.name;
985985
}
986986

987987
let mut helpers = Vec::new();
988-
for mi in attrs.lists(sym::proc_macro_derive) {
988+
for mi in hir_attr_lists(attrs, sym::proc_macro_derive) {
989989
if !mi.has_name(sym::attributes) {
990990
continue;
991991
}
@@ -2985,7 +2985,7 @@ fn clean_use_statement_inner<'tcx>(
29852985

29862986
let visibility = cx.tcx.visibility(import.owner_id);
29872987
let attrs = cx.tcx.hir().attrs(import.hir_id());
2988-
let inline_attr = attrs.lists(sym::doc).get_word_attr(sym::inline);
2988+
let inline_attr = hir_attr_lists(attrs, sym::doc).get_word_attr(sym::inline);
29892989
let pub_underscore = visibility.is_public() && name == kw::Underscore;
29902990
let current_mod = cx.tcx.parent_module_from_def_id(import.owner_id.def_id);
29912991
let import_def_id = import.owner_id.def_id;

src/librustdoc/clean/types.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -980,18 +980,24 @@ pub(crate) struct Module {
980980
}
981981

982982
pub(crate) trait AttributesExt {
983-
type AttributeIterator<'a>: Iterator<Item = ast::MetaItemInner>
984-
where
985-
Self: 'a;
986983
type Attributes<'a>: Iterator<Item = &'a hir::Attribute>
987984
where
988985
Self: 'a;
989986

990-
fn lists(&self, name: Symbol) -> Self::AttributeIterator<'_>;
991-
992987
fn iter(&self) -> Self::Attributes<'_>;
993988
}
994989

990+
pub fn hir_attr_lists<A: AttributesExt + ?Sized>(
991+
attrs: &A,
992+
name: Symbol,
993+
) -> impl Iterator<Item = ast::MetaItemInner> + use<'_, A> {
994+
attrs
995+
.iter()
996+
.filter(move |attr| attr.has_name(name))
997+
.filter_map(ast::attr::AttributeExt::meta_item_list)
998+
.flatten()
999+
}
1000+
9951001
pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
9961002
attrs: &A,
9971003
tcx: TyCtxt<'_>,
@@ -1066,7 +1072,7 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
10661072

10671073
// treat #[target_feature(enable = "feat")] attributes as if they were
10681074
// #[doc(cfg(target_feature = "feat"))] attributes as well
1069-
for attr in attrs.lists(sym::target_feature) {
1075+
for attr in hir_attr_lists(attrs, sym::target_feature) {
10701076
if attr.has_name(sym::enable) {
10711077
if attr.value_str().is_some() {
10721078
// Clone `enable = "feat"`, change to `target_feature = "feat"`.
@@ -1085,38 +1091,19 @@ pub fn extract_cfg_from_attrs<A: AttributesExt + ?Sized>(
10851091
}
10861092

10871093
impl AttributesExt for [hir::Attribute] {
1088-
type AttributeIterator<'a> = impl Iterator<Item = ast::MetaItemInner> + 'a;
10891094
type Attributes<'a> = impl Iterator<Item = &'a hir::Attribute> + 'a;
10901095

1091-
fn lists(&self, name: Symbol) -> Self::AttributeIterator<'_> {
1092-
self.iter()
1093-
.filter(move |attr| attr.has_name(name))
1094-
.filter_map(ast::attr::AttributeExt::meta_item_list)
1095-
.flatten()
1096-
}
1097-
10981096
fn iter(&self) -> Self::Attributes<'_> {
10991097
self.iter()
11001098
}
11011099
}
11021100

11031101
impl AttributesExt for [(Cow<'_, hir::Attribute>, Option<DefId>)] {
1104-
type AttributeIterator<'a>
1105-
= impl Iterator<Item = ast::MetaItemInner> + 'a
1106-
where
1107-
Self: 'a;
11081102
type Attributes<'a>
11091103
= impl Iterator<Item = &'a hir::Attribute> + 'a
11101104
where
11111105
Self: 'a;
11121106

1113-
fn lists(&self, name: Symbol) -> Self::AttributeIterator<'_> {
1114-
AttributesExt::iter(self)
1115-
.filter(move |attr| attr.has_name(name))
1116-
.filter_map(hir::Attribute::meta_item_list)
1117-
.flatten()
1118-
}
1119-
11201107
fn iter(&self) -> Self::Attributes<'_> {
11211108
self.iter().map(move |(attr, _)| match attr {
11221109
Cow::Borrowed(attr) => *attr,
@@ -1188,7 +1175,7 @@ pub(crate) struct Attributes {
11881175

11891176
impl Attributes {
11901177
pub(crate) fn lists(&self, name: Symbol) -> impl Iterator<Item = ast::MetaItemInner> + '_ {
1191-
self.other_attrs.lists(name)
1178+
hir_attr_lists(&self.other_attrs[..], name)
11921179
}
11931180

11941181
pub(crate) fn has_doc_flag(&self, flag: Symbol) -> bool {
@@ -1255,7 +1242,9 @@ impl Attributes {
12551242
pub(crate) fn get_doc_aliases(&self) -> Box<[Symbol]> {
12561243
let mut aliases = FxIndexSet::default();
12571244

1258-
for attr in self.other_attrs.lists(sym::doc).filter(|a| a.has_name(sym::alias)) {
1245+
for attr in
1246+
hir_attr_lists(&self.other_attrs[..], sym::doc).filter(|a| a.has_name(sym::alias))
1247+
{
12591248
if let Some(values) = attr.meta_item_list() {
12601249
for l in values {
12611250
if let Some(lit) = l.lit()

src/librustdoc/visit_ast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use tracing::debug;
1919

2020
use crate::clean::cfg::Cfg;
2121
use crate::clean::utils::{inherits_doc_hidden, should_ignore_res};
22-
use crate::clean::{AttributesExt, NestedAttributesExt, reexport_chain};
22+
use crate::clean::{NestedAttributesExt, hir_attr_lists, reexport_chain};
2323
use crate::core;
2424

2525
/// This module is used to store stuff from Rust's AST in a more convenient
@@ -247,8 +247,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
247247
let document_hidden = self.cx.render_options.document_hidden;
248248
let use_attrs = tcx.hir().attrs(tcx.local_def_id_to_hir_id(def_id));
249249
// Don't inline `doc(hidden)` imports so they can be stripped at a later stage.
250-
let is_no_inline = use_attrs.lists(sym::doc).has_word(sym::no_inline)
251-
|| (document_hidden && use_attrs.lists(sym::doc).has_word(sym::hidden));
250+
let is_no_inline = hir_attr_lists(use_attrs, sym::doc).has_word(sym::no_inline)
251+
|| (document_hidden && hir_attr_lists(use_attrs, sym::doc).has_word(sym::hidden));
252252

253253
if is_no_inline {
254254
return false;

0 commit comments

Comments
 (0)