]> git.lizzy.rs Git - rust.git/blob - src/attrs.rs
Merge pull request #906 from Manishearth/birkenfeld-master
[rust.git] / src / attrs.rs
1 //! checks for attributes
2
3 use reexport::*;
4 use rustc::lint::*;
5 use rustc::hir::*;
6 use semver::Version;
7 use syntax::ast::{Attribute, Lit, LitKind, MetaItemKind};
8 use syntax::attr::*;
9 use syntax::codemap::Span;
10 use utils::{in_macro, match_path, span_lint};
11 use utils::paths;
12
13 /// **What it does:** This lint checks for items annotated with `#[inline(always)]`, unless the annotated function is empty or simply panics.
14 ///
15 /// **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.
16 ///
17 /// 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.
18 ///
19 /// **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.
20 ///
21 /// **Example:**
22 /// ```
23 /// #[inline(always)]
24 /// fn not_quite_hot_code(..) { ... }
25 /// ```
26 declare_lint! {
27     pub INLINE_ALWAYS, Warn,
28     "`#[inline(always)]` is a bad idea in most cases"
29 }
30
31 /// **What it does:** This lint checks for `#[deprecated]` annotations with a `since` field that is not a valid semantic version..
32 ///
33 /// **Why is this bad?** For checking the version of the deprecation, it must be valid semver. Failing that, the contained information is useless.
34 ///
35 /// **Known problems:** None
36 ///
37 /// **Example:**
38 /// ```
39 /// #[deprecated(since = "forever")]
40 /// fn something_else(..) { ... }
41 /// ```
42 declare_lint! {
43     pub DEPRECATED_SEMVER, Warn,
44     "`Warn` on `#[deprecated(since = \"x\")]` where x is not semver"
45 }
46
47 #[derive(Copy,Clone)]
48 pub struct AttrPass;
49
50 impl LintPass for AttrPass {
51     fn get_lints(&self) -> LintArray {
52         lint_array!(INLINE_ALWAYS, DEPRECATED_SEMVER)
53     }
54 }
55
56 impl LateLintPass for AttrPass {
57     fn check_attribute(&mut self, cx: &LateContext, attr: &Attribute) {
58         if let MetaItemKind::List(ref name, ref items) = attr.node.value.node {
59             if items.is_empty() || name != &"deprecated" {
60                 return;
61             }
62             for ref item in items {
63                 if let MetaItemKind::NameValue(ref name, ref lit) = item.node {
64                     if name == &"since" {
65                         check_semver(cx, item.span, lit);
66                     }
67                 }
68             }
69         }
70     }
71
72     fn check_item(&mut self, cx: &LateContext, item: &Item) {
73         if is_relevant_item(item) {
74             check_attrs(cx, item.span, &item.name, &item.attrs)
75         }
76     }
77
78     fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
79         if is_relevant_impl(item) {
80             check_attrs(cx, item.span, &item.name, &item.attrs)
81         }
82     }
83
84     fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
85         if is_relevant_trait(item) {
86             check_attrs(cx, item.span, &item.name, &item.attrs)
87         }
88     }
89 }
90
91 fn is_relevant_item(item: &Item) -> bool {
92     if let ItemFn(_, _, _, _, _, ref block) = item.node {
93         is_relevant_block(block)
94     } else {
95         false
96     }
97 }
98
99 fn is_relevant_impl(item: &ImplItem) -> bool {
100     match item.node {
101         ImplItemKind::Method(_, ref block) => is_relevant_block(block),
102         _ => false,
103     }
104 }
105
106 fn is_relevant_trait(item: &TraitItem) -> bool {
107     match item.node {
108         MethodTraitItem(_, None) => true,
109         MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
110         _ => false,
111     }
112 }
113
114 fn is_relevant_block(block: &Block) -> bool {
115     for stmt in &block.stmts {
116         match stmt.node {
117             StmtDecl(_, _) => return true,
118             StmtExpr(ref expr, _) |
119             StmtSemi(ref expr, _) => {
120                 return is_relevant_expr(expr);
121             }
122         }
123     }
124     block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
125 }
126
127 fn is_relevant_expr(expr: &Expr) -> bool {
128     match expr.node {
129         ExprBlock(ref block) => is_relevant_block(block),
130         ExprRet(Some(ref e)) => is_relevant_expr(e),
131         ExprRet(None) | ExprBreak(_) => false,
132         ExprCall(ref path_expr, _) => {
133             if let ExprPath(_, ref path) = path_expr.node {
134                 !match_path(path, &paths::BEGIN_UNWIND)
135             } else {
136                 true
137             }
138         }
139         _ => true,
140     }
141 }
142
143 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
144     if in_macro(cx, span) {
145         return;
146     }
147
148     for attr in attrs {
149         if let MetaItemKind::List(ref inline, ref values) = attr.node.value.node {
150             if values.len() != 1 || inline != &"inline" {
151                 continue;
152             }
153             if let MetaItemKind::Word(ref always) = values[0].node {
154                 if always != &"always" {
155                     continue;
156                 }
157                 span_lint(cx,
158                           INLINE_ALWAYS,
159                           attr.span,
160                           &format!("you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
161                                    name));
162             }
163         }
164     }
165 }
166
167 fn check_semver(cx: &LateContext, span: Span, lit: &Lit) {
168     if let LitKind::Str(ref is, _) = lit.node {
169         if Version::parse(&*is).is_ok() {
170             return;
171         }
172     }
173     span_lint(cx,
174               DEPRECATED_SEMVER,
175               span,
176               "the since field must contain a semver-compliant version");
177 }