Skip to content

Commit 262fce4

Browse files
authored
Rollup merge of rust-lang#75043 - petrochenkov:hasname, r=nnethercote
rustc_ast: `(Nested)MetaItem::check_name` -> `has_name` For consistency with `Attribute::has_name` which doesn't mark the attribute as used either. Replace all uses of `check_name` with `has_name` outside of rustc, only rustc needs to mark attributes as used. cc rust-lang#74932 r? @nnethercote
2 parents cc0ac7e + 05f414b commit 262fce4

File tree

32 files changed

+94
-89
lines changed

32 files changed

+94
-89
lines changed

src/librustc_ast/attr/mod.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ impl NestedMetaItem {
100100
}
101101

102102
/// Returns `true` if this list item is a MetaItem with a name of `name`.
103-
pub fn check_name(&self, name: Symbol) -> bool {
104-
self.meta_item().map_or(false, |meta_item| meta_item.check_name(name))
103+
pub fn has_name(&self, name: Symbol) -> bool {
104+
self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
105105
}
106106

107107
/// For a single-segment meta item, returns its name; otherwise, returns `None`.
@@ -173,8 +173,13 @@ impl Attribute {
173173
}
174174
}
175175

176-
/// Returns `true` if the attribute's path matches the argument. If it matches, then the
177-
/// attribute is marked as used.
176+
/// Returns `true` if the attribute's path matches the argument.
177+
/// If it matches, then the attribute is marked as used.
178+
/// Should only be used by rustc, other tools can use `has_name` instead,
179+
/// because only rustc is supposed to report the `unused_attributes` lint.
180+
/// `MetaItem` and `NestedMetaItem` are produced by "lowering" an `Attribute`
181+
/// and don't have identity, so they only has the `has_name` method,
182+
/// and you need to mark the original `Attribute` as used when necessary.
178183
pub fn check_name(&self, name: Symbol) -> bool {
179184
let matches = self.has_name(name);
180185
if matches {
@@ -278,7 +283,7 @@ impl MetaItem {
278283
}
279284
}
280285

281-
pub fn check_name(&self, name: Symbol) -> bool {
286+
pub fn has_name(&self, name: Symbol) -> bool {
282287
self.path == name
283288
}
284289

@@ -405,7 +410,7 @@ pub fn mk_doc_comment(style: AttrStyle, comment: Symbol, span: Span) -> Attribut
405410
}
406411

407412
pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
408-
items.iter().any(|item| item.check_name(name))
413+
items.iter().any(|item| item.has_name(name))
409414
}
410415

411416
pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {

src/librustc_ast_passes/feature_gate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
243243
if attr.check_name(sym::doc) {
244244
for nested_meta in attr.meta_item_list().unwrap_or_default() {
245245
macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
246-
$(if nested_meta.check_name(sym::$name) {
246+
$(if nested_meta.has_name(sym::$name) {
247247
let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
248248
gate_feature_post!(self, $feature, attr.span, msg);
249249
})*
@@ -314,7 +314,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
314314
ast::ItemKind::Struct(..) => {
315315
for attr in attr::filter_by_name(&i.attrs[..], sym::repr) {
316316
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
317-
if item.check_name(sym::simd) {
317+
if item.has_name(sym::simd) {
318318
gate_feature_post!(
319319
&self,
320320
repr_simd,

src/librustc_attr/builtin.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
9292
if let Some(meta) = attr.meta() {
9393
if let MetaItemKind::List(items) = meta.kind {
9494
if items.len() == 1 {
95-
if items[0].check_name(sym::allowed) {
95+
if items[0].has_name(sym::allowed) {
9696
return Some(UnwindAttr::Allowed);
97-
} else if items[0].check_name(sym::aborts) {
97+
} else if items[0].has_name(sym::aborts) {
9898
return Some(UnwindAttr::Aborts);
9999
}
100100
}
@@ -168,7 +168,7 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool
168168
item.check_name(sym::feature)
169169
&& item
170170
.meta_item_list()
171-
.map(|list| list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name)))
171+
.map(|list| list.iter().any(|mi| mi.is_word() && mi.has_name(feature_name)))
172172
.unwrap_or(false)
173173
})
174174
}
@@ -505,7 +505,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
505505
}
506506

507507
fn try_gate_cfg(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) {
508-
let gate = find_gated_cfg(|sym| cfg.check_name(sym));
508+
let gate = find_gated_cfg(|sym| cfg.has_name(sym));
509509
if let (Some(feats), Some(gated_cfg)) = (features, gate) {
510510
gate_cfg(&gated_cfg, cfg.span, sess, feats);
511511
}
@@ -898,7 +898,7 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
898898
}
899899
} else {
900900
if let Some(meta_item) = item.meta_item() {
901-
if meta_item.check_name(sym::align) {
901+
if meta_item.has_name(sym::align) {
902902
if let MetaItemKind::NameValue(ref value) = meta_item.kind {
903903
recognised = true;
904904
let mut err = struct_span_err!(

src/librustc_builtin_macros/proc_macro_harness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a> CollectProcMacros<'a> {
143143

144144
let attributes_attr = list.get(1);
145145
let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr {
146-
if !attr.check_name(sym::attributes) {
146+
if !attr.has_name(sym::attributes) {
147147
self.handler.span_err(attr.span(), "second argument must be `attributes`")
148148
}
149149
attr.meta_item_list()

src/librustc_builtin_macros/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
336336
Some(list) => {
337337
let msg = list
338338
.iter()
339-
.find(|mi| mi.check_name(sym::expected))
339+
.find(|mi| mi.has_name(sym::expected))
340340
.and_then(|mi| mi.meta_item())
341341
.and_then(|mi| mi.value_str());
342342
if list.len() != 1 || msg.is_none() {

src/librustc_expand/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1644,14 +1644,14 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
16441644
}
16451645

16461646
if let Some(list) = at.meta_item_list() {
1647-
if !list.iter().any(|it| it.check_name(sym::include)) {
1647+
if !list.iter().any(|it| it.has_name(sym::include)) {
16481648
return noop_visit_attribute(at, self);
16491649
}
16501650

16511651
let mut items = vec![];
16521652

16531653
for mut it in list {
1654-
if !it.check_name(sym::include) {
1654+
if !it.has_name(sym::include) {
16551655
items.push({
16561656
noop_visit_meta_list_item(&mut it, self);
16571657
it

src/librustc_incremental/assert_module_sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl AssertModuleSource<'tcx> {
149149

150150
fn field(&self, attr: &ast::Attribute, name: Symbol) -> Symbol {
151151
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
152-
if item.check_name(name) {
152+
if item.has_name(name) {
153153
if let Some(value) = item.value_str() {
154154
return value;
155155
} else {

src/librustc_incremental/persist/dirty_clean.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl DirtyCleanVisitor<'tcx> {
231231

232232
fn labels(&self, attr: &Attribute) -> Option<Labels> {
233233
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
234-
if item.check_name(LABEL) {
234+
if item.has_name(LABEL) {
235235
let value = expect_associated_value(self.tcx, &item);
236236
return Some(self.resolve_labels(&item, value));
237237
}
@@ -242,7 +242,7 @@ impl DirtyCleanVisitor<'tcx> {
242242
/// `except=` attribute value
243243
fn except(&self, attr: &Attribute) -> Labels {
244244
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
245-
if item.check_name(EXCEPT) {
245+
if item.has_name(EXCEPT) {
246246
let value = expect_associated_value(self.tcx, &item);
247247
return self.resolve_labels(&item, value);
248248
}
@@ -474,15 +474,15 @@ fn check_config(tcx: TyCtxt<'_>, attr: &Attribute) -> bool {
474474
debug!("check_config: config={:?}", config);
475475
let (mut cfg, mut except, mut label) = (None, false, false);
476476
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
477-
if item.check_name(CFG) {
477+
if item.has_name(CFG) {
478478
let value = expect_associated_value(tcx, &item);
479479
debug!("check_config: searching for cfg {:?}", value);
480480
cfg = Some(config.contains(&(value, None)));
481481
}
482-
if item.check_name(LABEL) {
482+
if item.has_name(LABEL) {
483483
label = true;
484484
}
485-
if item.check_name(EXCEPT) {
485+
if item.has_name(EXCEPT) {
486486
except = true;
487487
}
488488
}

src/librustc_lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn has_doc(attr: &ast::Attribute) -> bool {
330330

331331
if let Some(list) = attr.meta_item_list() {
332332
for meta in list {
333-
if meta.check_name(sym::include) || meta.check_name(sym::hidden) {
333+
if meta.has_name(sym::include) || meta.has_name(sym::hidden) {
334334
return true;
335335
}
336336
}

src/librustc_metadata/native_libs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
5858
let mut kind_specified = false;
5959

6060
for item in items.iter() {
61-
if item.check_name(sym::kind) {
61+
if item.has_name(sym::kind) {
6262
kind_specified = true;
6363
let kind = match item.value_str() {
6464
Some(name) => name,
@@ -84,9 +84,9 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
8484
NativeLibKind::Unspecified
8585
}
8686
};
87-
} else if item.check_name(sym::name) {
87+
} else if item.has_name(sym::name) {
8888
lib.name = item.value_str();
89-
} else if item.check_name(sym::cfg) {
89+
} else if item.has_name(sym::cfg) {
9090
let cfg = match item.meta_item_list() {
9191
Some(list) => list,
9292
None => continue, // skip like historical compilers
@@ -98,7 +98,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
9898
} else {
9999
self.tcx.sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`");
100100
}
101-
} else if item.check_name(sym::wasm_import_module) {
101+
} else if item.has_name(sym::wasm_import_module) {
102102
match item.value_str() {
103103
Some(s) => lib.wasm_import_module = Some(s),
104104
None => {

src/librustc_mir/dataflow/framework/engine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl RustcMirAttrs {
339339
.flat_map(|attr| attr.meta_item_list().into_iter().flat_map(|v| v.into_iter()));
340340

341341
for attr in rustc_mir_attrs {
342-
let attr_result = if attr.check_name(sym::borrowck_graphviz_postflow) {
342+
let attr_result = if attr.has_name(sym::borrowck_graphviz_postflow) {
343343
Self::set_field(&mut ret.basename_and_suffix, tcx, &attr, |s| {
344344
let path = PathBuf::from(s.to_string());
345345
match path.file_name() {
@@ -350,7 +350,7 @@ impl RustcMirAttrs {
350350
}
351351
}
352352
})
353-
} else if attr.check_name(sym::borrowck_graphviz_format) {
353+
} else if attr.has_name(sym::borrowck_graphviz_format) {
354354
Self::set_field(&mut ret.formatter, tcx, &attr, |s| match s {
355355
sym::gen_kill | sym::two_phase => Ok(s),
356356
_ => {

src/librustc_mir/dataflow/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn has_rustc_mir_with(attrs: &[ast::Attribute], name: Symbol) -> Opti
3434
let items = attr.meta_item_list();
3535
for item in items.iter().flat_map(|l| l.iter()) {
3636
match item.meta_item() {
37-
Some(mi) if mi.check_name(name) => return Some(mi.clone()),
37+
Some(mi) if mi.has_name(name) => return Some(mi.clone()),
3838
_ => continue,
3939
}
4040
}

src/librustc_passes/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl CheckAttrVisitor<'tcx> {
222222
if let Some(mi) = attr.meta() {
223223
if let Some(list) = mi.meta_item_list() {
224224
for meta in list {
225-
if meta.check_name(sym::alias) {
225+
if meta.has_name(sym::alias) {
226226
if !meta.is_value_str()
227227
|| meta
228228
.value_str()

src/librustc_save_analysis/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,10 @@ impl<'tcx> SaveContext<'tcx> {
832832
if let Some(meta_list) = attr.meta_item_list() {
833833
meta_list
834834
.into_iter()
835-
.filter(|it| it.check_name(sym::include))
835+
.filter(|it| it.has_name(sym::include))
836836
.filter_map(|it| it.meta_item_list().map(|l| l.to_owned()))
837837
.flat_map(|it| it)
838-
.filter(|meta| meta.check_name(sym::contents))
838+
.filter(|meta| meta.has_name(sym::contents))
839839
.filter_map(|meta| meta.value_str())
840840
.for_each(|val| {
841841
result.push_str(&val.as_str());

src/librustc_trait_selection/traits/on_unimplemented.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,27 @@ impl<'tcx> OnUnimplementedDirective {
9595
};
9696

9797
for item in item_iter {
98-
if item.check_name(sym::message) && message.is_none() {
98+
if item.has_name(sym::message) && message.is_none() {
9999
if let Some(message_) = item.value_str() {
100100
message = parse_value(message_)?;
101101
continue;
102102
}
103-
} else if item.check_name(sym::label) && label.is_none() {
103+
} else if item.has_name(sym::label) && label.is_none() {
104104
if let Some(label_) = item.value_str() {
105105
label = parse_value(label_)?;
106106
continue;
107107
}
108-
} else if item.check_name(sym::note) && note.is_none() {
108+
} else if item.has_name(sym::note) && note.is_none() {
109109
if let Some(note_) = item.value_str() {
110110
note = parse_value(note_)?;
111111
continue;
112112
}
113-
} else if item.check_name(sym::enclosing_scope) && enclosing_scope.is_none() {
113+
} else if item.has_name(sym::enclosing_scope) && enclosing_scope.is_none() {
114114
if let Some(enclosing_scope_) = item.value_str() {
115115
enclosing_scope = parse_value(enclosing_scope_)?;
116116
continue;
117117
}
118-
} else if item.check_name(sym::on)
118+
} else if item.has_name(sym::on)
119119
&& is_root
120120
&& message.is_none()
121121
&& label.is_none()

src/librustc_typeck/collect.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ fn from_target_feature(
22312231
let rust_features = tcx.features();
22322232
for item in list {
22332233
// Only `enable = ...` is accepted in the meta-item list.
2234-
if !item.check_name(sym::enable) {
2234+
if !item.has_name(sym::enable) {
22352235
bad_item(item.span());
22362236
continue;
22372237
}
@@ -2483,11 +2483,11 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
24832483
no_sanitize_span = Some(attr.span);
24842484
if let Some(list) = attr.meta_item_list() {
24852485
for item in list.iter() {
2486-
if item.check_name(sym::address) {
2486+
if item.has_name(sym::address) {
24872487
codegen_fn_attrs.no_sanitize |= SanitizerSet::ADDRESS;
2488-
} else if item.check_name(sym::memory) {
2488+
} else if item.has_name(sym::memory) {
24892489
codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY;
2490-
} else if item.check_name(sym::thread) {
2490+
} else if item.has_name(sym::thread) {
24912491
codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD;
24922492
} else {
24932493
tcx.sess

src/librustdoc/clean/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl Clean<ExternalCrate> for CrateNum {
113113
let mut prim = None;
114114
for attr in attrs.lists(sym::doc) {
115115
if let Some(v) = attr.value_str() {
116-
if attr.check_name(sym::primitive) {
116+
if attr.has_name(sym::primitive) {
117117
prim = PrimitiveType::from_symbol(v);
118118
if prim.is_some() {
119119
break;
@@ -168,7 +168,7 @@ impl Clean<ExternalCrate> for CrateNum {
168168
let mut keyword = None;
169169
for attr in attrs.lists(sym::doc) {
170170
if let Some(v) = attr.value_str() {
171-
if attr.check_name(sym::keyword) {
171+
if attr.has_name(sym::keyword) {
172172
if v.is_doc_keyword() {
173173
keyword = Some(v.to_string());
174174
break;
@@ -2157,7 +2157,7 @@ impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
21572157
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
21582158
let please_inline = self.vis.node.is_pub()
21592159
&& self.attrs.iter().any(|a| {
2160-
a.check_name(sym::doc)
2160+
a.has_name(sym::doc)
21612161
&& match a.meta_item_list() {
21622162
Some(l) => attr::list_contains_name(&l, sym::inline),
21632163
None => false,
@@ -2197,7 +2197,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
21972197
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
21982198
let mut denied = !self.vis.node.is_pub()
21992199
|| self.attrs.iter().any(|a| {
2200-
a.check_name(sym::doc)
2200+
a.has_name(sym::doc)
22012201
&& match a.meta_item_list() {
22022202
Some(l) => {
22032203
attr::list_contains_name(&l, sym::no_inline)

0 commit comments

Comments
 (0)