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