1 //! checks for attributes
4 use rustc_front::hir::*;
6 use syntax::codemap::Span;
8 use syntax::ast::{Attribute, MetaList, MetaWord};
9 use utils::{in_macro, match_path, span_lint};
11 declare_lint! { pub INLINE_ALWAYS, Warn,
12 "`#[inline(always)]` is a bad idea in most cases" }
18 impl LintPass for AttrPass {
19 fn get_lints(&self) -> LintArray {
20 lint_array!(INLINE_ALWAYS)
24 impl LateLintPass for AttrPass {
25 fn check_item(&mut self, cx: &LateContext, item: &Item) {
26 if is_relevant_item(item) {
27 check_attrs(cx, item.span, &item.name, &item.attrs)
31 fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
32 if is_relevant_impl(item) {
33 check_attrs(cx, item.span, &item.name, &item.attrs)
37 fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
38 if is_relevant_trait(item) {
39 check_attrs(cx, item.span, &item.name, &item.attrs)
44 fn is_relevant_item(item: &Item) -> bool {
45 if let &ItemFn(_, _, _, _, _, ref block) = &item.node {
46 is_relevant_block(block)
50 fn is_relevant_impl(item: &ImplItem) -> bool {
52 MethodImplItem(_, ref block) => is_relevant_block(block),
57 fn is_relevant_trait(item: &TraitItem) -> bool {
59 MethodTraitItem(_, None) => true,
60 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
65 fn is_relevant_block(block: &Block) -> bool {
66 for stmt in &block.stmts {
68 StmtDecl(_, _) => return true,
69 StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
70 return is_relevant_expr(expr);
74 block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
77 fn is_relevant_expr(expr: &Expr) -> bool {
79 ExprBlock(ref block) => is_relevant_block(block),
80 ExprRet(Some(ref e)) => is_relevant_expr(e),
81 ExprRet(None) | ExprBreak(_) => false,
82 ExprCall(ref path_expr, _) => {
83 if let ExprPath(_, ref path) = path_expr.node {
84 !match_path(path, &["std", "rt", "begin_unwind"])
91 fn check_attrs(cx: &LateContext, span: Span, name: &Name,
92 attrs: &[Attribute]) {
93 if in_macro(cx, span) { return; }
96 if let MetaList(ref inline, ref values) = attr.node.value.node {
97 if values.len() != 1 || inline != &"inline" { continue; }
98 if let MetaWord(ref always) = values[0].node {
99 if always != &"always" { continue; }
100 span_lint(cx, INLINE_ALWAYS, attr.span, &format!(
101 "you have declared `#[inline(always)]` on `{}`. This \
102 is usually a bad idea",