1 //! checks for attributes
3 use crate::reexport::*;
5 in_macro_or_desugar, is_present_in_source, last_line_of_span, paths, snippet_opt, span_lint, span_lint_and_sugg,
6 span_lint_and_then, without_block_comments,
8 use if_chain::if_chain;
11 in_external_macro, CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray,
12 LintContext, LintPass,
15 use rustc::{declare_lint_pass, declare_tool_lint};
16 use rustc_errors::Applicability;
18 use syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
19 use syntax::source_map::Span;
21 declare_clippy_lint! {
22 /// **What it does:** Checks for items annotated with `#[inline(always)]`,
23 /// unless the annotated function is empty or simply panics.
25 /// **Why is this bad?** While there are valid uses of this annotation (and once
26 /// you know when to use it, by all means `allow` this lint), it's a common
27 /// newbie-mistake to pepper one's code with it.
29 /// As a rule of thumb, before slapping `#[inline(always)]` on a function,
30 /// measure if that additional function call really affects your runtime profile
31 /// sufficiently to make up for the increase in compile time.
33 /// **Known problems:** False positives, big time. This lint is meant to be
34 /// deactivated by everyone doing serious performance work. This means having
35 /// done the measurement.
40 /// fn not_quite_hot_code(..) { ... }
44 "use of `#[inline(always)]`"
47 declare_clippy_lint! {
48 /// **What it does:** Checks for `extern crate` and `use` items annotated with
51 /// This lint whitelists `#[allow(unused_imports)]` and `#[allow(deprecated)]` on
52 /// `use` items and `#[allow(unused_imports)]` on `extern crate` items with a
53 /// `#[macro_use]` attribute.
55 /// **Why is this bad?** Lint attributes have no effect on crate imports. Most
56 /// likely a `!` was forgotten.
58 /// **Known problems:** None.
63 /// #[deny(dead_code)]
65 /// #[forbid(dead_code)]
69 /// #[allow(unused_imports)]
71 /// #[allow(unused_imports)]
75 pub USELESS_ATTRIBUTE,
77 "use of lint attributes on `extern crate` items"
80 declare_clippy_lint! {
81 /// **What it does:** Checks for `#[deprecated]` annotations with a `since`
82 /// field that is not a valid semantic version.
84 /// **Why is this bad?** For checking the version of the deprecation, it must be
85 /// a valid semver. Failing that, the contained information is useless.
87 /// **Known problems:** None.
91 /// #[deprecated(since = "forever")]
92 /// fn something_else() { /* ... */ }
94 pub DEPRECATED_SEMVER,
96 "use of `#[deprecated(since = \"x\")]` where x is not semver"
99 declare_clippy_lint! {
100 /// **What it does:** Checks for empty lines after outer attributes
102 /// **Why is this bad?**
103 /// Most likely the attribute was meant to be an inner attribute using a '!'.
104 /// If it was meant to be an outer attribute, then the following item
105 /// should not be separated by empty lines.
107 /// **Known problems:** Can cause false positives.
109 /// From the clippy side it's difficult to detect empty lines between an attributes and the
110 /// following item because empty lines and comments are not part of the AST. The parsing
111 /// currently works for basic cases but is not perfect.
116 /// #[inline(always)]
118 /// fn not_quite_good_code(..) { ... }
120 /// // Good (as inner attribute)
121 /// #![inline(always)]
123 /// fn this_is_fine(..) { ... }
125 /// // Good (as outer attribute)
126 /// #[inline(always)]
127 /// fn this_is_fine_too(..) { ... }
129 pub EMPTY_LINE_AFTER_OUTER_ATTR,
131 "empty line after outer attribute"
134 declare_clippy_lint! {
135 /// **What it does:** Checks for `allow`/`warn`/`deny`/`forbid` attributes with scoped clippy
136 /// lints and if those lints exist in clippy. If there is a uppercase letter in the lint name
137 /// (not the tool name) and a lowercase version of this lint exists, it will suggest to lowercase
140 /// **Why is this bad?** A lint attribute with a mistyped lint name won't have an effect.
142 /// **Known problems:** None.
147 /// #![warn(if_not_els)]
148 /// #![deny(clippy::All)]
153 /// #![warn(if_not_else)]
154 /// #![deny(clippy::all)]
156 pub UNKNOWN_CLIPPY_LINTS,
158 "unknown_lints for scoped Clippy lints"
161 declare_clippy_lint! {
162 /// **What it does:** Checks for `#[cfg_attr(rustfmt, rustfmt_skip)]` and suggests to replace it
163 /// with `#[rustfmt::skip]`.
165 /// **Why is this bad?** Since tool_attributes ([rust-lang/rust#44690](https://github.com/rust-lang/rust/issues/44690))
166 /// are stable now, they should be used instead of the old `cfg_attr(rustfmt)` attributes.
168 /// **Known problems:** This lint doesn't detect crate level inner attributes, because they get
169 /// processed before the PreExpansionPass lints get executed. See
170 /// [#3123](https://github.com/rust-lang/rust-clippy/pull/3123#issuecomment-422321765)
176 /// #[cfg_attr(rustfmt, rustfmt_skip)]
185 pub DEPRECATED_CFG_ATTR,
187 "usage of `cfg_attr(rustfmt)` instead of `tool_attributes`"
190 declare_lint_pass!(Attributes => [
194 EMPTY_LINE_AFTER_OUTER_ATTR,
195 UNKNOWN_CLIPPY_LINTS,
198 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes {
199 fn check_attribute(&mut self, cx: &LateContext<'a, 'tcx>, attr: &'tcx Attribute) {
200 if let Some(items) = &attr.meta_item_list() {
201 if let Some(ident) = attr.ident() {
202 match &*ident.as_str() {
203 "allow" | "warn" | "deny" | "forbid" => {
204 check_clippy_lint_names(cx, items);
208 if items.is_empty() || !attr.check_name("deprecated") {
213 if let NestedMetaItem::MetaItem(mi) = &item;
214 if let MetaItemKind::NameValue(lit) = &mi.node;
215 if mi.check_name("since");
217 check_semver(cx, item.span(), lit);
225 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
226 if is_relevant_item(cx, item) {
227 check_attrs(cx, item.span, item.ident.name, &item.attrs)
230 ItemKind::ExternCrate(..) | ItemKind::Use(..) => {
231 let skip_unused_imports = item.attrs.iter().any(|attr| attr.check_name("macro_use"));
233 for attr in &item.attrs {
234 if in_external_macro(cx.sess(), attr.span) {
237 if let Some(lint_list) = &attr.meta_item_list() {
238 if let Some(ident) = attr.ident() {
239 match &*ident.as_str() {
240 "allow" | "warn" | "deny" | "forbid" => {
241 // whitelist `unused_imports` and `deprecated` for `use` items
242 // and `unused_imports` for `extern crate` items with `macro_use`
243 for lint in lint_list {
245 ItemKind::Use(..) => {
246 if is_word(lint, "unused_imports") || is_word(lint, "deprecated") {
250 ItemKind::ExternCrate(..) => {
251 if is_word(lint, "unused_imports") && skip_unused_imports {
254 if is_word(lint, "unused_extern_crates") {
261 let line_span = last_line_of_span(cx, attr.span);
263 if let Some(mut sugg) = snippet_opt(cx, line_span) {
264 if sugg.contains("#[") {
269 "useless lint attribute",
271 sugg = sugg.replacen("#[", "#![", 1);
274 "if you just forgot a `!`, use",
276 Applicability::MachineApplicable,
293 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
294 if is_relevant_impl(cx, item) {
295 check_attrs(cx, item.span, item.ident.name, &item.attrs)
299 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
300 if is_relevant_trait(cx, item) {
301 check_attrs(cx, item.span, item.ident.name, &item.attrs)
306 #[allow(clippy::single_match_else)]
307 fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) {
308 let lint_store = cx.lints();
311 if let Some(meta_item) = lint.meta_item();
312 if meta_item.path.segments.len() > 1;
313 if let tool_name = meta_item.path.segments[0].ident;
314 if tool_name.as_str() == "clippy";
315 let name = meta_item.path.segments.last().unwrap().ident.name;
316 if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name(
318 Some(tool_name.as_str()),
323 UNKNOWN_CLIPPY_LINTS,
325 &format!("unknown clippy lint: clippy::{}", name),
327 if name.as_str().chars().any(char::is_uppercase) {
328 let name_lower = name.as_str().to_lowercase();
329 match lint_store.check_lint_name(
331 Some(tool_name.as_str())
333 // FIXME: can we suggest similar lint names here?
334 // https://github.com/rust-lang/rust/pull/56992
335 CheckLintNameResult::NoLint(None) => (),
339 "lowercase the lint name",
341 Applicability::MaybeIncorrect,
353 fn is_relevant_item(cx: &LateContext<'_, '_>, item: &Item) -> bool {
354 if let ItemKind::Fn(_, _, _, eid) = item.node {
355 is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value)
361 fn is_relevant_impl(cx: &LateContext<'_, '_>, item: &ImplItem) -> bool {
363 ImplItemKind::Method(_, eid) => is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value),
368 fn is_relevant_trait(cx: &LateContext<'_, '_>, item: &TraitItem) -> bool {
370 TraitItemKind::Method(_, TraitMethod::Required(_)) => true,
371 TraitItemKind::Method(_, TraitMethod::Provided(eid)) => {
372 is_relevant_expr(cx, cx.tcx.body_tables(eid), &cx.tcx.hir().body(eid).value)
378 fn is_relevant_block(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
379 if let Some(stmt) = block.stmts.first() {
381 StmtKind::Local(_) => true,
382 StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, tables, expr),
386 block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e))
390 fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
392 ExprKind::Block(block, _) => is_relevant_block(cx, tables, block),
393 ExprKind::Ret(Some(e)) => is_relevant_expr(cx, tables, e),
394 ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
395 ExprKind::Call(path_expr, _) => {
396 if let ExprKind::Path(qpath) = &path_expr.node {
397 if let Some(fun_id) = tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
398 !cx.match_def_path(fun_id, &paths::BEGIN_PANIC)
410 fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attribute]) {
411 if in_macro_or_desugar(span) {
416 if attr.is_sugared_doc {
419 if attr.style == AttrStyle::Outer {
420 if attr.tokens.is_empty() || !is_present_in_source(cx, attr.span) {
424 let begin_of_attr_to_item = Span::new(attr.span.lo(), span.lo(), span.ctxt());
425 let end_of_attr_to_item = Span::new(attr.span.hi(), span.lo(), span.ctxt());
427 if let Some(snippet) = snippet_opt(cx, end_of_attr_to_item) {
428 let lines = snippet.split('\n').collect::<Vec<_>>();
429 let lines = without_block_comments(lines);
431 if lines.iter().filter(|l| l.trim().is_empty()).count() > 2 {
434 EMPTY_LINE_AFTER_OUTER_ATTR,
435 begin_of_attr_to_item,
436 "Found an empty line after an outer attribute. \
437 Perhaps you forgot to add a '!' to make it an inner attribute?",
443 if let Some(values) = attr.meta_item_list() {
444 if values.len() != 1 || !attr.check_name("inline") {
447 if is_word(&values[0], "always") {
453 "you have declared `#[inline(always)]` on `{}`. This is usually a bad idea",
462 fn check_semver(cx: &LateContext<'_, '_>, span: Span, lit: &Lit) {
463 if let LitKind::Str(is, _) = lit.node {
464 if Version::parse(&is.as_str()).is_ok() {
472 "the since field must contain a semver-compliant version",
476 fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool {
477 if let NestedMetaItem::MetaItem(mi) = &nmi {
478 mi.is_word() && mi.check_name(expected)
484 declare_lint_pass!(DeprecatedCfgAttribute => [DEPRECATED_CFG_ATTR]);
486 impl EarlyLintPass for DeprecatedCfgAttribute {
487 fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
490 if attr.check_name("cfg_attr");
491 if let Some(items) = attr.meta_item_list();
493 // check for `rustfmt`
494 if let Some(feature_item) = items[0].meta_item();
495 if feature_item.check_name("rustfmt");
496 // check for `rustfmt_skip` and `rustfmt::skip`
497 if let Some(skip_item) = &items[1].meta_item();
498 if skip_item.check_name("rustfmt_skip") ||
499 skip_item.path.segments.last().expect("empty path in attribute").ident.name == "skip";
500 // Only lint outer attributes, because custom inner attributes are unstable
501 // Tracking issue: https://github.com/rust-lang/rust/issues/54726
502 if let AttrStyle::Outer = attr.style;
508 "`cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes",
510 "#[rustfmt::skip]".to_string(),
511 Applicability::MachineApplicable,