@@ -3,15 +3,17 @@ use rustc::hir::def_id::DefId;
3
3
use rustc:: lint;
4
4
use rustc:: ty;
5
5
use rustc:: ty:: adjustment;
6
+ use rustc_data_structures:: fx:: FxHashMap ;
6
7
use lint:: { LateContext , EarlyContext , LintContext , LintArray } ;
7
8
use lint:: { LintPass , EarlyLintPass , LateLintPass } ;
8
9
9
10
use syntax:: ast;
10
11
use syntax:: attr;
11
12
use syntax:: errors:: Applicability ;
12
- use syntax:: feature_gate:: { BUILTIN_ATTRIBUTES , AttributeType } ;
13
+ use syntax:: feature_gate:: { AttributeType , BuiltinAttribute , BUILTIN_ATTRIBUTE_MAP } ;
13
14
use syntax:: print:: pprust;
14
15
use syntax:: symbol:: keywords;
16
+ use syntax:: symbol:: Symbol ;
15
17
use syntax:: util:: parser;
16
18
use syntax_pos:: Span ;
17
19
@@ -210,17 +212,32 @@ declare_lint! {
210
212
"detects attributes that were not used by the compiler"
211
213
}
212
214
213
- declare_lint_pass ! ( UnusedAttributes => [ UNUSED_ATTRIBUTES ] ) ;
215
+ #[ derive( Copy , Clone ) ]
216
+ pub struct UnusedAttributes {
217
+ builtin_attributes : & ' static FxHashMap < Symbol , & ' static BuiltinAttribute > ,
218
+ }
219
+
220
+ impl UnusedAttributes {
221
+ pub fn new ( ) -> Self {
222
+ UnusedAttributes {
223
+ builtin_attributes : & * BUILTIN_ATTRIBUTE_MAP ,
224
+ }
225
+ }
226
+ }
227
+
228
+ impl_lint_pass ! ( UnusedAttributes => [ UNUSED_ATTRIBUTES ] ) ;
214
229
215
230
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UnusedAttributes {
216
231
fn check_attribute ( & mut self , cx : & LateContext < ' _ , ' _ > , attr : & ast:: Attribute ) {
217
232
debug ! ( "checking attribute: {:?}" , attr) ;
218
- // Note that check_name() marks the attribute as used if it matches.
219
- for & ( name, ty, ..) in BUILTIN_ATTRIBUTES {
233
+
234
+ let attr_info = attr. ident ( ) . and_then ( |ident| self . builtin_attributes . get ( & ident. name ) ) ;
235
+
236
+ if let Some ( & & ( name, ty, ..) ) = attr_info {
220
237
match ty {
221
- AttributeType :: Whitelisted if attr . check_name ( name ) => {
238
+ AttributeType :: Whitelisted => {
222
239
debug ! ( "{:?} is Whitelisted" , name) ;
223
- break ;
240
+ return ;
224
241
}
225
242
_ => ( ) ,
226
243
}
@@ -239,11 +256,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
239
256
debug ! ( "Emitting warning for: {:?}" , attr) ;
240
257
cx. span_lint ( UNUSED_ATTRIBUTES , attr. span , "unused attribute" ) ;
241
258
// Is it a builtin attribute that must be used at the crate level?
242
- let known_crate = BUILTIN_ATTRIBUTES . iter ( )
243
- . find ( |& & ( builtin, ty, ..) | {
244
- name == builtin && ty == AttributeType :: CrateLevel
245
- } )
246
- . is_some ( ) ;
259
+ let known_crate = attr_info. map ( |& & ( _, ty, ..) | {
260
+ ty == AttributeType :: CrateLevel
261
+ } ) . unwrap_or ( false ) ;
247
262
248
263
// Has a plugin registered this attribute as one that must be used at
249
264
// the crate level?
0 commit comments