Skip to content

Commit 825beb0

Browse files
committed
Remove the pre-expansion pass
1 parent 0029a91 commit 825beb0

16 files changed

+222
-175
lines changed

clippy_lints/src/attrs/deprecated_cfg_attr.rs

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use super::{Attribute, DEPRECATED_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR, unnecessary_clippy_cfg};
2-
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
33
use clippy_utils::msrvs::{self, MsrvStack};
44
use clippy_utils::sym;
55
use rustc_ast::AttrStyle;
66
use rustc_errors::Applicability;
7-
use rustc_lint::EarlyContext;
7+
use rustc_hir::attrs::CfgEntry;
8+
use rustc_hir::def_id::{LOCAL_CRATE, LocalDefId};
9+
use rustc_lint::{EarlyContext, LateContext};
810

9-
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
11+
pub(super) fn check_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
1012
// check cfg_attr
11-
if attr.has_name(sym::cfg_attr)
13+
if attr.has_name(sym::cfg_attr_trace)
1214
&& let Some(items) = attr.meta_item_list()
1315
&& items.len() == 2
1416
&& let Some(feature_item) = items[0].meta_item()
@@ -27,18 +29,23 @@ pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
2729
.ident
2830
.name
2931
== sym::skip)
30-
// Only lint outer attributes, because custom inner attributes are unstable
31-
// Tracking issue: https://github.com/rust-lang/rust/issues/54726
32-
&& attr.style == AttrStyle::Outer
3332
{
34-
span_lint_and_sugg(
33+
span_lint_and_then(
3534
cx,
3635
DEPRECATED_CFG_ATTR,
3736
attr.span,
38-
"`cfg_attr` is deprecated for rustfmt and got replaced by tool attributes",
39-
"use",
40-
"#[rustfmt::skip]".to_string(),
41-
Applicability::MachineApplicable,
37+
"`cfg_attr` is deprecated for rustfmt",
38+
|diag| {
39+
diag.span_suggestion_verbose(
40+
attr.span,
41+
"use the `rustfmt::skip` tool attribute instead",
42+
match attr.style {
43+
AttrStyle::Outer => "#[rustfmt::skip]",
44+
AttrStyle::Inner => "#![rustfmt::skip]",
45+
},
46+
Applicability::MaybeIncorrect, // See Known Problems
47+
);
48+
},
4249
);
4350
} else {
4451
check_deprecated_cfg_recursively(cx, feature_item);
@@ -49,8 +56,8 @@ pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &MsrvStack) {
4956
}
5057
}
5158

52-
pub(super) fn check_clippy(cx: &EarlyContext<'_>, attr: &Attribute) {
53-
if attr.has_name(sym::cfg)
59+
pub(super) fn check_cfg(cx: &EarlyContext<'_>, attr: &Attribute) {
60+
if attr.has_name(sym::cfg_trace)
5461
&& let Some(list) = attr.meta_item_list()
5562
{
5663
for item in list.iter().filter_map(|item| item.meta_item()) {
@@ -85,3 +92,40 @@ fn check_cargo_clippy_attr(cx: &EarlyContext<'_>, item: &rustc_ast::MetaItem) {
8592
);
8693
}
8794
}
95+
96+
pub fn check_stripped(cx: &LateContext<'_>) {
97+
for stripped in cx.tcx.stripped_cfg_items(LOCAL_CRATE) {
98+
if let Some(parent_module) = stripped.parent_module.as_local() {
99+
check_cfg_entry(cx, &stripped.cfg.0, parent_module);
100+
}
101+
}
102+
}
103+
104+
fn check_cfg_entry(cx: &LateContext<'_>, cfg: &CfgEntry, parent_module: LocalDefId) {
105+
match cfg {
106+
&CfgEntry::NameValue {
107+
name: sym::feature,
108+
name_span: _,
109+
value: Some((sym::cargo_clippy, _)),
110+
span,
111+
} => {
112+
span_lint_hir_and_then(
113+
cx,
114+
DEPRECATED_CLIPPY_CFG_ATTR,
115+
cx.tcx.local_def_id_to_hir_id(parent_module),
116+
span,
117+
"`feature = \"cargo-clippy\"` was replaced by `clippy`",
118+
|diag| {
119+
diag.span_suggestion(span, "replace with", "clippy", Applicability::MachineApplicable);
120+
},
121+
);
122+
},
123+
CfgEntry::All(children, _) | CfgEntry::Any(children, _) => {
124+
for child in children {
125+
check_cfg_entry(cx, child, parent_module);
126+
}
127+
},
128+
CfgEntry::Not(child, _) => check_cfg_entry(cx, child, parent_module),
129+
_ => {},
130+
}
131+
}

clippy_lints/src/attrs/mod.rs

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ declare_clippy_lint! {
160160
/// are stable now, they should be used instead of the old `cfg_attr(rustfmt)` attributes.
161161
///
162162
/// ### Known problems
163-
/// This lint doesn't detect crate level inner attributes, because they get
164-
/// processed before the PreExpansionPass lints get executed. See
165-
/// [#3123](https://github.com/rust-lang/rust-clippy/pull/3123#issuecomment-422321765)
163+
/// `cfg_attr` is valid in some places that `rustfmt::skip` is not, e.g. outer attributes on
164+
/// blocks and inner attributes on inline modules.
166165
///
167166
/// ### Example
168167
/// ```no_run
@@ -474,22 +473,27 @@ declare_clippy_lint! {
474473
"ignored tests without messages"
475474
}
476475

477-
pub struct Attributes {
476+
pub struct LateAttributes {
478477
msrv: Msrv,
479478
}
480479

481-
impl_lint_pass!(Attributes => [
480+
impl_lint_pass!(LateAttributes => [
482481
INLINE_ALWAYS,
483482
REPR_PACKED_WITHOUT_ABI,
483+
DEPRECATED_CLIPPY_CFG_ATTR,
484484
]);
485485

486-
impl Attributes {
486+
impl LateAttributes {
487487
pub fn new(conf: &'static Conf) -> Self {
488488
Self { msrv: conf.msrv }
489489
}
490490
}
491491

492-
impl<'tcx> LateLintPass<'tcx> for Attributes {
492+
impl<'tcx> LateLintPass<'tcx> for LateAttributes {
493+
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
494+
deprecated_cfg_attr::check_stripped(cx);
495+
}
496+
493497
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
494498
let attrs = cx.tcx.hir_attrs(item.hir_id());
495499
if let ItemKind::Fn { ident, .. } = item.kind
@@ -526,35 +530,6 @@ impl EarlyAttributes {
526530
}
527531

528532
impl_lint_pass!(EarlyAttributes => [
529-
DEPRECATED_CFG_ATTR,
530-
NON_MINIMAL_CFG,
531-
DEPRECATED_CLIPPY_CFG_ATTR,
532-
UNNECESSARY_CLIPPY_CFG,
533-
]);
534-
535-
impl EarlyLintPass for EarlyAttributes {
536-
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
537-
deprecated_cfg_attr::check(cx, attr, &self.msrv);
538-
deprecated_cfg_attr::check_clippy(cx, attr);
539-
non_minimal_cfg::check(cx, attr);
540-
}
541-
542-
extract_msrv_attr!();
543-
}
544-
545-
pub struct PostExpansionEarlyAttributes {
546-
msrv: MsrvStack,
547-
}
548-
549-
impl PostExpansionEarlyAttributes {
550-
pub fn new(conf: &'static Conf) -> Self {
551-
Self {
552-
msrv: MsrvStack::new(conf.msrv),
553-
}
554-
}
555-
}
556-
557-
impl_lint_pass!(PostExpansionEarlyAttributes => [
558533
ALLOW_ATTRIBUTES,
559534
ALLOW_ATTRIBUTES_WITHOUT_REASON,
560535
DEPRECATED_SEMVER,
@@ -564,15 +539,23 @@ impl_lint_pass!(PostExpansionEarlyAttributes => [
564539
SHOULD_PANIC_WITHOUT_EXPECT,
565540
MIXED_ATTRIBUTES_STYLE,
566541
DUPLICATED_ATTRIBUTES,
542+
DEPRECATED_CFG_ATTR,
543+
DEPRECATED_CLIPPY_CFG_ATTR,
544+
UNNECESSARY_CLIPPY_CFG,
545+
NON_MINIMAL_CFG,
567546
]);
568547

569-
impl EarlyLintPass for PostExpansionEarlyAttributes {
548+
impl EarlyLintPass for EarlyAttributes {
570549
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &ast::Crate) {
571550
blanket_clippy_restriction_lints::check_command_line(cx);
572551
duplicated_attributes::check(cx, &krate.attrs);
573552
}
574553

575554
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
555+
deprecated_cfg_attr::check_cfg_attr(cx, attr, &self.msrv);
556+
deprecated_cfg_attr::check_cfg(cx, attr);
557+
non_minimal_cfg::check(cx, attr);
558+
576559
if let Some(items) = &attr.meta_item_list()
577560
&& let Some(ident) = attr.ident()
578561
{

clippy_lints/src/attrs/non_minimal_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_lint::EarlyContext;
77
use rustc_span::sym;
88

99
pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute) {
10-
if attr.has_name(sym::cfg)
10+
if attr.has_name(sym::cfg_trace)
1111
&& let Some(items) = attr.meta_item_list()
1212
{
1313
check_nested_cfg(cx, &items);

clippy_lints/src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
442442
store.register_removed(name, reason);
443443
}
444444

445-
// NOTE: Do not add any more pre-expansion passes. These should be removed eventually.
446-
// Due to the architecture of the compiler, currently `cfg_attr` attributes on crate
447-
// level (i.e `#![cfg_attr(...)]`) will still be expanded even when using a pre-expansion pass.
448-
store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes::new(conf)));
449-
450-
store.register_early_pass(move || Box::new(attrs::PostExpansionEarlyAttributes::new(conf)));
445+
store.register_early_pass(move || Box::new(attrs::EarlyAttributes::new(conf)));
451446

452447
let format_args_storage = FormatArgsStorage::default();
453448
let format_args = format_args_storage.clone();
@@ -480,7 +475,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
480475
store.register_late_pass(|_| Box::new(unnecessary_mut_passed::UnnecessaryMutPassed));
481476
store.register_late_pass(|_| Box::<significant_drop_tightening::SignificantDropTightening<'_>>::default());
482477
store.register_late_pass(move |_| Box::new(len_zero::LenZero::new(conf)));
483-
store.register_late_pass(move |_| Box::new(attrs::Attributes::new(conf)));
478+
store.register_late_pass(move |_| Box::new(attrs::LateAttributes::new(conf)));
484479
store.register_late_pass(|_| Box::new(blocks_in_conditions::BlocksInConditions));
485480
store.register_late_pass(|_| Box::new(unicode::Unicode));
486481
store.register_late_pass(|_| Box::new(uninit_vec::UninitVec));

tests/ui/cfg_attr_cargo_clippy.fixed

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77
//~^ deprecated_clippy_cfg_attr
88
#[cfg_attr(not(clippy), derive(Debug))]
99
//~^ deprecated_clippy_cfg_attr
10-
#[cfg(clippy)]
11-
//~^ deprecated_clippy_cfg_attr
10+
struct Attrs;
11+
1212
#[cfg(not(clippy))]
1313
//~^ deprecated_clippy_cfg_attr
14+
struct Not;
15+
16+
#[cfg(clippy)]
17+
//~^ deprecated_clippy_cfg_attr
18+
struct Stripped;
19+
1420
#[cfg(any(clippy))]
1521
//~^ deprecated_clippy_cfg_attr
22+
struct StrippedAny;
23+
1624
#[cfg(all(clippy))]
1725
//~^ deprecated_clippy_cfg_attr
18-
pub struct Bar;
26+
struct StrippedAll;
1927

2028
fn main() {}

tests/ui/cfg_attr_cargo_clippy.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77
//~^ deprecated_clippy_cfg_attr
88
#[cfg_attr(not(feature = "cargo-clippy"), derive(Debug))]
99
//~^ deprecated_clippy_cfg_attr
10-
#[cfg(feature = "cargo-clippy")]
11-
//~^ deprecated_clippy_cfg_attr
10+
struct Attrs;
11+
1212
#[cfg(not(feature = "cargo-clippy"))]
1313
//~^ deprecated_clippy_cfg_attr
14+
struct Not;
15+
16+
#[cfg(feature = "cargo-clippy")]
17+
//~^ deprecated_clippy_cfg_attr
18+
struct Stripped;
19+
1420
#[cfg(any(feature = "cargo-clippy"))]
1521
//~^ deprecated_clippy_cfg_attr
22+
struct StrippedAny;
23+
1624
#[cfg(all(feature = "cargo-clippy"))]
1725
//~^ deprecated_clippy_cfg_attr
18-
pub struct Bar;
26+
struct StrippedAll;
1927

2028
fn main() {}

tests/ui/cfg_attr_cargo_clippy.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ error: `feature = "cargo-clippy"` was replaced by `clippy`
1919
LL | #[cfg_attr(not(feature = "cargo-clippy"), derive(Debug))]
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `clippy`
2121

22-
error: `feature = "cargo-clippy"` was replaced by `clippy`
23-
--> tests/ui/cfg_attr_cargo_clippy.rs:10:7
24-
|
25-
LL | #[cfg(feature = "cargo-clippy")]
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `clippy`
27-
2822
error: `feature = "cargo-clippy"` was replaced by `clippy`
2923
--> tests/ui/cfg_attr_cargo_clippy.rs:12:11
3024
|
3125
LL | #[cfg(not(feature = "cargo-clippy"))]
3226
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `clippy`
3327

3428
error: `feature = "cargo-clippy"` was replaced by `clippy`
35-
--> tests/ui/cfg_attr_cargo_clippy.rs:14:11
29+
--> tests/ui/cfg_attr_cargo_clippy.rs:16:7
30+
|
31+
LL | #[cfg(feature = "cargo-clippy")]
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `clippy`
33+
34+
error: `feature = "cargo-clippy"` was replaced by `clippy`
35+
--> tests/ui/cfg_attr_cargo_clippy.rs:20:11
3636
|
3737
LL | #[cfg(any(feature = "cargo-clippy"))]
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `clippy`
3939

4040
error: `feature = "cargo-clippy"` was replaced by `clippy`
41-
--> tests/ui/cfg_attr_cargo_clippy.rs:16:11
41+
--> tests/ui/cfg_attr_cargo_clippy.rs:24:11
4242
|
4343
LL | #[cfg(all(feature = "cargo-clippy"))]
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `clippy`

tests/ui/cfg_attr_rustfmt.fixed

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
2-
#![feature(stmt_expr_attributes)]
3-
4-
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
1+
#![feature(stmt_expr_attributes, custom_inner_attributes)]
52
#![warn(clippy::deprecated_cfg_attr)]
63

7-
// This doesn't get linted, see known problems
8-
#![cfg_attr(rustfmt, rustfmt_skip)]
4+
#![rustfmt::skip]
5+
//~^ deprecated_cfg_attr
96

107
#[rustfmt::skip]
118
trait Foo
@@ -14,33 +11,25 @@ fn foo(
1411
);
1512
}
1613

17-
fn skip_on_statements() {
18-
#[rustfmt::skip]
19-
//~^ deprecated_cfg_attr
20-
5+3;
21-
}
22-
2314
#[rustfmt::skip]
2415
//~^ deprecated_cfg_attr
25-
fn main() {
26-
foo::f();
27-
}
16+
fn outer() {}
2817

29-
mod foo {
30-
#![cfg_attr(rustfmt, rustfmt_skip)]
18+
// Ideally these would not lint
19+
fn invalid() {
20+
#![rustfmt::skip]
21+
//~^ deprecated_cfg_attr
3122

32-
pub fn f() {}
23+
#[rustfmt::skip]
24+
//~^ deprecated_cfg_attr
25+
{}
3326
}
3427

3528
#[clippy::msrv = "1.29"]
36-
fn msrv_1_29() {
37-
#[cfg_attr(rustfmt, rustfmt::skip)]
38-
1+29;
39-
}
29+
#[cfg_attr(rustfmt, rustfmt::skip)]
30+
fn msrv_1_29() {}
4031

4132
#[clippy::msrv = "1.30"]
42-
fn msrv_1_30() {
43-
#[rustfmt::skip]
44-
//~^ deprecated_cfg_attr
45-
1+30;
46-
}
33+
#[rustfmt::skip]
34+
//~^ deprecated_cfg_attr
35+
fn msrv_1_30() {}

0 commit comments

Comments
 (0)