Skip to content

Commit ebcea40

Browse files
bors[bot]flip1995
andcommitted
Merge #3123
3123: Lint #[cfg_attr(rustfmt, rustfmt_skip)] and suggest #[rustfmt::skip] r=phansch a=flip1995 Closes #3121 Co-authored-by: flip1995 <[email protected]> Co-authored-by: flip1995 <[email protected]>
2 parents dc1e409 + 318f84f commit ebcea40

File tree

6 files changed

+139
-3
lines changed

6 files changed

+139
-3
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ All notable changes to this project will be documented in this file.
651651
[`decimal_literal_representation`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#decimal_literal_representation
652652
[`declare_interior_mutable_const`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
653653
[`default_trait_access`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#default_trait_access
654+
[`deprecated_cfg_attr`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deprecated_cfg_attr
654655
[`deprecated_semver`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deprecated_semver
655656
[`deref_addrof`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#deref_addrof
656657
[`derive_hash_xor_eq`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#derive_hash_xor_eq

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ We are currently in the process of discussing Clippy 1.0 via the RFC process in
99

1010
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
1111

12-
[There are 286 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
12+
[There are 287 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
1313

1414
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1515

Diff for: clippy_lints/src/attrs.rs

+74-2
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](https://github.com/rust-lang/rust/issues/44690))
176+
/// are stable now, they should be used instead of the old `cfg_attr(rustfmt)` attributes.
177+
///
178+
/// **Known problems:** This lint doesn't detect crate level inner attributes, because they get
179+
/// processed before the PreExpansionPass lints get executed. See
180+
/// [#3123](https://github.com/rust-lang-nursery/rust-clippy/pull/3123#issuecomment-422321765)
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

@@ -466,3 +495,46 @@ fn is_present_in_source(cx: &LateContext<'_, '_>, span: Span) -> bool {
466495
}
467496
true
468497
}
498+
499+
#[derive(Copy, Clone)]
500+
pub struct CfgAttrPass;
501+
502+
impl LintPass for CfgAttrPass {
503+
fn get_lints(&self) -> LintArray {
504+
lint_array!(
505+
DEPRECATED_CFG_ATTR,
506+
)
507+
}
508+
}
509+
510+
impl EarlyLintPass for CfgAttrPass {
511+
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
512+
if_chain! {
513+
// check cfg_attr
514+
if attr.name() == "cfg_attr";
515+
if let Some(ref items) = attr.meta_item_list();
516+
if items.len() == 2;
517+
// check for `rustfmt`
518+
if let Some(feature_item) = items[0].meta_item();
519+
if feature_item.name() == "rustfmt";
520+
// check for `rustfmt_skip` and `rustfmt::skip`
521+
if let Some(skip_item) = &items[1].meta_item();
522+
if skip_item.name() == "rustfmt_skip" || skip_item.name() == "skip";
523+
then {
524+
let attr_style = match attr.style {
525+
AttrStyle::Outer => "#[",
526+
AttrStyle::Inner => "#![",
527+
};
528+
span_lint_and_sugg(
529+
cx,
530+
DEPRECATED_CFG_ATTR,
531+
attr.span,
532+
"`cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes",
533+
"use",
534+
format!("{}rustfmt::skip]", attr_style),
535+
);
536+
}
537+
}
538+
}
539+
}
540+

Diff for: clippy_lints/src/lib.rs

+3
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,

Diff for: tests/ui/cfg_attr_rustfmt.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
#![feature(stmt_expr_attributes)]
11+
12+
#![warn(clippy::deprecated_cfg_attr)]
13+
14+
// This doesn't get linted, see known problems
15+
#![cfg_attr(rustfmt, rustfmt_skip)]
16+
17+
#[rustfmt::skip]
18+
trait Foo
19+
{
20+
fn foo(
21+
);
22+
}
23+
24+
fn skip_on_statements() {
25+
#[cfg_attr(rustfmt, rustfmt::skip)]
26+
5+3;
27+
}
28+
29+
#[cfg_attr(rustfmt, rustfmt_skip)]
30+
fn main() {
31+
foo::f();
32+
}
33+
34+
mod foo {
35+
#![cfg_attr(rustfmt, rustfmt_skip)]
36+
37+
pub fn f() {}
38+
}

Diff for: tests/ui/cfg_attr_rustfmt.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
2+
--> $DIR/cfg_attr_rustfmt.rs:25:5
3+
|
4+
25 | #[cfg_attr(rustfmt, rustfmt::skip)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]`
6+
|
7+
= note: `-D clippy::deprecated-cfg-attr` implied by `-D warnings`
8+
9+
error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
10+
--> $DIR/cfg_attr_rustfmt.rs:29:1
11+
|
12+
29 | #[cfg_attr(rustfmt, rustfmt_skip)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#[rustfmt::skip]`
14+
15+
error: `cfg_attr` is deprecated for rustfmt and got replaced by tool_attributes
16+
--> $DIR/cfg_attr_rustfmt.rs:35:5
17+
|
18+
35 | #![cfg_attr(rustfmt, rustfmt_skip)]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `#![rustfmt::skip]`
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)