1 //! checks for attributes
7 use syntax::ast::{Attribute, Lit, LitKind, MetaItemKind};
8 use syntax::codemap::Span;
9 use utils::{in_macro, match_path, span_lint};
12 /// **What it does:** This lint checks for items annotated with `#[inline(always)]`, unless the annotated function is empty or simply panics.
14 /// **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.
16 /// 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.
18 /// **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.
23 /// fn not_quite_hot_code(..) { ... }
26 pub INLINE_ALWAYS, Warn,
27 "`#[inline(always)]` is a bad idea in most cases"
30 /// **What it does:** This lint checks for `#[deprecated]` annotations with a `since` field that is not a valid semantic version..
32 /// **Why is this bad?** For checking the version of the deprecation, it must be valid semver. Failing that, the contained information is useless.
34 /// **Known problems:** None
38 /// #[deprecated(since = "forever")]
39 /// fn something_else(..) { ... }
42 pub DEPRECATED_SEMVER, Warn,
43 "`Warn` on `#[deprecated(since = \"x\")]` where x is not semver"
49 impl LintPass for AttrPass {
50 fn get_lints(&self) -> LintArray {
51 lint_array!(INLINE_ALWAYS, DEPRECATED_SEMVER)
55 impl LateLintPass for AttrPass {
56 fn check_attribute(&mut self, cx: &LateContext, attr: &Attribute) {
57 if let MetaItemKind::List(ref name, ref items) = attr.node.value.node {
58 if items.is_empty() || name != &"deprecated" {
61 for ref item in items {
62 if let MetaItemKind::NameValue(ref name, ref lit) = item.node {
64 check_semver(cx, item.span, lit);
71 fn check_item(&mut self, cx: &LateContext, item: &Item) {
72 if is_relevant_item(item) {
73 check_attrs(cx, item.span, &item.name, &item.attrs)
77 fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
78 if is_relevant_impl(item) {
79 check_attrs(cx, item.span, &item.name, &item.attrs)
83 fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
84 if is_relevant_trait(item) {
85 check_attrs(cx, item.span, &item.name, &item.attrs)
90 fn is_relevant_item(item: &Item) -> bool {
91 if let ItemFn(_, _, _, _, _, ref block) = item.node {
92 is_relevant_block(block)
98 fn is_relevant_impl(item: &ImplItem) -> bool {
100 ImplItemKind::Method(_, ref block) => is_relevant_block(block),
105 fn is_relevant_trait(item: &TraitItem) -> bool {
107 MethodTraitItem(_, None) => true,
108 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
113 fn is_relevant_block(block: &Block) -> bool {
114 for stmt in &block.stmts {
116 StmtDecl(_, _) => return true,
117 StmtExpr(ref expr, _) |
118 StmtSemi(ref expr, _) => {
119 return is_relevant_expr(expr);
123 block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
126 fn is_relevant_expr(expr: &Expr) -> bool {
128 ExprBlock(ref block) => is_relevant_block(block),
129 ExprRet(Some(ref e)) => is_relevant_expr(e),
130 ExprRet(None) | ExprBreak(_) => false,
131 ExprCall(ref path_expr, _) => {
132 if let ExprPath(_, ref path) = path_expr.node {
133 !match_path(path, &paths::BEGIN_UNWIND)
142 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
143 if in_macro(cx, span) {
148 if let MetaItemKind::List(ref inline, ref values) = attr.node.value.node {
149 if values.len() != 1 || inline != &"inline" {
152 if let MetaItemKind::Word(ref always) = values[0].node {
153 if always != &"always" {
159 &format!("you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
166 fn check_semver(cx: &LateContext, span: Span, lit: &Lit) {
167 if let LitKind::Str(ref is, _) = lit.node {
168 if Version::parse(&*is).is_ok() {
175 "the since field must contain a semver-compliant version");