Skip to content

Commit 16fdda3

Browse files
committed
temp: fix disallowed macros
1 parent 269d5b5 commit 16fdda3

File tree

6 files changed

+229
-45
lines changed

6 files changed

+229
-45
lines changed

src/tools/clippy/clippy_lints/src/disallowed_macros.rs

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
21
use clippy_config::Conf;
32
use clippy_config::types::{DisallowedPath, create_disallowed_map};
43
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
54
use clippy_utils::macros::macro_backtrace;
65
use clippy_utils::paths::PathNS;
76
use rustc_data_structures::fx::FxHashSet;
7+
use rustc_hir::attrs::AttributeKind;
88
use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::DefIdMap;
1010
use rustc_hir::{
11-
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,
1213
};
1314
use rustc_lint::{LateContext, LateLintPass};
1415
use rustc_middle::ty::TyCtxt;
1516
use rustc_session::impl_lint_pass;
1617
use rustc_span::{ExpnId, MacroKind, Span};
1718

18-
use crate::utils::attr_collector::AttrStorage;
19-
2019
declare_clippy_lint! {
2120
/// ### What it does
2221
/// Denies the configured macros in clippy.toml
@@ -69,14 +68,10 @@ pub struct DisallowedMacros {
6968
// Track the most recently seen node that can have a `derive` attribute.
7069
// Needed to use the correct lint level.
7170
derive_src: Option<OwnerId>,
72-
73-
// When a macro is disallowed in an early pass, it's stored
74-
// and emitted during the late pass. This happens for attributes.
75-
early_macro_cache: AttrStorage,
7671
}
7772

7873
impl DisallowedMacros {
79-
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf, early_macro_cache: AttrStorage) -> Self {
74+
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
8075
let (disallowed, _) = create_disallowed_map(
8176
tcx,
8277
&conf.disallowed_macros,
@@ -89,7 +84,6 @@ impl DisallowedMacros {
8984
disallowed,
9085
seen: FxHashSet::default(),
9186
derive_src: None,
92-
early_macro_cache,
9387
}
9488
}
9589

@@ -126,15 +120,87 @@ impl DisallowedMacros {
126120
}
127121

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

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

src/tools/clippy/clippy_lints/src/lib.rs

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

src/tools/clippy/tests/ui-toml/disallowed_macros/clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
disallowed-macros = [
2+
"std::panic",
23
"std::println",
34
"std::vec",
45
{ path = "std::cfg" },

src/tools/clippy/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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
fn test_allow_on_function() {
2+
#![allow(clippy::disallowed_macros)]
3+
4+
panic!();
5+
panic!("message");
6+
panic!("{}", 123);
7+
panic!("{} {}", 123, 456);
8+
println!("test");
9+
println!("{}", 123);
10+
vec![1, 2, 3];
11+
}
12+
13+
fn test_expect_on_function() {
14+
#![expect(clippy::disallowed_macros)]
15+
16+
panic!();
17+
panic!("message");
18+
panic!("{}", 123);
19+
panic!("{} {}", 123, 456);
20+
println!("test");
21+
println!("{}", 123);
22+
vec![1, 2, 3];
23+
}
24+
25+
fn test_no_attributes() {
26+
panic!();
27+
//~^ disallowed_macros
28+
panic!("message");
29+
//~^ disallowed_macros
30+
panic!("{}", 123);
31+
//~^ disallowed_macros
32+
panic!("{} {}", 123, 456);
33+
//~^ disallowed_macros
34+
println!("test");
35+
//~^ disallowed_macros
36+
println!("{}", 123);
37+
//~^ disallowed_macros
38+
vec![1, 2, 3];
39+
//~^ disallowed_macros
40+
}
41+
42+
#[allow(clippy::disallowed_macros)]
43+
mod allowed_module {
44+
pub fn test() {
45+
panic!();
46+
panic!("message");
47+
panic!("{}", 123);
48+
println!("test");
49+
vec![1, 2, 3];
50+
}
51+
}
52+
53+
#[expect(clippy::disallowed_macros)]
54+
mod expected_module {
55+
pub fn test() {
56+
panic!();
57+
panic!("message");
58+
panic!("{}", 123);
59+
println!("test");
60+
vec![1, 2, 3];
61+
}
62+
}
63+
64+
fn main() {
65+
test_allow_on_function();
66+
test_expect_on_function();
67+
test_no_attributes();
68+
allowed_module::test();
69+
expected_module::test();
70+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error: use of a disallowed macro `std::panic`
2+
--> tests/ui-toml/disallowed_macros/disallowed_macros_regression.rs:26:5
3+
|
4+
LL | panic!();
5+
| ^^^^^^^^
6+
|
7+
= note: `-D clippy::disallowed-macros` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::disallowed_macros)]`
9+
10+
error: use of a disallowed macro `std::panic`
11+
--> tests/ui-toml/disallowed_macros/disallowed_macros_regression.rs:28:5
12+
|
13+
LL | panic!("message");
14+
| ^^^^^^^^^^^^^^^^^
15+
16+
error: use of a disallowed macro `std::panic`
17+
--> tests/ui-toml/disallowed_macros/disallowed_macros_regression.rs:30:5
18+
|
19+
LL | panic!("{}", 123);
20+
| ^^^^^^^^^^^^^^^^^
21+
22+
error: use of a disallowed macro `std::panic`
23+
--> tests/ui-toml/disallowed_macros/disallowed_macros_regression.rs:32:5
24+
|
25+
LL | panic!("{} {}", 123, 456);
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
27+
28+
error: use of a disallowed macro `std::println`
29+
--> tests/ui-toml/disallowed_macros/disallowed_macros_regression.rs:34:5
30+
|
31+
LL | println!("test");
32+
| ^^^^^^^^^^^^^^^^
33+
34+
error: use of a disallowed macro `std::println`
35+
--> tests/ui-toml/disallowed_macros/disallowed_macros_regression.rs:36:5
36+
|
37+
LL | println!("{}", 123);
38+
| ^^^^^^^^^^^^^^^^^^^
39+
40+
error: use of a disallowed macro `std::vec`
41+
--> tests/ui-toml/disallowed_macros/disallowed_macros_regression.rs:38:5
42+
|
43+
LL | vec![1, 2, 3];
44+
| ^^^^^^^^^^^^^
45+
46+
error: aborting due to 7 previous errors
47+

0 commit comments

Comments
 (0)