parent
0d651c72ff
commit
77838d6ba7
48
src/attrs.rs
Normal file
48
src/attrs.rs
Normal file
@ -0,0 +1,48 @@
|
||||
/// checks for attributes
|
||||
|
||||
use rustc::plugin::Registry;
|
||||
use rustc::lint::*;
|
||||
use syntax::ast::*;
|
||||
use syntax::ptr::P;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::parse::token::InternedString;
|
||||
|
||||
declare_lint! { pub INLINE_ALWAYS, Warn,
|
||||
"#[inline(always)] is usually a bad idea."}
|
||||
|
||||
|
||||
#[derive(Copy,Clone)]
|
||||
pub struct AttrPass;
|
||||
|
||||
impl LintPass for AttrPass {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(INLINE_ALWAYS)
|
||||
}
|
||||
|
||||
fn check_item(&mut self, cx: &Context, item: &Item) {
|
||||
check_attrs(cx, &item.ident, &item.attrs)
|
||||
}
|
||||
|
||||
fn check_impl_item(&mut self, cx: &Context, item: &ImplItem) {
|
||||
check_attrs(cx, &item.ident, &item.attrs)
|
||||
}
|
||||
|
||||
fn check_trait_item(&mut self, cx: &Context, item: &TraitItem) {
|
||||
check_attrs(cx, &item.ident, &item.attrs)
|
||||
}
|
||||
}
|
||||
|
||||
fn check_attrs(cx: &Context, ident: &Ident, attrs: &[Attribute]) {
|
||||
for attr in attrs {
|
||||
if let MetaList(ref inline, ref values) = attr.node.value.node {
|
||||
if values.len() != 1 || inline != &"inline" { continue; }
|
||||
if let MetaWord(ref always) = values[0].node {
|
||||
if always != &"always" { continue; }
|
||||
cx.span_lint(INLINE_ALWAYS, attr.span, &format!(
|
||||
"You have declared #[inline(always)] on {}. This \
|
||||
is usually a bad idea. Are you sure?",
|
||||
ident.as_str()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ pub mod eta_reduction;
|
||||
pub mod identity_op;
|
||||
pub mod mut_mut;
|
||||
pub mod len_zero;
|
||||
pub mod attrs;
|
||||
|
||||
#[plugin_registrar]
|
||||
pub fn plugin_registrar(reg: &mut Registry) {
|
||||
@ -44,6 +45,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
||||
reg.register_lint_pass(box mut_mut::MutMut as LintPassObject);
|
||||
reg.register_lint_pass(box len_zero::LenZero as LintPassObject);
|
||||
reg.register_lint_pass(box misc::CmpOwned as LintPassObject);
|
||||
reg.register_lint_pass(box attrs::AttrPass as LintPassObject);
|
||||
|
||||
reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST,
|
||||
misc::SINGLE_MATCH, misc::STR_TO_STRING,
|
||||
@ -60,5 +62,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
||||
mut_mut::MUT_MUT,
|
||||
len_zero::LEN_ZERO,
|
||||
len_zero::LEN_WITHOUT_IS_EMPTY,
|
||||
attrs::INLINE_ALWAYS,
|
||||
]);
|
||||
}
|
||||
|
12
tests/compile-fail/attrs.rs
Normal file
12
tests/compile-fail/attrs.rs
Normal file
@ -0,0 +1,12 @@
|
||||
#![feature(plugin)]
|
||||
#![plugin(clippy)]
|
||||
|
||||
#[deny(inline_always)]
|
||||
#[inline(always)] //~ERROR You have declared #[inline(always)] on test_attr_lint.
|
||||
fn test_attr_lint() {
|
||||
assert!(true)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test_attr_lint()
|
||||
}
|
Loading…
Reference in New Issue
Block a user