1 //! checks for attributes
8 use syntax::ast::{Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
9 use syntax::codemap::Span;
10 use utils::{in_macro, match_def_path, paths, span_lint, span_lint_and_then, snippet_opt};
12 /// **What it does:** Checks for items annotated with `#[inline(always)]`,
13 /// unless the annotated function is empty or simply panics.
15 /// **Why is this bad?** While there are valid uses of this annotation (and once
16 /// you know when to use it, by all means `allow` this lint), it's a common
17 /// newbie-mistake to pepper one's code with it.
19 /// As a rule of thumb, before slapping `#[inline(always)]` on a function,
20 /// measure if that additional function call really affects your runtime profile
21 /// sufficiently to make up for the increase in compile time.
23 /// **Known problems:** False positives, big time. This lint is meant to be
24 /// deactivated by everyone doing serious performance work. This means having
25 /// done the measurement.
30 /// fn not_quite_hot_code(..) { ... }
35 "use of `#[inline(always)]`"
38 /// **What it does:** Checks for `extern crate` and `use` items annotated with lint attributes
40 /// **Why is this bad?** Lint attributes have no effect on crate imports. Most likely a `!` was
43 /// **Known problems:** Technically one might allow `unused_import` on a `use` item,
44 /// but it's easier to remove the unused item.
48 /// #[deny(dead_code)]
50 /// #[allow(unused_import)]
54 pub USELESS_ATTRIBUTE,
56 "use of lint attributes on `extern crate` items"
59 /// **What it does:** Checks for `#[deprecated]` annotations with a `since`
60 /// field that is not a valid semantic version.
62 /// **Why is this bad?** For checking the version of the deprecation, it must be
63 /// a valid semver. Failing that, the contained information is useless.
65 /// **Known problems:** None.
69 /// #[deprecated(since = "forever")]
70 /// fn something_else(..) { ... }
73 pub DEPRECATED_SEMVER,
75 "use of `#[deprecated(since = \"x\")]` where x is not semver"
81 impl LintPass for AttrPass {
82 fn get_lints(&self) -> LintArray {
83 lint_array!(INLINE_ALWAYS, DEPRECATED_SEMVER, USELESS_ATTRIBUTE)
87 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
88 fn check_attribute(&mut self, cx: &LateContext<'a, 'tcx>, attr: &'tcx Attribute) {
89 if let MetaItemKind::List(ref items) = attr.value.node {
90 if items.is_empty() || attr.name() != "deprecated" {
95 let NestedMetaItemKind::MetaItem(ref mi) = item.node,
96 let MetaItemKind::NameValue(ref lit) = mi.node,
99 check_semver(cx, item.span, lit);
105 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
106 if is_relevant_item(cx.tcx, item) {
107 check_attrs(cx, item.span, &item.name, &item.attrs)
112 for attr in &item.attrs {
113 if let MetaItemKind::List(ref lint_list) = attr.value.node {
114 match &*attr.name().as_str() {
115 "allow" | "warn" | "deny" | "forbid" => {
116 // whitelist `unused_imports` and `deprecated`
117 for lint in lint_list {
118 if is_word(lint, "unused_imports") || is_word(lint, "deprecated") {
119 if let ItemUse(_, _) = item.node {
124 if let Some(mut sugg) = snippet_opt(cx, attr.span) {
126 span_lint_and_then(cx,
129 "useless lint attribute",
132 db.span_suggestion(attr.span, "if you just forgot a `!`, use", sugg);
146 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
147 if is_relevant_impl(cx.tcx, item) {
148 check_attrs(cx, item.span, &item.name, &item.attrs)
152 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
153 if is_relevant_trait(cx.tcx, item) {
154 check_attrs(cx, item.span, &item.name, &item.attrs)
159 fn is_relevant_item(tcx: ty::TyCtxt, item: &Item) -> bool {
160 if let ItemFn(_, _, _, _, _, eid) = item.node {
161 is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value)
167 fn is_relevant_impl(tcx: ty::TyCtxt, item: &ImplItem) -> bool {
169 ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value),
174 fn is_relevant_trait(tcx: ty::TyCtxt, item: &TraitItem) -> bool {
176 TraitItemKind::Method(_, TraitMethod::Required(_)) => true,
177 TraitItemKind::Method(_, TraitMethod::Provided(eid)) => {
178 is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value)
184 fn is_relevant_block(tcx: ty::TyCtxt, tables: &ty::Tables, block: &Block) -> bool {
185 for stmt in &block.stmts {
187 StmtDecl(_, _) => return true,
188 StmtExpr(ref expr, _) |
189 StmtSemi(ref expr, _) => {
190 return is_relevant_expr(tcx, tables, expr);
194 block.expr.as_ref().map_or(false, |e| is_relevant_expr(tcx, tables, e))
197 fn is_relevant_expr(tcx: ty::TyCtxt, tables: &ty::Tables, expr: &Expr) -> bool {
199 ExprBlock(ref block) => is_relevant_block(tcx, tables, block),
200 ExprRet(Some(ref e)) => is_relevant_expr(tcx, tables, e),
202 ExprBreak(_, None) => false,
203 ExprCall(ref path_expr, _) => {
204 if let ExprPath(ref qpath) = path_expr.node {
205 let fun_id = tables.qpath_def(qpath, path_expr.id).def_id();
206 !match_def_path(tcx, fun_id, &paths::BEGIN_PANIC)
215 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
216 if in_macro(cx, span) {
221 if let MetaItemKind::List(ref values) = attr.value.node {
222 if values.len() != 1 || attr.name() != "inline" {
225 if is_word(&values[0], "always") {
229 &format!("you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
236 fn check_semver(cx: &LateContext, span: Span, lit: &Lit) {
237 if let LitKind::Str(ref is, _) = lit.node {
238 if Version::parse(&*is.as_str()).is_ok() {
245 "the since field must contain a semver-compliant version");
248 fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool {
249 if let NestedMetaItemKind::MetaItem(ref mi) = nmi.node {
250 mi.is_word() && mi.name() == expected