Skip to content

Commit f4a76f3

Browse files
committed
Prefer to use attr::contains_name() and attr::find_by_name()
1 parent 18da3c6 commit f4a76f3

File tree

13 files changed

+24
-28
lines changed

13 files changed

+24
-28
lines changed

src/librustc/hir/lowering.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ impl<'a> LoweringContext<'a> {
15711571
bounds,
15721572
default: tp.default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
15731573
span: tp.span,
1574-
pure_wrt_drop: tp.attrs.iter().any(|attr| attr.check_name("may_dangle")),
1574+
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
15751575
synthetic: tp.attrs.iter()
15761576
.filter(|attr| attr.check_name("rustc_synthetic"))
15771577
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
@@ -1611,7 +1611,7 @@ impl<'a> LoweringContext<'a> {
16111611
let def = hir::LifetimeDef {
16121612
lifetime: self.lower_lifetime(&l.lifetime),
16131613
bounds: self.lower_lifetimes(&l.bounds),
1614-
pure_wrt_drop: l.attrs.iter().any(|attr| attr.check_name("may_dangle")),
1614+
pure_wrt_drop: attr::contains_name(&l.attrs, "may_dangle"),
16151615
in_band: false,
16161616
};
16171617

@@ -2331,7 +2331,7 @@ impl<'a> LoweringContext<'a> {
23312331
let mut vis = self.lower_visibility(&i.vis, None);
23322332
let attrs = self.lower_attrs(&i.attrs);
23332333
if let ItemKind::MacroDef(ref def) = i.node {
2334-
if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
2334+
if !def.legacy || attr::contains_name(&i.attrs, "macro_export") {
23352335
let body = self.lower_token_stream(def.stream());
23362336
self.exported_macros.push(hir::MacroDef {
23372337
name,

src/librustc/traits/on_unimplemented.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
140140
{
141141
let attrs = tcx.get_attrs(impl_def_id);
142142

143-
let attr = if let Some(item) =
144-
attrs.into_iter().find(|a| a.check_name("rustc_on_unimplemented"))
145-
{
143+
let attr = if let Some(item) = attr::find_by_name(&attrs, "rustc_on_unimplemented") {
146144
item
147145
} else {
148146
return Ok(None);

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24022402

24032403
/// Determine whether an item is annotated with an attribute
24042404
pub fn has_attr(self, did: DefId, attr: &str) -> bool {
2405-
self.get_attrs(did).iter().any(|item| item.check_name(attr))
2405+
attr::contains_name(&self.get_attrs(did), attr)
24062406
}
24072407

24082408
/// Returns true if this is an `auto trait`.

src/librustc_driver/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ compiler as a whole, see
33
[the README.md file found in `librustc`](../librustc/README.md).
44

55
The `driver` crate is effectively the "main" function for the rust
6-
compiler. It orchstrates the compilation process and "knits together"
6+
compiler. It orchestrates the compilation process and "knits together"
77
the code from the other crates within rustc. This crate itself does
88
not contain any of the "main logic" of the compiler (though it does
99
have some code related to pretty printing or other minor compiler

src/librustc_lint/bad_style.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ impl LintPass for NonSnakeCase {
221221

222222
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
223223
fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) {
224-
let attr_crate_name = cr.attrs
225-
.iter()
226-
.find(|at| at.check_name("crate_name"))
224+
let attr_crate_name = attr::find_by_name(&cr.attrs, "crate_name")
227225
.and_then(|at| at.value_str().map(|s| (at, s)));
228226
if let Some(ref name) = cx.tcx.sess.opts.crate_name {
229227
self.check_snake_case(cx, "crate", name, None);

src/librustc_mir/hair/cx/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc::ty::subst::Subst;
2727
use rustc::ty::{self, Ty, TyCtxt};
2828
use rustc::ty::subst::Substs;
2929
use syntax::ast;
30+
use syntax::attr;
3031
use syntax::symbol::Symbol;
3132
use rustc::hir;
3233
use rustc_const_math::{ConstInt, ConstUsize};
@@ -78,8 +79,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
7879
// Some functions always have overflow checks enabled,
7980
// however, they may not get codegen'd, depending on
8081
// the settings for the crate they are translated in.
81-
let mut check_overflow = attrs.iter()
82-
.any(|item| item.check_name("rustc_inherit_overflow_checks"));
82+
let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
8383

8484
// Respect -C overflow-checks.
8585
check_overflow |= tcx.sess.overflow_checks();

src/librustc_passes/ast_validation.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ impl<'a> AstValidator<'a> {
5151
}
5252

5353
fn invalid_non_exhaustive_attribute(&self, variant: &Variant) {
54-
let has_non_exhaustive = variant.node.attrs.iter()
55-
.any(|attr| attr.check_name("non_exhaustive"));
54+
let has_non_exhaustive = attr::contains_name(&variant.node.attrs, "non_exhaustive");
5655
if has_non_exhaustive {
5756
self.err_handler().span_err(variant.span,
5857
"#[non_exhaustive] is not yet supported on variants");
@@ -308,7 +307,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
308307
ItemKind::Mod(_) => {
309308
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
310309
attr::first_attr_value_str_by_name(&item.attrs, "path");
311-
if item.attrs.iter().any(|attr| attr.check_name("warn_directory_ownership")) {
310+
if attr::contains_name(&item.attrs, "warn_directory_ownership") {
312311
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
313312
let msg = "cannot declare a new module at this location";
314313
self.session.buffer_lint(lint, item.id, item.span, msg);

src/librustc_resolve/build_reduced_graph.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ impl<'a> Resolver<'a> {
358358

359359
let mut ctor_vis = vis;
360360

361-
let has_non_exhaustive = item.attrs.iter()
362-
.any(|item| item.check_name("non_exhaustive"));
361+
let has_non_exhaustive = attr::contains_name(&item.attrs, "non_exhaustive");
363362

364363
// If the structure is marked as non_exhaustive then lower the visibility
365364
// to within the crate.

src/librustc_trans_utils/link.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc::session::Session;
1313
use rustc::middle::cstore::{self, LinkMeta};
1414
use rustc::hir::svh::Svh;
1515
use std::path::{Path, PathBuf};
16-
use syntax::ast;
16+
use syntax::{ast, attr};
1717
use syntax_pos::Span;
1818

1919
pub fn out_filename(sess: &Session,
@@ -69,8 +69,8 @@ pub fn find_crate_name(sess: Option<&Session>,
6969
// as used. After doing this, however, we still prioritize a crate name from
7070
// the command line over one found in the #[crate_name] attribute. If we
7171
// find both we ensure that they're the same later on as well.
72-
let attr_crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
73-
.and_then(|at| at.value_str().map(|s| (at, s)));
72+
let attr_crate_name = attr::find_by_name(attrs, "crate_name")
73+
.and_then(|at| at.value_str().map(|s| (at, s)));
7474

7575
if let Some(sess) = sess {
7676
if let Some(ref s) = sess.opts.crate_name {

src/libsyntax/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,15 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
386386
}
387387

388388
fn is_ignored(i: &ast::Item) -> bool {
389-
i.attrs.iter().any(|attr| attr.check_name("ignore"))
389+
attr::contains_name(&i.attrs, "ignore")
390390
}
391391

392392
fn is_allowed_fail(i: &ast::Item) -> bool {
393-
i.attrs.iter().any(|attr| attr.check_name("allow_fail"))
393+
attr::contains_name(&i.attrs, "allow_fail")
394394
}
395395

396396
fn should_panic(i: &ast::Item, cx: &TestCtxt) -> ShouldPanic {
397-
match i.attrs.iter().find(|attr| attr.check_name("should_panic")) {
397+
match attr::find_by_name(&i.attrs, "should_panic") {
398398
Some(attr) => {
399399
let sd = cx.span_diagnostic;
400400
if attr.is_value_str() {

src/libsyntax_ext/proc_macro_registrar.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::mem;
1313
use errors;
1414

1515
use syntax::ast::{self, Ident, NodeId};
16+
use syntax::attr;
1617
use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute};
1718
use syntax::ext::base::ExtCtxt;
1819
use syntax::ext::build::AstBuilder;
@@ -248,8 +249,7 @@ impl<'a> CollectProcMacros<'a> {
248249
impl<'a> Visitor<'a> for CollectProcMacros<'a> {
249250
fn visit_item(&mut self, item: &'a ast::Item) {
250251
if let ast::ItemKind::MacroDef(..) = item.node {
251-
if self.is_proc_macro_crate &&
252-
item.attrs.iter().any(|attr| attr.path == "macro_export") {
252+
if self.is_proc_macro_crate && attr::contains_name(&item.attrs, "macro_export") {
253253
let msg =
254254
"cannot export macro_rules! macros from a `proc-macro` crate type currently";
255255
self.handler.span_err(item.span, msg);

src/test/run-pass-fulldeps/auxiliary/custom_derive_plugin_attr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate rustc;
2121
extern crate rustc_plugin;
2222

2323
use syntax::ast;
24+
use syntax::attr;
2425
use syntax::ext::base::{MultiDecorator, ExtCtxt, Annotatable};
2526
use syntax::ext::build::AstBuilder;
2627
use syntax::symbol::Symbol;
@@ -80,7 +81,7 @@ fn totalsum_substructure(cx: &mut ExtCtxt, trait_span: Span,
8081
};
8182

8283
fields.iter().fold(cx.expr_isize(trait_span, 0), |acc, ref item| {
83-
if item.attrs.iter().find(|a| a.check_name("ignore")).is_some() {
84+
if attr::contains_name(&item.attrs, "ignore") {
8485
acc
8586
} else {
8687
cx.expr_binary(item.span, ast::BinOpKind::Add, acc,

src/test/run-pass-fulldeps/proc-macro/auxiliary/issue-40001-plugin.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern crate rustc_plugin;
1717
extern crate syntax;
1818

1919
use rustc_plugin::Registry;
20+
use syntax::attr;
2021
use syntax::ext::base::*;
2122
use syntax::feature_gate::AttributeType::Whitelisted;
2223
use syntax::symbol::Symbol;
@@ -59,7 +60,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingWhitelistedAttrPass {
5960
_ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)),
6061
};
6162

62-
if !item.attrs.iter().any(|a| a.check_name("whitelisted_attr")) {
63+
if attr::contains_name(&item.attrs, "whitelisted_attr") {
6364
cx.span_lint(MISSING_WHITELISTED_ATTR, span,
6465
"Missing 'whitelited_attr' attribute");
6566
}

0 commit comments

Comments
 (0)