1
1
use crate :: util:: { check_builtin_macro_attribute, warn_on_duplicate_attribute} ;
2
2
3
+ use core:: ops:: ControlFlow ;
3
4
use rustc_ast as ast;
4
5
use rustc_ast:: mut_visit:: MutVisitor ;
5
6
use rustc_ast:: ptr:: P ;
@@ -87,41 +88,40 @@ fn flat_map_annotatable(
87
88
}
88
89
}
89
90
90
- struct CfgFinder {
91
- has_cfg_or_cfg_attr : bool ,
92
- }
93
-
94
- impl CfgFinder {
95
- fn has_cfg_or_cfg_attr ( annotatable : & Annotatable ) -> bool {
96
- let mut finder = CfgFinder { has_cfg_or_cfg_attr : false } ;
97
- match annotatable {
98
- Annotatable :: Item ( item) => finder. visit_item ( item) ,
99
- Annotatable :: TraitItem ( item) => finder. visit_assoc_item ( item, visit:: AssocCtxt :: Trait ) ,
100
- Annotatable :: ImplItem ( item) => finder. visit_assoc_item ( item, visit:: AssocCtxt :: Impl ) ,
101
- Annotatable :: ForeignItem ( item) => finder. visit_foreign_item ( item) ,
102
- Annotatable :: Stmt ( stmt) => finder. visit_stmt ( stmt) ,
103
- Annotatable :: Expr ( expr) => finder. visit_expr ( expr) ,
104
- Annotatable :: Arm ( arm) => finder. visit_arm ( arm) ,
105
- Annotatable :: ExprField ( field) => finder. visit_expr_field ( field) ,
106
- Annotatable :: PatField ( field) => finder. visit_pat_field ( field) ,
107
- Annotatable :: GenericParam ( param) => finder. visit_generic_param ( param) ,
108
- Annotatable :: Param ( param) => finder. visit_param ( param) ,
109
- Annotatable :: FieldDef ( field) => finder. visit_field_def ( field) ,
110
- Annotatable :: Variant ( variant) => finder. visit_variant ( variant) ,
111
- Annotatable :: Crate ( krate) => finder. visit_crate ( krate) ,
112
- } ;
113
- finder. has_cfg_or_cfg_attr
114
- }
115
- }
91
+ fn has_cfg_or_cfg_attr ( annotatable : & Annotatable ) -> bool {
92
+ struct CfgFinder ;
116
93
117
- impl < ' ast > visit:: Visitor < ' ast > for CfgFinder {
118
- fn visit_attribute ( & mut self , attr : & ' ast Attribute ) {
119
- // We want short-circuiting behavior, so don't use the '|=' operator.
120
- self . has_cfg_or_cfg_attr = self . has_cfg_or_cfg_attr
121
- || attr
94
+ impl < ' ast > visit:: Visitor < ' ast > for CfgFinder {
95
+ type Result = ControlFlow < ( ) > ;
96
+ fn visit_attribute ( & mut self , attr : & ' ast Attribute ) -> ControlFlow < ( ) > {
97
+ if attr
122
98
. ident ( )
123
- . is_some_and ( |ident| ident. name == sym:: cfg || ident. name == sym:: cfg_attr) ;
99
+ . is_some_and ( |ident| ident. name == sym:: cfg || ident. name == sym:: cfg_attr)
100
+ {
101
+ ControlFlow :: Break ( ( ) )
102
+ } else {
103
+ ControlFlow :: Continue ( ( ) )
104
+ }
105
+ }
124
106
}
107
+
108
+ let res = match annotatable {
109
+ Annotatable :: Item ( item) => CfgFinder . visit_item ( item) ,
110
+ Annotatable :: TraitItem ( item) => CfgFinder . visit_assoc_item ( item, visit:: AssocCtxt :: Trait ) ,
111
+ Annotatable :: ImplItem ( item) => CfgFinder . visit_assoc_item ( item, visit:: AssocCtxt :: Impl ) ,
112
+ Annotatable :: ForeignItem ( item) => CfgFinder . visit_foreign_item ( item) ,
113
+ Annotatable :: Stmt ( stmt) => CfgFinder . visit_stmt ( stmt) ,
114
+ Annotatable :: Expr ( expr) => CfgFinder . visit_expr ( expr) ,
115
+ Annotatable :: Arm ( arm) => CfgFinder . visit_arm ( arm) ,
116
+ Annotatable :: ExprField ( field) => CfgFinder . visit_expr_field ( field) ,
117
+ Annotatable :: PatField ( field) => CfgFinder . visit_pat_field ( field) ,
118
+ Annotatable :: GenericParam ( param) => CfgFinder . visit_generic_param ( param) ,
119
+ Annotatable :: Param ( param) => CfgFinder . visit_param ( param) ,
120
+ Annotatable :: FieldDef ( field) => CfgFinder . visit_field_def ( field) ,
121
+ Annotatable :: Variant ( variant) => CfgFinder . visit_variant ( variant) ,
122
+ Annotatable :: Crate ( krate) => CfgFinder . visit_crate ( krate) ,
123
+ } ;
124
+ res. is_break ( )
125
125
}
126
126
127
127
impl CfgEval < ' _ , ' _ > {
@@ -132,7 +132,7 @@ impl CfgEval<'_, '_> {
132
132
fn configure_annotatable ( & mut self , mut annotatable : Annotatable ) -> Option < Annotatable > {
133
133
// Tokenizing and re-parsing the `Annotatable` can have a significant
134
134
// performance impact, so try to avoid it if possible
135
- if !CfgFinder :: has_cfg_or_cfg_attr ( & annotatable) {
135
+ if !has_cfg_or_cfg_attr ( & annotatable) {
136
136
return Some ( annotatable) ;
137
137
}
138
138
0 commit comments