Skip to content

Commit c931096

Browse files
committed
collect attribute spans early for disallowed macros
1 parent e8b78e2 commit c931096

File tree

5 files changed

+98
-38
lines changed

5 files changed

+98
-38
lines changed

clippy_lints/src/disallowed_macros.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use clippy_config::Conf;
22
use clippy_utils::create_disallowed_map;
33
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
44
use clippy_utils::macros::macro_backtrace;
5-
use rustc_ast::Attribute;
65
use rustc_data_structures::fx::FxHashSet;
76
use rustc_errors::Diag;
87
use rustc_hir::def_id::DefIdMap;
@@ -14,6 +13,8 @@ use rustc_middle::ty::TyCtxt;
1413
use rustc_session::impl_lint_pass;
1514
use rustc_span::{ExpnId, MacroKind, Span};
1615

16+
use crate::utils::attr_collector::AttrStorage;
17+
1718
declare_clippy_lint! {
1819
/// ### What it does
1920
/// Denies the configured macros in clippy.toml
@@ -64,14 +65,19 @@ pub struct DisallowedMacros {
6465
// Track the most recently seen node that can have a `derive` attribute.
6566
// Needed to use the correct lint level.
6667
derive_src: Option<OwnerId>,
68+
69+
// When a macro is disallowed in an early pass, it's stored
70+
// and emitted during the late pass. This happens for attributes.
71+
earlies: AttrStorage,
6772
}
6873

6974
impl DisallowedMacros {
70-
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
75+
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf, earlies: AttrStorage) -> Self {
7176
Self {
7277
disallowed: create_disallowed_map(tcx, &conf.disallowed_macros),
7378
seen: FxHashSet::default(),
7479
derive_src: None,
80+
earlies,
7581
}
7682
}
7783

@@ -114,6 +120,15 @@ impl DisallowedMacros {
114120
impl_lint_pass!(DisallowedMacros => [DISALLOWED_MACROS]);
115121

116122
impl LateLintPass<'_> for DisallowedMacros {
123+
fn check_crate(&mut self, cx: &LateContext<'_>) {
124+
// once we check a crate in the late pass we can emit the early pass lints
125+
if let Some(attr_spans) = self.earlies.clone().0.get() {
126+
for span in attr_spans {
127+
self.check(cx, *span, None);
128+
}
129+
}
130+
}
131+
117132
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
118133
self.check(cx, expr.span, None);
119134
// `$t + $t` can have the context of $t, check also the span of the binary operator
@@ -164,8 +179,4 @@ impl LateLintPass<'_> for DisallowedMacros {
164179
fn check_path(&mut self, cx: &LateContext<'_>, path: &Path<'_>, _: HirId) {
165180
self.check(cx, path.span, None);
166181
}
167-
168-
fn check_attribute(&mut self, cx: &LateContext<'_>, attr: &Attribute) {
169-
self.check(cx, attr.span, self.derive_src);
170-
}
171182
}

clippy_lints/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ use clippy_config::{Conf, get_configuration_metadata, sanitize_explanation};
399399
use clippy_utils::macros::FormatArgsStorage;
400400
use rustc_data_structures::fx::FxHashSet;
401401
use rustc_lint::{Lint, LintId};
402+
use utils::attr_collector::{AttrCollector, AttrStorage};
402403

403404
/// Register all pre expansion lints
404405
///
@@ -461,6 +462,7 @@ pub(crate) enum LintCategory {
461462
#[cfg(feature = "internal")]
462463
Internal,
463464
}
465+
464466
#[allow(clippy::enum_glob_use)]
465467
use LintCategory::*;
466468

@@ -582,6 +584,10 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
582584
))
583585
});
584586

587+
let attr_storage = AttrStorage::default();
588+
let attrs = attr_storage.clone();
589+
store.register_early_pass(move || Box::new(AttrCollector::new(attrs.clone())));
590+
585591
// all the internal lints
586592
#[cfg(feature = "internal")]
587593
{
@@ -791,7 +797,8 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
791797
store.register_late_pass(|_| Box::new(unwrap_in_result::UnwrapInResult));
792798
store.register_late_pass(|_| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned));
793799
store.register_late_pass(|_| Box::new(async_yields_async::AsyncYieldsAsync));
794-
store.register_late_pass(move |tcx| Box::new(disallowed_macros::DisallowedMacros::new(tcx, conf)));
800+
let attrs = attr_storage.clone();
801+
store.register_late_pass(move |tcx| Box::new(disallowed_macros::DisallowedMacros::new(tcx, conf, attrs.clone())));
795802
store.register_late_pass(move |tcx| Box::new(disallowed_methods::DisallowedMethods::new(tcx, conf)));
796803
store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86AttSyntax));
797804
store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86IntelSyntax));
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::mem;
2+
use std::sync::OnceLock;
3+
4+
use rustc_ast::{Attribute, Crate};
5+
use rustc_data_structures::sync::Lrc;
6+
use rustc_lint::{EarlyContext, EarlyLintPass};
7+
use rustc_session::impl_lint_pass;
8+
use rustc_span::Span;
9+
10+
#[derive(Clone, Default)]
11+
pub struct AttrStorage(pub Lrc<OnceLock<Vec<Span>>>);
12+
13+
pub struct AttrCollector {
14+
storage: AttrStorage,
15+
attrs: Vec<Span>,
16+
}
17+
18+
impl AttrCollector {
19+
pub fn new(storage: AttrStorage) -> Self {
20+
Self {
21+
storage,
22+
attrs: Vec::new(),
23+
}
24+
}
25+
}
26+
27+
impl_lint_pass!(AttrCollector => []);
28+
29+
impl EarlyLintPass for AttrCollector {
30+
fn check_attribute(&mut self, _cx: &EarlyContext<'_>, attr: &Attribute) {
31+
self.attrs.push(attr.span);
32+
}
33+
34+
fn check_crate_post(&mut self, _: &EarlyContext<'_>, _: &Crate) {
35+
self.storage
36+
.0
37+
.set(mem::take(&mut self.attrs))
38+
.expect("should only be called once");
39+
}
40+
}

clippy_lints/src/utils/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
pub mod attr_collector;
12
pub mod author;
23
pub mod dump_hir;
34
pub mod format_args_collector;
5+
46
#[cfg(feature = "internal")]
57
pub mod internal_lints;

tests/ui-toml/disallowed_macros/disallowed_macros.stderr

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1+
error: use of a disallowed macro `std::vec`
2+
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:16:5
3+
|
4+
LL | vec![1, 2, 3];
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 `serde::Serialize`
11+
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:18:14
12+
|
13+
LL | #[derive(Serialize)]
14+
| ^^^^^^^^^
15+
|
16+
= note: no serializing
17+
18+
error: use of a disallowed macro `macros::attr`
19+
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:31:1
20+
|
21+
LL | / macros::attr! {
22+
LL | | struct S;
23+
LL | | }
24+
| |_^
25+
26+
error: use of a disallowed macro `proc_macros::Derive`
27+
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:47:10
28+
|
29+
LL | #[derive(Derive)]
30+
| ^^^^^^
31+
132
error: use of a disallowed macro `std::println`
233
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:13:5
334
|
435
LL | println!("one");
536
| ^^^^^^^^^^^^^^^
6-
|
7-
= note: `-D clippy::disallowed-macros` implied by `-D warnings`
8-
= help: to override `-D warnings` add `#[allow(clippy::disallowed_macros)]`
937

1038
error: use of a disallowed macro `std::println`
1139
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:14:5
@@ -19,20 +47,6 @@ error: use of a disallowed macro `std::cfg`
1947
LL | cfg!(unix);
2048
| ^^^^^^^^^^
2149

22-
error: use of a disallowed macro `std::vec`
23-
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:16:5
24-
|
25-
LL | vec![1, 2, 3];
26-
| ^^^^^^^^^^^^^
27-
28-
error: use of a disallowed macro `serde::Serialize`
29-
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:18:14
30-
|
31-
LL | #[derive(Serialize)]
32-
| ^^^^^^^^^
33-
|
34-
= note: no serializing
35-
3650
error: use of a disallowed macro `macros::expr`
3751
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:21:13
3852
|
@@ -69,14 +83,6 @@ error: use of a disallowed macro `macros::binop`
6983
LL | let _ = macros::binop!(1);
7084
| ^^^^^^^^^^^^^^^^^
7185

72-
error: use of a disallowed macro `macros::attr`
73-
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:31:1
74-
|
75-
LL | / macros::attr! {
76-
LL | | struct S;
77-
LL | | }
78-
| |_^
79-
8086
error: use of a disallowed macro `macros::item`
8187
--> tests/ui-toml/disallowed_macros/disallowed_macros.rs:36:5
8288
|
@@ -95,11 +101,5 @@ error: use of a disallowed macro `macros::item`
95101
LL | macros::item!();
96102
| ^^^^^^^^^^^^^^^
97103

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

0 commit comments

Comments
 (0)