@@ -2,7 +2,6 @@ use clippy_config::Conf;
2
2
use clippy_utils:: create_disallowed_map;
3
3
use clippy_utils:: diagnostics:: { span_lint_and_then, span_lint_hir_and_then} ;
4
4
use clippy_utils:: macros:: macro_backtrace;
5
- use rustc_ast:: Attribute ;
6
5
use rustc_data_structures:: fx:: FxHashSet ;
7
6
use rustc_errors:: Diag ;
8
7
use rustc_hir:: def_id:: DefIdMap ;
@@ -14,6 +13,8 @@ use rustc_middle::ty::TyCtxt;
14
13
use rustc_session:: impl_lint_pass;
15
14
use rustc_span:: { ExpnId , MacroKind , Span } ;
16
15
16
+ use crate :: utils:: attr_collector:: AttrStorage ;
17
+
17
18
declare_clippy_lint ! {
18
19
/// ### What it does
19
20
/// Denies the configured macros in clippy.toml
@@ -64,14 +65,19 @@ pub struct DisallowedMacros {
64
65
// Track the most recently seen node that can have a `derive` attribute.
65
66
// Needed to use the correct lint level.
66
67
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 ,
67
72
}
68
73
69
74
impl DisallowedMacros {
70
- pub fn new ( tcx : TyCtxt < ' _ > , conf : & ' static Conf ) -> Self {
75
+ pub fn new ( tcx : TyCtxt < ' _ > , conf : & ' static Conf , earlies : AttrStorage ) -> Self {
71
76
Self {
72
77
disallowed : create_disallowed_map ( tcx, & conf. disallowed_macros ) ,
73
78
seen : FxHashSet :: default ( ) ,
74
79
derive_src : None ,
80
+ earlies,
75
81
}
76
82
}
77
83
@@ -114,6 +120,15 @@ impl DisallowedMacros {
114
120
impl_lint_pass ! ( DisallowedMacros => [ DISALLOWED_MACROS ] ) ;
115
121
116
122
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
+
117
132
fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
118
133
self . check ( cx, expr. span , None ) ;
119
134
// `$t + $t` can have the context of $t, check also the span of the binary operator
@@ -164,8 +179,4 @@ impl LateLintPass<'_> for DisallowedMacros {
164
179
fn check_path ( & mut self , cx : & LateContext < ' _ > , path : & Path < ' _ > , _: HirId ) {
165
180
self . check ( cx, path. span , None ) ;
166
181
}
167
-
168
- fn check_attribute ( & mut self , cx : & LateContext < ' _ > , attr : & Attribute ) {
169
- self . check ( cx, attr. span , self . derive_src ) ;
170
- }
171
182
}
0 commit comments