From 77838d6ba78c23fdc337da7ece974beda95280df Mon Sep 17 00:00:00 2001 From: llogiq Date: Sat, 30 May 2015 15:10:19 +0200 Subject: [PATCH] New lint for issue #72 --- src/attrs.rs | 48 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ tests/compile-fail/attrs.rs | 12 ++++++++++ 3 files changed, 63 insertions(+) create mode 100644 src/attrs.rs create mode 100644 tests/compile-fail/attrs.rs diff --git a/src/attrs.rs b/src/attrs.rs new file mode 100644 index 00000000000..3ad3889f9db --- /dev/null +++ b/src/attrs.rs @@ -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())); + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 92cc7ef254c..918f8813cd2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, ]); } diff --git a/tests/compile-fail/attrs.rs b/tests/compile-fail/attrs.rs new file mode 100644 index 00000000000..30ce191d3db --- /dev/null +++ b/tests/compile-fail/attrs.rs @@ -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() +}