1 //! checks for attributes
4 use rustc_front::hir::*;
7 use syntax::codemap::Span;
9 use syntax::ast::{Attribute, Lit, Lit_, MetaList, MetaWord, MetaNameValue};
10 use utils::{in_macro, match_path, span_lint, BEGIN_UNWIND};
12 /// **What it does:** This lint `Warn`s on 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(..) { ... }
25 declare_lint! { pub INLINE_ALWAYS, Warn,
26 "`#[inline(always)]` is a bad idea in most cases" }
28 /// **What it does:** This lint `Warn`s on `#[deprecated]` annotations with a `since` field that is not a valid semantic version..
30 /// **Why is this bad?** For checking the version of the deprecation, it must be valid semver. Failing that, the contained information is useless.
32 /// **Known problems:** None
36 /// #[deprecated(since = "forever")]
37 /// fn something_else(..) { ... }
39 declare_lint! { pub DEPRECATED_SEMVER, Warn,
40 "`Warn` on `#[deprecated(since = \"x\")]` where x is not semver" }
45 impl LintPass for AttrPass {
46 fn get_lints(&self) -> LintArray {
47 lint_array!(INLINE_ALWAYS, DEPRECATED_SEMVER)
51 impl LateLintPass for AttrPass {
52 fn check_attribute(&mut self, cx: &LateContext, attr: &Attribute) {
53 if let MetaList(ref name, ref items) = attr.node.value.node {
54 if items.is_empty() || name != &"deprecated" {
57 for ref item in items {
58 if let MetaNameValue(ref name, ref lit) = item.node {
60 check_semver(cx, item.span, lit);
67 fn check_item(&mut self, cx: &LateContext, item: &Item) {
68 if is_relevant_item(item) {
69 check_attrs(cx, item.span, &item.name, &item.attrs)
73 fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
74 if is_relevant_impl(item) {
75 check_attrs(cx, item.span, &item.name, &item.attrs)
79 fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
80 if is_relevant_trait(item) {
81 check_attrs(cx, item.span, &item.name, &item.attrs)
86 fn is_relevant_item(item: &Item) -> bool {
87 if let ItemFn(_, _, _, _, _, ref block) = item.node {
88 is_relevant_block(block)
94 fn is_relevant_impl(item: &ImplItem) -> bool {
96 ImplItemKind::Method(_, ref block) => is_relevant_block(block),
101 fn is_relevant_trait(item: &TraitItem) -> bool {
103 MethodTraitItem(_, None) => true,
104 MethodTraitItem(_, Some(ref block)) => is_relevant_block(block),
109 fn is_relevant_block(block: &Block) -> bool {
110 for stmt in &block.stmts {
112 StmtDecl(_, _) => return true,
113 StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => {
114 return is_relevant_expr(expr);
118 block.expr.as_ref().map_or(false, |e| is_relevant_expr(e))
121 fn is_relevant_expr(expr: &Expr) -> bool {
123 ExprBlock(ref block) => is_relevant_block(block),
124 ExprRet(Some(ref e)) => is_relevant_expr(e),
125 ExprRet(None) | ExprBreak(_) => false,
126 ExprCall(ref path_expr, _) => {
127 if let ExprPath(_, ref path) = path_expr.node {
128 !match_path(path, &BEGIN_UNWIND)
137 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
138 if in_macro(cx, span) {
143 if let MetaList(ref inline, ref values) = attr.node.value.node {
144 if values.len() != 1 || inline != &"inline" {
147 if let MetaWord(ref always) = values[0].node {
148 if always != &"always" {
154 &format!("you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
161 fn check_semver(cx: &LateContext, span: Span, lit: &Lit) {
162 if let Lit_::LitStr(ref is, _) = lit.node {
163 if Version::parse(&*is).is_ok() {
170 "the since field must contain a semver-compliant version");