]> git.lizzy.rs Git - rust.git/blob - src/attrs.rs
Remove * dep
[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 warns 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 { false }
61 }
62
63 fn is_relevant_impl(item: &ImplItem) -> bool {
64     match item.node {
65         ImplItemKind::Method(_, ref block) => is_relevant_block(block),
66         _ => false
67     }
68 }
69
70 fn is_relevant_trait(item: &TraitItem) -> bool {
71     match item.node {
72         MethodTraitItem(_, None) => true,
73         MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
74         _ => false
75     }
76 }
77
78 fn is_relevant_block(block: &Block) -> bool {
79     for stmt in &block.stmts {
80         match stmt.node {
81             StmtDecl(_, _) => return true,
82             StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
83                 return is_relevant_expr(expr);
84             }
85         }
86     }
87     block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
88 }
89
90 fn is_relevant_expr(expr: &Expr) -> bool {
91     match expr.node {
92         ExprBlock(ref block) => is_relevant_block(block),
93         ExprRet(Some(ref e)) => is_relevant_expr(e),
94         ExprRet(None) | ExprBreak(_) => false,
95         ExprCall(ref path_expr, _) => {
96             if let ExprPath(_, ref path) = path_expr.node {
97                 !match_path(path, &BEGIN_UNWIND)
98             } else { true }
99         }
100         _ => true
101     }
102 }
103
104 fn check_attrs(cx: &LateContext, span: Span, name: &Name,
105         attrs: &[Attribute]) {
106     if in_macro(cx, span) { return; }
107
108     for attr in attrs {
109         if let MetaList(ref inline, ref values) = attr.node.value.node {
110             if values.len() != 1 || inline != &"inline" { continue; }
111             if let MetaWord(ref always) = values[0].node {
112                 if always != &"always" { continue; }
113                 span_lint(cx, INLINE_ALWAYS, attr.span, &format!(
114                     "you have declared `#[inline(always)]` on `{}`. This \
115                      is usually a bad idea",
116                     name));
117             }
118         }
119     }
120 }