]> git.lizzy.rs Git - rust.git/blob - src/attrs.rs
Merge pull request #241 from Manishearth/links
[rust.git] / src / attrs.rs
1 //! checks for attributes
2
3 use rustc::lint::*;
4 use syntax::ast::*;
5 use syntax::codemap::ExpnInfo;
6
7 use utils::{in_macro, match_path, span_help_and_lint};
8
9 declare_lint! { pub INLINE_ALWAYS, Warn,
10     "`#[inline(always)]` is a bad idea in most cases" }
11
12
13 #[derive(Copy,Clone)]
14 pub struct AttrPass;
15
16 impl LintPass for AttrPass {
17     fn get_lints(&self) -> LintArray {
18         lint_array!(INLINE_ALWAYS)
19     }
20
21     fn check_item(&mut self, cx: &Context, item: &Item) {
22         if is_relevant_item(item) {
23             cx.sess().codemap().with_expn_info(item.span.expn_id,
24                 |info| check_attrs(cx, info, &item.ident, &item.attrs))
25         }
26     }
27
28     fn check_impl_item(&mut self, cx: &Context, item: &ImplItem) {
29         if is_relevant_impl(item) {
30             cx.sess().codemap().with_expn_info(item.span.expn_id,
31                 |info| check_attrs(cx, info, &item.ident, &item.attrs))
32         }
33     }
34
35     fn check_trait_item(&mut self, cx: &Context, item: &TraitItem) {
36         if is_relevant_trait(item) {
37             cx.sess().codemap().with_expn_info(item.span.expn_id,
38                 |info| check_attrs(cx, info, &item.ident, &item.attrs))
39         }
40     }
41 }
42
43 fn is_relevant_item(item: &Item) -> bool {
44     if let &ItemFn(_, _, _, _, _, ref block) = &item.node {
45         is_relevant_block(block)
46     } else { false }
47 }
48
49 fn is_relevant_impl(item: &ImplItem) -> bool {
50     match item.node {
51         MethodImplItem(_, ref block) => is_relevant_block(block),
52         _ => false
53     }
54 }
55
56 fn is_relevant_trait(item: &TraitItem) -> bool {
57     match item.node {
58         MethodTraitItem(_, None) => true,
59         MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
60         _ => false
61     }
62 }
63
64 fn is_relevant_block(block: &Block) -> bool {
65     for stmt in &block.stmts {
66         match stmt.node {
67             StmtDecl(_, _) => return true,
68             StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
69                 return is_relevant_expr(expr);
70             }
71             _ => ()
72         }
73     }
74     block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
75 }
76
77 fn is_relevant_expr(expr: &Expr) -> bool {
78     match expr.node {
79         ExprBlock(ref block) => is_relevant_block(block),
80         ExprRet(Some(ref e)) | ExprParen(ref e) =>
81             is_relevant_expr(e),
82         ExprRet(None) | ExprBreak(_) | ExprMac(_) => false,
83         ExprCall(ref path_expr, _) => {
84             if let ExprPath(_, ref path) = path_expr.node {
85                 !match_path(path, &["std", "rt", "begin_unwind"])
86             } else { true }
87         }
88         _ => true
89     }
90 }
91
92 fn check_attrs(cx: &Context, info: Option<&ExpnInfo>, ident: &Ident,
93                attrs: &[Attribute]) {
94     if in_macro(cx, info) { return; }
95
96     for attr in attrs {
97         if let MetaList(ref inline, ref values) = attr.node.value.node {
98             if values.len() != 1 || inline != &"inline" { continue; }
99             if let MetaWord(ref always) = values[0].node {
100                 if always != &"always" { continue; }
101                 span_help_and_lint(cx, INLINE_ALWAYS, attr.span, &format!(
102                     "you have declared `#[inline(always)]` on `{}`. This \
103                      is usually a bad idea. Are you sure?",
104                     ident.name),
105                     "for further information see https://github.com/\
106                     Manishearth/rust-clippy/wiki#inline_always");
107             }
108         }
109     }
110 }