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