Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1.9 KiB
Rust
Raw Normal View History

2021-11-24 10:20:23 +01:00
use rustc_ast::{Attribute, MetaItem};
use rustc_expand::base::{Annotatable, ExtCtxt};
2019-11-30 02:34:18 +01:00
use rustc_feature::AttributeTemplate;
2021-11-24 10:20:23 +01:00
use rustc_lint_defs::builtin::DUPLICATE_MACRO_ATTRIBUTES;
2019-10-08 14:15:26 +02:00
use rustc_parse::validate_attr;
use rustc_span::Symbol;
2019-10-08 14:15:26 +02:00
pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) {
// All the built-in macro attributes are "words" at the moment.
2019-12-30 21:38:43 +03:00
let template = AttributeTemplate { word: true, ..Default::default() };
2019-10-08 14:15:26 +02:00
let attr = ecx.attribute(meta_item.clone());
validate_attr::check_builtin_attribute(&ecx.sess.parse_sess, &attr, name, template);
2019-10-08 14:15:26 +02:00
}
2021-11-24 10:20:23 +01:00
/// Emit a warning if the item is annotated with the given attribute. This is used to diagnose when
/// an attribute may have been mistakenly duplicated.
pub fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable, name: Symbol) {
let attrs: Option<&[Attribute]> = match item {
Annotatable::Item(item) => Some(&item.attrs),
Annotatable::TraitItem(item) => Some(&item.attrs),
Annotatable::ImplItem(item) => Some(&item.attrs),
Annotatable::ForeignItem(item) => Some(&item.attrs),
Annotatable::Expr(expr) => Some(&expr.attrs),
Annotatable::Arm(arm) => Some(&arm.attrs),
Annotatable::ExprField(field) => Some(&field.attrs),
Annotatable::PatField(field) => Some(&field.attrs),
Annotatable::GenericParam(param) => Some(&param.attrs),
Annotatable::Param(param) => Some(&param.attrs),
Annotatable::FieldDef(def) => Some(&def.attrs),
Annotatable::Variant(variant) => Some(&variant.attrs),
_ => None,
};
if let Some(attrs) = attrs {
if let Some(attr) = ecx.sess.find_by_name(attrs, name) {
ecx.parse_sess().buffer_lint(
DUPLICATE_MACRO_ATTRIBUTES,
attr.span,
ecx.current_expansion.lint_node_id,
"duplicated attribute",
);
}
}
}