Skip to content

Commit df06202

Browse files
committed
fix(14017): Process attributes entirely in late pass
1 parent e622300 commit df06202

File tree

3 files changed

+109
-44
lines changed

3 files changed

+109
-44
lines changed

clippy_lints/src/disallowed_macros.rs

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
44
use clippy_utils::macros::macro_backtrace;
55
use clippy_utils::paths::PathNS;
66
use rustc_data_structures::fx::FxHashSet;
7+
use rustc_hir::attrs::AttributeKind;
78
use rustc_hir::def::DefKind;
89
use rustc_hir::def_id::DefIdMap;
910
use rustc_hir::{
10-
AmbigArg, Expr, ExprKind, ForeignItem, HirId, ImplItem, Item, ItemKind, OwnerId, Pat, Path, Stmt, TraitItem, Ty,
11+
AmbigArg, Attribute, Expr, ExprKind, ForeignItem, HirId, ImplItem, Item, ItemKind, OwnerId, Pat, Path, Stmt,
12+
TraitItem, Ty,
1113
};
1214
use rustc_lint::{LateContext, LateLintPass};
1315
use rustc_middle::ty::TyCtxt;
1416
use rustc_session::impl_lint_pass;
1517
use rustc_span::{ExpnId, MacroKind, Span};
1618

17-
use crate::utils::attr_collector::AttrStorage;
18-
1919
declare_clippy_lint! {
2020
/// ### What it does
2121
/// Denies the configured macros in clippy.toml
@@ -68,14 +68,10 @@ pub struct DisallowedMacros {
6868
// Track the most recently seen node that can have a `derive` attribute.
6969
// Needed to use the correct lint level.
7070
derive_src: Option<OwnerId>,
71-
72-
// When a macro is disallowed in an early pass, it's stored
73-
// and emitted during the late pass. This happens for attributes.
74-
early_macro_cache: AttrStorage,
7571
}
7672

7773
impl DisallowedMacros {
78-
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf, early_macro_cache: AttrStorage) -> Self {
74+
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
7975
let (disallowed, _) = create_disallowed_map(
8076
tcx,
8177
&conf.disallowed_macros,
@@ -88,7 +84,6 @@ impl DisallowedMacros {
8884
disallowed,
8985
seen: FxHashSet::default(),
9086
derive_src: None,
91-
early_macro_cache,
9287
}
9388
}
9489

@@ -125,15 +120,85 @@ impl DisallowedMacros {
125120
}
126121

127122
impl_lint_pass!(DisallowedMacros => [DISALLOWED_MACROS]);
128-
129123
impl LateLintPass<'_> for DisallowedMacros {
130-
fn check_crate(&mut self, cx: &LateContext<'_>) {
131-
// once we check a crate in the late pass we can emit the early pass lints
132-
if let Some(attr_spans) = self.early_macro_cache.clone().0.get() {
133-
for span in attr_spans {
134-
self.check(cx, *span, None);
135-
}
136-
}
124+
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &Attribute) {
125+
let span = match attr {
126+
Attribute::Unparsed(attr_item) => attr_item.span,
127+
Attribute::Parsed(kind) => {
128+
match kind {
129+
AttributeKind::Align { span, .. }
130+
| AttributeKind::AllowConstFnUnstable(_, span)
131+
| AttributeKind::AllowIncoherentImpl(span)
132+
| AttributeKind::AllowInternalUnstable(_, span)
133+
| AttributeKind::AsPtr(span)
134+
| AttributeKind::AutomaticallyDerived(span)
135+
| AttributeKind::BodyStability { span, .. }
136+
| AttributeKind::Coinductive(span)
137+
| AttributeKind::Cold(span)
138+
| AttributeKind::Confusables { first_span: span, .. }
139+
| AttributeKind::ConstContinue(span)
140+
| AttributeKind::ConstStability { span, .. }
141+
| AttributeKind::ConstTrait(span)
142+
| AttributeKind::Coverage(span, _)
143+
| AttributeKind::DenyExplicitImpl(span)
144+
| AttributeKind::Deprecation { span, .. }
145+
| AttributeKind::DoNotImplementViaObject(span)
146+
| AttributeKind::DocComment { span, .. }
147+
| AttributeKind::ExportName { span, .. }
148+
| AttributeKind::FfiConst(span)
149+
| AttributeKind::FfiPure(span)
150+
| AttributeKind::Ignore { span, .. }
151+
| AttributeKind::Inline(_, span)
152+
| AttributeKind::LinkName { span, .. }
153+
| AttributeKind::LinkOrdinal { span, .. }
154+
| AttributeKind::LinkSection { span, .. }
155+
| AttributeKind::LoopMatch(span)
156+
| AttributeKind::MacroEscape(span)
157+
| AttributeKind::MacroUse { span, .. }
158+
| AttributeKind::Marker(span)
159+
| AttributeKind::MayDangle(span)
160+
| AttributeKind::MustUse { span, .. }
161+
| AttributeKind::Naked(span)
162+
| AttributeKind::NoImplicitPrelude(span)
163+
| AttributeKind::NoMangle(span)
164+
| AttributeKind::NonExhaustive(span)
165+
| AttributeKind::Optimize(_, span)
166+
| AttributeKind::ParenSugar(span)
167+
| AttributeKind::PassByValue(span)
168+
| AttributeKind::Path(_, span)
169+
| AttributeKind::Pointee(span)
170+
| AttributeKind::ProcMacro(span)
171+
| AttributeKind::ProcMacroAttribute(span)
172+
| AttributeKind::ProcMacroDerive { span, .. }
173+
| AttributeKind::PubTransparent(span)
174+
| AttributeKind::Repr { first_span: span, .. }
175+
| AttributeKind::RustcBuiltinMacro { span, .. }
176+
| AttributeKind::RustcLayoutScalarValidRangeEnd(_, span)
177+
| AttributeKind::RustcLayoutScalarValidRangeStart(_, span)
178+
| AttributeKind::SkipDuringMethodDispatch { span, .. }
179+
| AttributeKind::SpecializationTrait(span)
180+
| AttributeKind::Stability { span, .. }
181+
| AttributeKind::StdInternalSymbol(span)
182+
| AttributeKind::TargetFeature(_, span)
183+
| AttributeKind::TrackCaller(span)
184+
| AttributeKind::TypeConst(span)
185+
| AttributeKind::UnsafeSpecializationMarker(span)
186+
| AttributeKind::Used { span, .. } => *span,
187+
188+
AttributeKind::CoherenceIsCore
189+
| AttributeKind::ConstStabilityIndirect
190+
| AttributeKind::Dummy
191+
| AttributeKind::ExportStable
192+
| AttributeKind::Fundamental
193+
| AttributeKind::MacroTransparency(_)
194+
| AttributeKind::RustcObjectLifetimeDefault
195+
| AttributeKind::UnstableFeatureBound(_) => {
196+
return;
197+
},
198+
}
199+
},
200+
};
201+
self.check(cx, span, self.derive_src);
137202
}
138203

139204
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {

clippy_lints/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
657657
store.register_late_pass(|_| Box::new(unwrap_in_result::UnwrapInResult));
658658
store.register_late_pass(|_| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned));
659659
store.register_late_pass(|_| Box::new(async_yields_async::AsyncYieldsAsync));
660-
let attrs = attr_storage.clone();
661-
store.register_late_pass(move |tcx| Box::new(disallowed_macros::DisallowedMacros::new(tcx, conf, attrs.clone())));
660+
store.register_late_pass(move |tcx| Box::new(disallowed_macros::DisallowedMacros::new(tcx, conf)));
662661
store.register_late_pass(move |tcx| Box::new(disallowed_methods::DisallowedMethods::new(tcx, conf)));
663662
store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86AttSyntax));
664663
store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86IntelSyntax));

tests/ui-toml/disallowed_macros/disallowed_macros.stderr

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
1-
error: use of a disallowed macro `serde::Serialize`
2-
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:22:14
3-
|
4-
LL | #[derive(Serialize)]
5-
| ^^^^^^^^^
6-
|
7-
= note: no serializing
8-
= note: `-D clippy::disallowed-macros` implied by `-D warnings`
9-
= help: to override `-D warnings` add `#[allow(clippy::disallowed_macros)]`
10-
11-
error: use of a disallowed macro `macros::attr`
12-
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:42:1
13-
|
14-
LL | / macros::attr! {
15-
LL | |
16-
LL | | struct S;
17-
LL | | }
18-
| |_^
19-
20-
error: use of a disallowed macro `proc_macros::Derive`
21-
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:62:10
22-
|
23-
LL | #[derive(Derive)]
24-
| ^^^^^^
25-
261
error: use of a disallowed macro `std::println`
272
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:13:5
283
|
294
LL | println!("one");
305
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::disallowed-macros` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::disallowed_macros)]`
319

3210
error: use of a disallowed macro `std::println`
3311
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:15:5
@@ -47,6 +25,14 @@ error: use of a disallowed macro `std::vec`
4725
LL | vec![1, 2, 3];
4826
| ^^^^^^^^^^^^^
4927

28+
error: use of a disallowed macro `serde::Serialize`
29+
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:22:14
30+
|
31+
LL | #[derive(Serialize)]
32+
| ^^^^^^^^^
33+
|
34+
= note: no serializing
35+
5036
error: use of a disallowed macro `macros::expr`
5137
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:26:13
5238
|
@@ -83,6 +69,15 @@ error: use of a disallowed macro `macros::binop`
8369
LL | let _ = macros::binop!(1);
8470
| ^^^^^^^^^^^^^^^^^
8571

72+
error: use of a disallowed macro `macros::attr`
73+
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:42:1
74+
|
75+
LL | / macros::attr! {
76+
LL | |
77+
LL | | struct S;
78+
LL | | }
79+
| |_^
80+
8681
error: use of a disallowed macro `macros::item`
8782
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:48:5
8883
|
@@ -101,5 +96,11 @@ error: use of a disallowed macro `macros::item`
10196
LL | macros::item!();
10297
| ^^^^^^^^^^^^^^^
10398

99+
error: use of a disallowed macro `proc_macros::Derive`
100+
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:62:10
101+
|
102+
LL | #[derive(Derive)]
103+
| ^^^^^^
104+
104105
error: aborting due to 16 previous errors
105106

0 commit comments

Comments
 (0)