]> git.lizzy.rs Git - rust.git/blob - src/attrs.rs
Merged #365
[rust.git] / src / attrs.rs
1 //! checks for attributes
2
3 use rustc::lint::*;
4 use rustc_front::hir::*;
5 use reexport::*;
6 use syntax::codemap::Span;
7 use syntax::attr::*;
8 use syntax::ast::{Attribute, MetaList, MetaWord};
9 use utils::{in_macro, match_path, span_lint};
10
11 declare_lint! { pub INLINE_ALWAYS, Warn,
12     "`#[inline(always)]` is a bad idea in most cases" }
13
14
15 #[derive(Copy,Clone)]
16 pub struct AttrPass;
17
18 impl LintPass for AttrPass {
19     fn get_lints(&self) -> LintArray {
20         lint_array!(INLINE_ALWAYS)
21     }
22 }
23
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)
28         }
29     }
30
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)
34         }
35     }
36
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)
40         }
41     }
42 }
43
44 fn is_relevant_item(item: &Item) -> bool {
45     if let &ItemFn(_, _, _, _, _, ref block) = &item.node {
46         is_relevant_block(block)
47     } else { false }
48 }
49
50 fn is_relevant_impl(item: &ImplItem) -> bool {
51     match item.node {
52         MethodImplItem(_, ref block) => is_relevant_block(block),
53         _ => false
54     }
55 }
56
57 fn is_relevant_trait(item: &TraitItem) -> bool {
58     match item.node {
59         MethodTraitItem(_, None) => true,
60         MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
61         _ => false
62     }
63 }
64
65 fn is_relevant_block(block: &Block) -> bool {
66     for stmt in &block.stmts {
67         match stmt.node {
68             StmtDecl(_, _) => return true,
69             StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
70                 return is_relevant_expr(expr);
71             }
72         }
73     }
74     block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
75 }
76
77 fn is_relevant_expr(expr: &Expr) -> bool {
78     match expr.node {
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"])
85             } else { true }
86         }
87         _ => true
88     }
89 }
90
91 fn check_attrs(cx: &LateContext, span: Span, name: &Name,
92         attrs: &[Attribute]) {
93     if in_macro(cx, span) { return; }
94
95     for attr in attrs {
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",
103                     name));
104             }
105         }
106     }
107 }