]> git.lizzy.rs Git - rust.git/blob - src/attrs.rs
ec2cfcb0efc2819f276592c4feb71c858bb9d63b
[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, BEGIN_UNWIND};
10
11 /// **What it does:** This lint `Warn`s on items annotated with `#[inline(always)]`, unless the annotated function is empty or simply panics.
12 ///
13 /// **Why is this bad?** While there are valid uses of this annotation (and once you know when to use it, by all means `allow` this lint), it's a common newbie-mistake to pepper one's code with it.
14 ///
15 /// As a rule of thumb, before slapping `#[inline(always)]` on a function, measure if that additional function call really affects your runtime profile sufficiently to make up for the increase in compile time.
16 ///
17 /// **Known problems:** False positives, big time. This lint is meant to be deactivated by everyone doing serious performance work. This means having done the measurement.
18 ///
19 /// **Example:**
20 /// ```
21 /// #[inline(always)]
22 /// fn not_quite_hot_code(..) { ... }
23 /// ```
24 declare_lint! { pub INLINE_ALWAYS, Warn,
25     "`#[inline(always)]` is a bad idea in most cases" }
26
27
28 #[derive(Copy,Clone)]
29 pub struct AttrPass;
30
31 impl LintPass for AttrPass {
32     fn get_lints(&self) -> LintArray {
33         lint_array!(INLINE_ALWAYS)
34     }
35 }
36
37 impl LateLintPass for AttrPass {
38     fn check_item(&mut self, cx: &LateContext, item: &Item) {
39         if is_relevant_item(item) {
40             check_attrs(cx, item.span, &item.name, &item.attrs)
41         }
42     }
43
44     fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
45         if is_relevant_impl(item) {
46             check_attrs(cx, item.span, &item.name, &item.attrs)
47         }
48     }
49
50     fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
51         if is_relevant_trait(item) {
52             check_attrs(cx, item.span, &item.name, &item.attrs)
53         }
54     }
55 }
56
57 fn is_relevant_item(item: &Item) -> bool {
58     if let ItemFn(_, _, _, _, _, ref block) = item.node {
59         is_relevant_block(block)
60     } else {
61         false
62     }
63 }
64
65 fn is_relevant_impl(item: &ImplItem) -> bool {
66     match item.node {
67         ImplItemKind::Method(_, ref block) => is_relevant_block(block),
68         _ => false,
69     }
70 }
71
72 fn is_relevant_trait(item: &TraitItem) -> bool {
73     match item.node {
74         MethodTraitItem(_, None) => true,
75         MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
76         _ => false,
77     }
78 }
79
80 fn is_relevant_block(block: &Block) -> bool {
81     for stmt in &block.stmts {
82         match stmt.node {
83             StmtDecl(_, _) => return true,
84             StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
85                 return is_relevant_expr(expr);
86             }
87         }
88     }
89     block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
90 }
91
92 fn is_relevant_expr(expr: &Expr) -> bool {
93     match expr.node {
94         ExprBlock(ref block) => is_relevant_block(block),
95         ExprRet(Some(ref e)) => is_relevant_expr(e),
96         ExprRet(None) | ExprBreak(_) => false,
97         ExprCall(ref path_expr, _) => {
98             if let ExprPath(_, ref path) = path_expr.node {
99                 !match_path(path, &BEGIN_UNWIND)
100             } else {
101                 true
102             }
103         }
104         _ => true,
105     }
106 }
107
108 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
109     if in_macro(cx, span) {
110         return;
111     }
112
113     for attr in attrs {
114         if let MetaList(ref inline, ref values) = attr.node.value.node {
115             if values.len() != 1 || inline != &"inline" {
116                 continue;
117             }
118             if let MetaWord(ref always) = values[0].node {
119                 if always != &"always" {
120                     continue;
121                 }
122                 span_lint(cx,
123                           INLINE_ALWAYS,
124                           attr.span,
125                           &format!("you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
126                                    name));
127             }
128         }
129     }
130 }