From e1cf160e2a2fba4cf7625dab1a52af5adfc534f5 Mon Sep 17 00:00:00 2001 From: flip1995 <9744647+flip1995@users.noreply.github.com> Date: Mon, 3 Sep 2018 15:34:12 +0200 Subject: [PATCH] Add cfg_attr(rustfmt) lint --- clippy_lints/src/attrs.rs | 33 +++++++++++++++++++++++++++++++-- clippy_lints/src/lib.rs | 3 +++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 89d676f3f..4ea13f649 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -12,13 +12,13 @@ use crate::reexport::*; use crate::utils::{ - in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, + in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_sugg, span_lint_and_then, without_block_comments, }; use if_chain::if_chain; use crate::rustc::hir::*; use crate::rustc::lint::{ - CheckLintNameResult, LateContext, LateLintPass, LintArray, LintContext, LintPass, + CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, }; use crate::rustc::ty::{self, TyCtxt}; use crate::rustc::{declare_tool_lint, lint_array}; @@ -169,6 +169,35 @@ declare_clippy_lint! { "unknown_lints for scoped Clippy lints" } +/// **What it does:** Checks for `#[cfg_attr(rustfmt, rustfmt_skip)]` and suggests to replace it +/// with `#[rustfmt::skip]`. +/// +/// **Why is this bad?** Since tool_attributes (rust-lang/rust#44690) are stable now, they should +/// be used instead of the old `cfg_attr(rustfmt)` attribute. +/// +/// **Known problems:** It currently only detects outer attributes. But since it does not really +/// makes sense to have `#![cfg_attr(rustfmt, rustfmt_skip)]` as an inner attribute, this should be +/// ok. +/// +/// **Example:** +/// +/// Bad: +/// ```rust +/// #[cfg_attr(rustfmt, rustfmt_skip)] +/// fn main() { } +/// ``` +/// +/// Good: +/// ```rust +/// #[rustfmt::skip] +/// fn main() { } +/// ``` +declare_clippy_lint! { + pub DEPRECATED_CFG_ATTR, + complexity, + "usage of `cfg_attr(rustfmt)` instead of `tool_attributes`" +} + #[derive(Copy, Clone)] pub struct AttrPass; diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 863c06234..f3fce54f9 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -220,6 +220,7 @@ pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &m store.register_pre_expansion_pass(Some(session), box non_expressive_names::NonExpressiveNames { single_char_binding_names_threshold: conf.single_char_binding_names_threshold, }); + store.register_pre_expansion_pass(Some(session), box attrs::CfgAttrPass); } pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf { @@ -532,6 +533,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { approx_const::APPROX_CONSTANT, assign_ops::ASSIGN_OP_PATTERN, assign_ops::MISREFACTORED_ASSIGN_OP, + attrs::DEPRECATED_CFG_ATTR, attrs::DEPRECATED_SEMVER, attrs::UNKNOWN_CLIPPY_LINTS, attrs::USELESS_ATTRIBUTE, @@ -839,6 +841,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_lint_group("clippy::complexity", Some("clippy_complexity"), vec![ assign_ops::MISREFACTORED_ASSIGN_OP, + attrs::DEPRECATED_CFG_ATTR, booleans::NONMINIMAL_BOOL, cyclomatic_complexity::CYCLOMATIC_COMPLEXITY, double_comparison::DOUBLE_COMPARISONS,