Skip to content

Commit

Permalink
Prefer to use attr::contains_name() and attr::find_by_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Dec 28, 2017
1 parent 18da3c6 commit d882691
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/librustc/hir/lowering.rs
Expand Up @@ -1571,7 +1571,7 @@ impl<'a> LoweringContext<'a> {
bounds,
default: tp.default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
span: tp.span,
pure_wrt_drop: tp.attrs.iter().any(|attr| attr.check_name("may_dangle")),
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
synthetic: tp.attrs.iter()
.filter(|attr| attr.check_name("rustc_synthetic"))
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
Expand Down Expand Up @@ -1611,7 +1611,7 @@ impl<'a> LoweringContext<'a> {
let def = hir::LifetimeDef {
lifetime: self.lower_lifetime(&l.lifetime),
bounds: self.lower_lifetimes(&l.bounds),
pure_wrt_drop: l.attrs.iter().any(|attr| attr.check_name("may_dangle")),
pure_wrt_drop: attr::contains_name(&l.attrs, "may_dangle"),
in_band: false,
};

Expand Down Expand Up @@ -2331,7 +2331,7 @@ impl<'a> LoweringContext<'a> {
let mut vis = self.lower_visibility(&i.vis, None);
let attrs = self.lower_attrs(&i.attrs);
if let ItemKind::MacroDef(ref def) = i.node {
if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
if !def.legacy || attr::contains_name(&i.attrs, "macro_export") {
let body = self.lower_token_stream(def.stream());
self.exported_macros.push(hir::MacroDef {
name,
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/traits/on_unimplemented.rs
Expand Up @@ -140,9 +140,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
{
let attrs = tcx.get_attrs(impl_def_id);

let attr = if let Some(item) =
attrs.into_iter().find(|a| a.check_name("rustc_on_unimplemented"))
{
let attr = if let Some(item) = attr::find_by_name(&attrs, "rustc_on_unimplemented") {
item
} else {
return Ok(None);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -2402,7 +2402,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

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

/// Returns true if this is an `auto trait`.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/README.md
Expand Up @@ -3,7 +3,7 @@ compiler as a whole, see
[the README.md file found in `librustc`](../librustc/README.md).

The `driver` crate is effectively the "main" function for the rust
compiler. It orchstrates the compilation process and "knits together"
compiler. It orchestrates the compilation process and "knits together"
the code from the other crates within rustc. This crate itself does
not contain any of the "main logic" of the compiler (though it does
have some code related to pretty printing or other minor compiler
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_lint/bad_style.rs
Expand Up @@ -221,9 +221,7 @@ impl LintPass for NonSnakeCase {

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) {
let attr_crate_name = cr.attrs
.iter()
.find(|at| at.check_name("crate_name"))
let attr_crate_name = attr::find_by_name(&cr.attrs, "crate_name")
.and_then(|at| at.value_str().map(|s| (at, s)));
if let Some(ref name) = cx.tcx.sess.opts.crate_name {
self.check_snake_case(cx, "crate", name, None);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/hair/cx/mod.rs
Expand Up @@ -27,6 +27,7 @@ use rustc::ty::subst::Subst;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
use syntax::ast;
use syntax::attr;
use syntax::symbol::Symbol;
use rustc::hir;
use rustc_const_math::{ConstInt, ConstUsize};
Expand Down Expand Up @@ -78,8 +79,7 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
// Some functions always have overflow checks enabled,
// however, they may not get codegen'd, depending on
// the settings for the crate they are translated in.
let mut check_overflow = attrs.iter()
.any(|item| item.check_name("rustc_inherit_overflow_checks"));
let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");

// Respect -C overflow-checks.
check_overflow |= tcx.sess.overflow_checks();
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_passes/ast_validation.rs
Expand Up @@ -51,8 +51,7 @@ impl<'a> AstValidator<'a> {
}

fn invalid_non_exhaustive_attribute(&self, variant: &Variant) {
let has_non_exhaustive = variant.node.attrs.iter()
.any(|attr| attr.check_name("non_exhaustive"));
let has_non_exhaustive = attr::contains_name(&variant.node.attrs, "non_exhaustive");
if has_non_exhaustive {
self.err_handler().span_err(variant.span,
"#[non_exhaustive] is not yet supported on variants");
Expand Down Expand Up @@ -308,7 +307,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
ItemKind::Mod(_) => {
// Ensure that `path` attributes on modules are recorded as used (c.f. #35584).
attr::first_attr_value_str_by_name(&item.attrs, "path");
if item.attrs.iter().any(|attr| attr.check_name("warn_directory_ownership")) {
if attr::contains_name(&item.attrs, "warn_directory_ownership") {
let lint = lint::builtin::LEGACY_DIRECTORY_OWNERSHIP;
let msg = "cannot declare a new module at this location";
self.session.buffer_lint(lint, item.id, item.span, msg);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -358,8 +358,7 @@ impl<'a> Resolver<'a> {

let mut ctor_vis = vis;

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

// If the structure is marked as non_exhaustive then lower the visibility
// to within the crate.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trans_utils/link.rs
Expand Up @@ -13,7 +13,7 @@ use rustc::session::Session;
use rustc::middle::cstore::{self, LinkMeta};
use rustc::hir::svh::Svh;
use std::path::{Path, PathBuf};
use syntax::ast;
use syntax::{ast, attr};
use syntax_pos::Span;

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

if let Some(sess) = sess {
if let Some(ref s) = sess.opts.crate_name {
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/test.rs
Expand Up @@ -386,15 +386,15 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
}

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

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

fn should_panic(i: &ast::Item, cx: &TestCtxt) -> ShouldPanic {
match i.attrs.iter().find(|attr| attr.check_name("should_panic")) {
match attr::find_by_name(&i.attrs, "should_panic") {
Some(attr) => {
let sd = cx.span_diagnostic;
if attr.is_value_str() {
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/proc_macro_registrar.rs
Expand Up @@ -13,6 +13,7 @@ use std::mem;
use errors;

use syntax::ast::{self, Ident, NodeId};
use syntax::attr;
use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute};
use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
Expand Down Expand Up @@ -248,8 +249,7 @@ impl<'a> CollectProcMacros<'a> {
impl<'a> Visitor<'a> for CollectProcMacros<'a> {
fn visit_item(&mut self, item: &'a ast::Item) {
if let ast::ItemKind::MacroDef(..) = item.node {
if self.is_proc_macro_crate &&
item.attrs.iter().any(|attr| attr.path == "macro_export") {
if self.is_proc_macro_crate && attr::contains_name(&item.attrs, "macro_export") {
let msg =
"cannot export macro_rules! macros from a `proc-macro` crate type currently";
self.handler.span_err(item.span, msg);
Expand Down
Expand Up @@ -21,6 +21,7 @@ extern crate rustc;
extern crate rustc_plugin;

use syntax::ast;
use syntax::attr;
use syntax::ext::base::{MultiDecorator, ExtCtxt, Annotatable};
use syntax::ext::build::AstBuilder;
use syntax::symbol::Symbol;
Expand Down Expand Up @@ -80,7 +81,7 @@ fn totalsum_substructure(cx: &mut ExtCtxt, trait_span: Span,
};

fields.iter().fold(cx.expr_isize(trait_span, 0), |acc, ref item| {
if item.attrs.iter().find(|a| a.check_name("ignore")).is_some() {
if attr::contains_name(&item.attrs, "ignore") {
acc
} else {
cx.expr_binary(item.span, ast::BinOpKind::Add, acc,
Expand Down
Expand Up @@ -17,6 +17,7 @@ extern crate rustc_plugin;
extern crate syntax;

use rustc_plugin::Registry;
use syntax::attr;
use syntax::ext::base::*;
use syntax::feature_gate::AttributeType::Whitelisted;
use syntax::symbol::Symbol;
Expand Down Expand Up @@ -59,9 +60,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingWhitelistedAttrPass {
_ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)),
};

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

0 comments on commit d882691

Please sign in to comment.