1 //! checks for attributes
6 use rustc::ty::{self, TyCtxt};
8 use syntax::ast::{Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
9 use syntax::codemap::Span;
10 use utils::{in_macro, match_def_path, paths, snippet_opt, span_lint, span_lint_and_then, opt_def_id};
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
41 /// **Why is this bad?** Lint attributes have no effect on crate imports. Most
45 /// **Known problems:** Technically one might allow `unused_import` on a `use`
47 /// but it's easier to remove the unused item.
51 /// #[deny(dead_code)]
53 /// #[allow(unused_import)]
57 pub USELESS_ATTRIBUTE,
59 "use of lint attributes on `extern crate` items"
62 /// **What it does:** Checks for `#[deprecated]` annotations with a `since`
63 /// field that is not a valid semantic version.
65 /// **Why is this bad?** For checking the version of the deprecation, it must be
66 /// a valid semver. Failing that, the contained information is useless.
68 /// **Known problems:** None.
72 /// #[deprecated(since = "forever")]
73 /// fn something_else(..) { ... }
76 pub DEPRECATED_SEMVER,
78 "use of `#[deprecated(since = \"x\")]` where x is not semver"
81 #[derive(Copy, Clone)]
84 impl LintPass for AttrPass {
85 fn get_lints(&self) -> LintArray {
86 lint_array!(INLINE_ALWAYS, DEPRECATED_SEMVER, USELESS_ATTRIBUTE)
90 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
91 fn check_attribute(&mut self, cx: &LateContext<'a, 'tcx>, attr: &'tcx Attribute) {
92 if let Some(ref items) = attr.meta_item_list() {
93 if items.is_empty() || attr.name().map_or(true, |n| n != "deprecated") {
98 let NestedMetaItemKind::MetaItem(ref mi) = item.node,
99 let MetaItemKind::NameValue(ref lit) = mi.node,
100 mi.name() == "since",
102 check_semver(cx, item.span, lit);
108 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
109 if is_relevant_item(cx.tcx, item) {
110 check_attrs(cx, item.span, &item.name, &item.attrs)
113 ItemExternCrate(_) | ItemUse(_, _) => {
114 for attr in &item.attrs {
115 if let Some(ref lint_list) = attr.meta_item_list() {
116 if let Some(name) = attr.name() {
117 match &*name.as_str() {
118 "allow" | "warn" | "deny" | "forbid" => {
119 // whitelist `unused_imports` and `deprecated`
120 for lint in lint_list {
121 if is_word(lint, "unused_imports") || is_word(lint, "deprecated") {
122 if let ItemUse(_, _) = item.node {
127 if let Some(mut sugg) = snippet_opt(cx, attr.span) {
133 "useless lint attribute",
138 "if you just forgot a `!`, use",
156 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
157 if is_relevant_impl(cx.tcx, item) {
158 check_attrs(cx, item.span, &item.name, &item.attrs)
162 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
163 if is_relevant_trait(cx.tcx, item) {
164 check_attrs(cx, item.span, &item.name, &item.attrs)
169 fn is_relevant_item(tcx: TyCtxt, item: &Item) -> bool {
170 if let ItemFn(_, _, _, _, _, eid) = item.node {
171 is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value)
177 fn is_relevant_impl(tcx: TyCtxt, item: &ImplItem) -> bool {
179 ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value),
184 fn is_relevant_trait(tcx: TyCtxt, item: &TraitItem) -> bool {
186 TraitItemKind::Method(_, TraitMethod::Required(_)) => true,
187 TraitItemKind::Method(_, TraitMethod::Provided(eid)) => {
188 is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir.body(eid).value)
194 fn is_relevant_block(tcx: TyCtxt, tables: &ty::TypeckTables, block: &Block) -> bool {
195 if let Some(stmt) = block.stmts.first() {
197 StmtDecl(_, _) => true,
198 StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => is_relevant_expr(tcx, tables, expr),
204 .map_or(false, |e| is_relevant_expr(tcx, tables, e))
208 fn is_relevant_expr(tcx: TyCtxt, tables: &ty::TypeckTables, expr: &Expr) -> bool {
210 ExprBlock(ref block) => is_relevant_block(tcx, tables, block),
211 ExprRet(Some(ref e)) => is_relevant_expr(tcx, tables, e),
212 ExprRet(None) | ExprBreak(_, None) => false,
213 ExprCall(ref path_expr, _) => if let ExprPath(ref qpath) = path_expr.node {
214 if let Some(fun_id) = opt_def_id(tables.qpath_def(qpath, path_expr.hir_id)) {
215 !match_def_path(tcx, fun_id, &paths::BEGIN_PANIC)
226 fn check_attrs(cx: &LateContext, span: Span, name: &Name, attrs: &[Attribute]) {
232 if let Some(ref values) = attr.meta_item_list() {
233 if values.len() != 1 || attr.name().map_or(true, |n| n != "inline") {
236 if is_word(&values[0], "always") {
242 "you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
251 fn check_semver(cx: &LateContext, span: Span, lit: &Lit) {
252 if let LitKind::Str(ref is, _) = lit.node {
253 if Version::parse(&is.as_str()).is_ok() {
261 "the since field must contain a semver-compliant version",
265 fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool {
266 if let NestedMetaItemKind::MetaItem(ref mi) = nmi.node {
267 mi.is_word() && mi.name() == expected