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