1 //! checks for attributes
4 use rustc_front::hir::*;
6 use syntax::codemap::ExpnInfo;
8 use utils::{in_macro, match_path, span_lint};
10 declare_lint! { pub INLINE_ALWAYS, Warn,
11 "`#[inline(always)]` is a bad idea in most cases" }
17 impl LintPass for AttrPass {
18 fn get_lints(&self) -> LintArray {
19 lint_array!(INLINE_ALWAYS)
22 fn check_item(&mut self, cx: &Context, item: &Item) {
23 if is_relevant_item(item) {
24 cx.sess().codemap().with_expn_info(item.span.expn_id,
25 |info| check_attrs(cx, info, &item.ident, &item.attrs))
29 fn check_impl_item(&mut self, cx: &Context, item: &ImplItem) {
30 if is_relevant_impl(item) {
31 cx.sess().codemap().with_expn_info(item.span.expn_id,
32 |info| check_attrs(cx, info, &item.ident, &item.attrs))
36 fn check_trait_item(&mut self, cx: &Context, item: &TraitItem) {
37 if is_relevant_trait(item) {
38 cx.sess().codemap().with_expn_info(item.span.expn_id,
39 |info| check_attrs(cx, info, &item.ident, &item.attrs))
44 fn is_relevant_item(item: &Item) -> bool {
45 if let &ItemFn(_, _, _, _, _, ref block) = &item.node {
46 is_relevant_block(block)
50 fn is_relevant_impl(item: &ImplItem) -> bool {
52 MethodImplItem(_, ref block) => is_relevant_block(block),
57 fn is_relevant_trait(item: &TraitItem) -> bool {
59 MethodTraitItem(_, None) => true,
60 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
65 fn is_relevant_block(block: &Block) -> bool {
66 for stmt in &block.stmts {
68 StmtDecl(_, _) => return true,
69 StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
70 return is_relevant_expr(expr);
74 block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
77 fn is_relevant_expr(expr: &Expr) -> bool {
79 ExprBlock(ref block) => is_relevant_block(block),
80 ExprRet(Some(ref e)) | ExprParen(ref e) =>
82 ExprRet(None) | ExprBreak(_) => false,
83 ExprCall(ref path_expr, _) => {
84 if let ExprPath(_, ref path) = path_expr.node {
85 !match_path(path, &["std", "rt", "begin_unwind"])
92 fn check_attrs(cx: &Context, info: Option<&ExpnInfo>, ident: &Ident,
93 attrs: &[Attribute]) {
94 if in_macro(cx, info) { return; }
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_lint(cx, INLINE_ALWAYS, attr.span, &format!(
102 "you have declared `#[inline(always)]` on `{}`. This \
103 is usually a bad idea. Are you sure?",