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 `Warn`s 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)
65 fn is_relevant_impl(item: &ImplItem) -> bool {
67 ImplItemKind::Method(_, ref block) => is_relevant_block(block),
72 fn is_relevant_trait(item: &TraitItem) -> bool {
74 MethodTraitItem(_, None) => true,
75 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
80 fn is_relevant_block(block: &Block) -> bool {
81 for stmt in &block.stmts {
83 StmtDecl(_, _) => return true,
84 StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
85 return is_relevant_expr(expr);
89 block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
92 fn is_relevant_expr(expr: &Expr) -> bool {
94 ExprBlock(ref block) => is_relevant_block(block),
95 ExprRet(Some(ref e)) => is_relevant_expr(e),
96 ExprRet(None) | ExprBreak(_) => false,
97 ExprCall(ref path_expr, _) => {
98 if let ExprPath(_, ref path) = path_expr.node {
99 !match_path(path, &BEGIN_UNWIND)
108 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
109 if in_macro(cx, span) {
114 if let MetaList(ref inline, ref values) = attr.node.value.node {
115 if values.len() != 1 || inline != &"inline" {
118 if let MetaWord(ref always) = values[0].node {
119 if always != &"always" {
125 &format!("you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",