Skip to content

Commit f712eb5

Browse files
authored
Merge pull request #13657 from jdonszelmann/disallowed-macros-to-early
collect attribute spans early for disallowed macros
2 parents 3518178 + c931096 commit f712eb5

5 files changed

Lines changed: 98 additions & 38 deletions

File tree

‎clippy_lints/src/disallowed_macros.rs‎

Lines changed: 17 additions & 6 deletions
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+
usecrate::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
implDisallowedMacros{
70-
pubfnnew(tcx:TyCtxt<'_>,conf:&'staticConf) -> Self{
75+
pubfnnew(tcx:TyCtxt<'_>,conf:&'staticConf,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
implLateLintPass<'_>forDisallowedMacros{
123+
fncheck_crate(&mutself,cx:&LateContext<'_>){
124+
// once we check a crate in the late pass we can emit the early pass lints
125+
ifletSome(attr_spans) = self.earlies.clone().0.get(){
126+
for span in attr_spans {
127+
self.check(cx,*span,None);
128+
}
129+
}
130+
}
131+
117132
fncheck_expr(&mutself,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
fncheck_path(&mutself,cx:&LateContext<'_>,path:&Path<'_>, _:HirId){
165180
self.check(cx, path.span,None);
166181
}
167-
168-
fncheck_attribute(&mutself,cx:&LateContext<'_>,attr:&Attribute){
169-
self.check(cx, attr.span,self.derive_src);
170-
}
171182
}

‎clippy_lints/src/lib.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ use clippy_config::{Conf, get_configuration_metadata, sanitize_explanation};
401401
use clippy_utils::macros::FormatArgsStorage;
402402
use rustc_data_structures::fx::FxHashSet;
403403
use rustc_lint::{Lint,LintId};
404+
use utils::attr_collector::{AttrCollector,AttrStorage};
404405

405406
/// Register all pre expansion lints
406407
///
@@ -465,6 +466,7 @@ pub(crate) enum LintCategory {
465466
#[cfg(feature = "internal")]
466467
Internal,
467468
}
469+
468470
#[allow(clippy::enum_glob_use)]
469471
useLintCategory::*;
470472

@@ -586,6 +588,10 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
586588
))
587589
});
588590

591+
let attr_storage = AttrStorage::default();
592+
let attrs = attr_storage.clone();
593+
store.register_early_pass(move || Box::new(AttrCollector::new(attrs.clone())));
594+
589595
// all the internal lints
590596
#[cfg(feature = "internal")]
591597
{
@@ -797,7 +803,8 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
797803
store.register_late_pass(|_| Box::new(unwrap_in_result::UnwrapInResult));
798804
store.register_late_pass(|_| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned));
799805
store.register_late_pass(|_| Box::new(async_yields_async::AsyncYieldsAsync));
800-
store.register_late_pass(move |tcx| Box::new(disallowed_macros::DisallowedMacros::new(tcx, conf)));
806+
let attrs = attr_storage.clone();
807+
store.register_late_pass(move |tcx| Box::new(disallowed_macros::DisallowedMacros::new(tcx, conf, attrs.clone())));
801808
store.register_late_pass(move |tcx| Box::new(disallowed_methods::DisallowedMethods::new(tcx, conf)));
802809
store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86AttSyntax));
803810
store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86IntelSyntax));
Lines changed: 40 additions & 0 deletions
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+
pubstructAttrStorage(pubLrc<OnceLock<Vec<Span>>>);
12+
13+
pubstructAttrCollector{
14+
storage:AttrStorage,
15+
attrs:Vec<Span>,
16+
}
17+
18+
implAttrCollector{
19+
pubfnnew(storage:AttrStorage) -> Self{
20+
Self{
21+
storage,
22+
attrs:Vec::new(),
23+
}
24+
}
25+
}
26+
27+
impl_lint_pass!(AttrCollector => []);
28+
29+
implEarlyLintPassforAttrCollector{
30+
fncheck_attribute(&mutself,_cx:&EarlyContext<'_>,attr:&Attribute){
31+
self.attrs.push(attr.span);
32+
}
33+
34+
fncheck_crate_post(&mutself, _:&EarlyContext<'_>, _:&Crate){
35+
self.storage
36+
.0
37+
.set(mem::take(&mutself.attrs))
38+
.expect("should only be called once");
39+
}
40+
}

‎clippy_lints/src/utils/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
pubmod attr_collector;
12
pubmod author;
23
pubmod dump_hir;
34
pubmod format_args_collector;
5+
46
#[cfg(feature = "internal")]
57
pubmod internal_lints;

‎tests/ui-toml/disallowed_macros/disallowed_macros.stderr‎

Lines changed: 31 additions & 31 deletions
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)