Skip to content

Commit e1cf160

Browse files
committed
Add cfg_attr(rustfmt) lint
1 parent dc1e409 commit e1cf160

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

clippy_lints/src/attrs.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
1313
use crate::reexport::*;
1414
use crate::utils::{
15-
in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint,
15+
in_macro, last_line_of_span, match_def_path, opt_def_id, paths, snippet_opt, span_lint, span_lint_and_sugg,
1616
span_lint_and_then, without_block_comments,
1717
};
1818
use if_chain::if_chain;
1919
use crate::rustc::hir::*;
2020
use crate::rustc::lint::{
21-
CheckLintNameResult, LateContext, LateLintPass, LintArray, LintContext, LintPass,
21+
CheckLintNameResult, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass,
2222
};
2323
use crate::rustc::ty::{self, TyCtxt};
2424
use crate::rustc::{declare_tool_lint, lint_array};
@@ -169,6 +169,35 @@ declare_clippy_lint! {
169169
"unknown_lints for scoped Clippy lints"
170170
}
171171

172+
/// **What it does:** Checks for `#[cfg_attr(rustfmt, rustfmt_skip)]` and suggests to replace it
173+
/// with `#[rustfmt::skip]`.
174+
///
175+
/// **Why is this bad?** Since tool_attributes (rust-lang/rust#44690) are stable now, they should
176+
/// be used instead of the old `cfg_attr(rustfmt)` attribute.
177+
///
178+
/// **Known problems:** It currently only detects outer attributes. But since it does not really
179+
/// makes sense to have `#![cfg_attr(rustfmt, rustfmt_skip)]` as an inner attribute, this should be
180+
/// ok.
181+
///
182+
/// **Example:**
183+
///
184+
/// Bad:
185+
/// ```rust
186+
/// #[cfg_attr(rustfmt, rustfmt_skip)]
187+
/// fn main() { }
188+
/// ```
189+
///
190+
/// Good:
191+
/// ```rust
192+
/// #[rustfmt::skip]
193+
/// fn main() { }
194+
/// ```
195+
declare_clippy_lint! {
196+
pub DEPRECATED_CFG_ATTR,
197+
complexity,
198+
"usage of `cfg_attr(rustfmt)` instead of `tool_attributes`"
199+
}
200+
172201
#[derive(Copy, Clone)]
173202
pub struct AttrPass;
174203

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ pub fn register_pre_expansion_lints(session: &rustc::session::Session, store: &m
220220
store.register_pre_expansion_pass(Some(session), box non_expressive_names::NonExpressiveNames {
221221
single_char_binding_names_threshold: conf.single_char_binding_names_threshold,
222222
});
223+
store.register_pre_expansion_pass(Some(session), box attrs::CfgAttrPass);
223224
}
224225

225226
pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
@@ -532,6 +533,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
532533
approx_const::APPROX_CONSTANT,
533534
assign_ops::ASSIGN_OP_PATTERN,
534535
assign_ops::MISREFACTORED_ASSIGN_OP,
536+
attrs::DEPRECATED_CFG_ATTR,
535537
attrs::DEPRECATED_SEMVER,
536538
attrs::UNKNOWN_CLIPPY_LINTS,
537539
attrs::USELESS_ATTRIBUTE,
@@ -839,6 +841,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
839841

840842
reg.register_lint_group("clippy::complexity", Some("clippy_complexity"), vec![
841843
assign_ops::MISREFACTORED_ASSIGN_OP,
844+
attrs::DEPRECATED_CFG_ATTR,
842845
booleans::NONMINIMAL_BOOL,
843846
cyclomatic_complexity::CYCLOMATIC_COMPLEXITY,
844847
double_comparison::DOUBLE_COMPARISONS,

0 commit comments

Comments
 (0)