1 /// checks for attributes
3 use rustc::plugin::Registry;
7 use syntax::codemap::{Span, ExpnInfo};
8 use syntax::parse::token::InternedString;
9 use utils::{in_macro, match_path, span_lint};
11 declare_lint! { pub INLINE_ALWAYS, Warn,
12 "#[inline(always)] is usually a bad idea."}
18 impl LintPass for AttrPass {
19 fn get_lints(&self) -> LintArray {
20 lint_array!(INLINE_ALWAYS)
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))
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))
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))
45 fn is_relevant_item(item: &Item) -> bool {
46 if let &ItemFn(_, _, _, _, _, ref block) = &item.node {
47 is_relevant_block(block)
51 fn is_relevant_impl(item: &ImplItem) -> bool {
53 MethodImplItem(_, ref block) => is_relevant_block(block),
58 fn is_relevant_trait(item: &TraitItem) -> bool {
60 MethodTraitItem(_, None) => true,
61 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
66 fn is_relevant_block(block: &Block) -> bool {
67 for stmt in block.stmts.iter() {
69 StmtDecl(_, _) => return true,
70 StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
71 return is_relevant_expr(expr);
76 block.expr.as_ref().map_or(false, |e| is_relevant_expr(&*e))
79 fn is_relevant_expr(expr: &Expr) -> bool {
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"])
94 fn check_attrs(cx: &Context, info: Option<&ExpnInfo>, ident: &Ident,
95 attrs: &[Attribute]) {
96 if in_macro(cx, info) { return; }
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()));