1 //! checks for attributes
7 use syntax::ast::{Attribute, Lit, LitKind, MetaItemKind};
9 use syntax::codemap::Span;
10 use utils::{in_macro, match_path, span_lint};
13 /// **What it does:** This lint checks for items annotated with `#[inline(always)]`, unless the annotated function is empty or simply panics.
15 /// **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.
17 /// 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.
19 /// **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.
24 /// fn not_quite_hot_code(..) { ... }
27 pub INLINE_ALWAYS, Warn,
28 "`#[inline(always)]` is a bad idea in most cases"
31 /// **What it does:** This lint checks for `#[deprecated]` annotations with a `since` field that is not a valid semantic version..
33 /// **Why is this bad?** For checking the version of the deprecation, it must be valid semver. Failing that, the contained information is useless.
35 /// **Known problems:** None
39 /// #[deprecated(since = "forever")]
40 /// fn something_else(..) { ... }
43 pub DEPRECATED_SEMVER, Warn,
44 "`Warn` on `#[deprecated(since = \"x\")]` where x is not semver"
50 impl LintPass for AttrPass {
51 fn get_lints(&self) -> LintArray {
52 lint_array!(INLINE_ALWAYS, DEPRECATED_SEMVER)
56 impl LateLintPass for AttrPass {
57 fn check_attribute(&mut self, cx: &LateContext, attr: &Attribute) {
58 if let MetaItemKind::List(ref name, ref items) = attr.node.value.node {
59 if items.is_empty() || name != &"deprecated" {
62 for ref item in items {
63 if let MetaItemKind::NameValue(ref name, ref lit) = item.node {
65 check_semver(cx, item.span, lit);
72 fn check_item(&mut self, cx: &LateContext, item: &Item) {
73 if is_relevant_item(item) {
74 check_attrs(cx, item.span, &item.name, &item.attrs)
78 fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
79 if is_relevant_impl(item) {
80 check_attrs(cx, item.span, &item.name, &item.attrs)
84 fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
85 if is_relevant_trait(item) {
86 check_attrs(cx, item.span, &item.name, &item.attrs)
91 fn is_relevant_item(item: &Item) -> bool {
92 if let ItemFn(_, _, _, _, _, ref block) = item.node {
93 is_relevant_block(block)
99 fn is_relevant_impl(item: &ImplItem) -> bool {
101 ImplItemKind::Method(_, ref block) => is_relevant_block(block),
106 fn is_relevant_trait(item: &TraitItem) -> bool {
108 MethodTraitItem(_, None) => true,
109 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
114 fn is_relevant_block(block: &Block) -> bool {
115 for stmt in &block.stmts {
117 StmtDecl(_, _) => return true,
118 StmtExpr(ref expr, _) |
119 StmtSemi(ref expr, _) => {
120 return is_relevant_expr(expr);
124 block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
127 fn is_relevant_expr(expr: &Expr) -> bool {
129 ExprBlock(ref block) => is_relevant_block(block),
130 ExprRet(Some(ref e)) => is_relevant_expr(e),
131 ExprRet(None) | ExprBreak(_) => false,
132 ExprCall(ref path_expr, _) => {
133 if let ExprPath(_, ref path) = path_expr.node {
134 !match_path(path, &paths::BEGIN_UNWIND)
143 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
144 if in_macro(cx, span) {
149 if let MetaItemKind::List(ref inline, ref values) = attr.node.value.node {
150 if values.len() != 1 || inline != &"inline" {
153 if let MetaItemKind::Word(ref always) = values[0].node {
154 if always != &"always" {
160 &format!("you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
167 fn check_semver(cx: &LateContext, span: Span, lit: &Lit) {
168 if let LitKind::Str(ref is, _) = lit.node {
169 if Version::parse(&*is).is_ok() {
176 "the since field must contain a semver-compliant version");