1 //! checks for attributes
4 use rustc_front::hir::*;
6 use syntax::codemap::Span;
8 use syntax::ast::{Attribute, MetaList, MetaWord};
9 use utils::{in_macro, match_path, span_lint, BEGIN_UNWIND};
11 /// **What it does:** This lint warns on items annotated with `#[inline(always)]`, unless the annotated function is empty or simply panics.
13 /// **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 /// 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 /// **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.
22 /// fn not_quite_hot_code(..) { ... }
24 declare_lint! { pub INLINE_ALWAYS, Warn,
25 "`#[inline(always)]` is a bad idea in most cases" }
31 impl LintPass for AttrPass {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(INLINE_ALWAYS)
37 impl LateLintPass for AttrPass {
38 fn check_item(&mut self, cx: &LateContext, item: &Item) {
39 if is_relevant_item(item) {
40 check_attrs(cx, item.span, &item.name, &item.attrs)
44 fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
45 if is_relevant_impl(item) {
46 check_attrs(cx, item.span, &item.name, &item.attrs)
50 fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
51 if is_relevant_trait(item) {
52 check_attrs(cx, item.span, &item.name, &item.attrs)
57 fn is_relevant_item(item: &Item) -> bool {
58 if let ItemFn(_, _, _, _, _, ref block) = item.node {
59 is_relevant_block(block)
63 fn is_relevant_impl(item: &ImplItem) -> bool {
65 ImplItemKind::Method(_, ref block) => is_relevant_block(block),
70 fn is_relevant_trait(item: &TraitItem) -> bool {
72 MethodTraitItem(_, None) => true,
73 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
78 fn is_relevant_block(block: &Block) -> bool {
79 for stmt in &block.stmts {
81 StmtDecl(_, _) => return true,
82 StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
83 return is_relevant_expr(expr);
87 block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
90 fn is_relevant_expr(expr: &Expr) -> bool {
92 ExprBlock(ref block) => is_relevant_block(block),
93 ExprRet(Some(ref e)) => is_relevant_expr(e),
94 ExprRet(None) | ExprBreak(_) => false,
95 ExprCall(ref path_expr, _) => {
96 if let ExprPath(_, ref path) = path_expr.node {
97 !match_path(path, &BEGIN_UNWIND)
104 fn check_attrs(cx: &LateContext, span: Span, name: &Name,
105 attrs: &[Attribute]) {
106 if in_macro(cx, span) { return; }
109 if let MetaList(ref inline, ref values) = attr.node.value.node {
110 if values.len() != 1 || inline != &"inline" { continue; }
111 if let MetaWord(ref always) = values[0].node {
112 if always != &"always" { continue; }
113 span_lint(cx, INLINE_ALWAYS, attr.span, &format!(
114 "you have declared `#[inline(always)]` on `{}`. This \
115 is usually a bad idea",