88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- use attr:: AttrMetaMethods ;
11+ use attr:: { AttrMetaMethods , HasAttrs } ;
1212use errors:: Handler ;
1313use feature_gate:: GatedCfgAttr ;
1414use fold:: Folder ;
@@ -20,7 +20,7 @@ use ptr::P;
2020use util:: small_vector:: SmallVector ;
2121
2222pub trait CfgFolder : fold:: Folder {
23- fn in_cfg ( & mut self , attrs : & [ ast :: Attribute ] ) -> bool ;
23+ fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > ;
2424 fn visit_unconfigurable_expr ( & mut self , _expr : & ast:: Expr ) { }
2525}
2626
@@ -32,8 +32,12 @@ struct Context<'a, F> where F: FnMut(&[ast::Attribute]) -> bool {
3232}
3333
3434impl < ' a , F : FnMut ( & [ ast:: Attribute ] ) -> bool > CfgFolder for Context < ' a , F > {
35- fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
36- ( self . in_cfg ) ( attrs)
35+ fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
36+ if ( self . in_cfg ) ( node. attrs ( ) ) {
37+ Some ( node)
38+ } else {
39+ None
40+ }
3741 }
3842
3943 fn visit_unconfigurable_expr ( & mut self , expr : & ast:: Expr ) {
@@ -70,58 +74,48 @@ impl<T: CfgFolder> fold::Folder for T {
7074 fn fold_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
7175 ast:: ForeignMod {
7276 abi : foreign_mod. abi ,
73- items : foreign_mod. items . into_iter ( ) . filter ( |item| {
74- self . in_cfg ( & item. attrs )
75- } ) . collect ( ) ,
77+ items : foreign_mod. items . into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ,
7678 }
7779 }
7880
7981 fn fold_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
8082 let fold_struct = |this : & mut Self , vdata| match vdata {
8183 ast:: VariantData :: Struct ( fields, id) => {
82- ast:: VariantData :: Struct ( fields. into_iter ( ) . filter ( |m| {
83- this. in_cfg ( & m. attrs )
84- } ) . collect ( ) , id)
84+ let fields = fields. into_iter ( ) . filter_map ( |field| this. configure ( field) ) ;
85+ ast:: VariantData :: Struct ( fields. collect ( ) , id)
8586 }
8687 ast:: VariantData :: Tuple ( fields, id) => {
87- ast:: VariantData :: Tuple ( fields. into_iter ( ) . filter ( |m| {
88- this. in_cfg ( & m. attrs )
89- } ) . collect ( ) , id)
88+ let fields = fields. into_iter ( ) . filter_map ( |field| this. configure ( field) ) ;
89+ ast:: VariantData :: Tuple ( fields. collect ( ) , id)
9090 }
9191 ast:: VariantData :: Unit ( id) => ast:: VariantData :: Unit ( id)
9292 } ;
9393
9494 let item = match item {
95- ast:: ItemKind :: Impl ( u, o, a, b, c, impl_items) => {
96- let impl_items = impl_items. into_iter ( )
97- . filter ( |ii| self . in_cfg ( & ii. attrs ) )
98- . collect ( ) ;
99- ast:: ItemKind :: Impl ( u, o, a, b, c, impl_items)
95+ ast:: ItemKind :: Impl ( u, o, a, b, c, items) => {
96+ let items = items. into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ;
97+ ast:: ItemKind :: Impl ( u, o, a, b, c, items)
10098 }
101- ast:: ItemKind :: Trait ( u, a, b, methods) => {
102- let methods = methods. into_iter ( )
103- . filter ( |ti| self . in_cfg ( & ti. attrs ) )
104- . collect ( ) ;
105- ast:: ItemKind :: Trait ( u, a, b, methods)
99+ ast:: ItemKind :: Trait ( u, a, b, items) => {
100+ let items = items. into_iter ( ) . filter_map ( |item| self . configure ( item) ) . collect ( ) ;
101+ ast:: ItemKind :: Trait ( u, a, b, items)
106102 }
107103 ast:: ItemKind :: Struct ( def, generics) => {
108104 ast:: ItemKind :: Struct ( fold_struct ( self , def) , generics)
109105 }
110106 ast:: ItemKind :: Enum ( def, generics) => {
111107 let variants = def. variants . into_iter ( ) . filter_map ( |v| {
112- if !self . in_cfg ( & v. node . attrs ) {
113- None
114- } else {
115- Some ( Spanned {
108+ self . configure ( v) . map ( |v| {
109+ Spanned {
116110 node : ast:: Variant_ {
117111 name : v. node . name ,
118112 attrs : v. node . attrs ,
119113 data : fold_struct ( self , v. node . data ) ,
120114 disr_expr : v. node . disr_expr ,
121115 } ,
122116 span : v. span
123- } )
124- }
117+ }
118+ } )
125119 } ) ;
126120 ast:: ItemKind :: Enum ( ast:: EnumDef {
127121 variants : variants. collect ( ) ,
@@ -146,31 +140,21 @@ impl<T: CfgFolder> fold::Folder for T {
146140 }
147141
148142 fn fold_opt_expr ( & mut self , expr : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
149- if self . in_cfg ( expr. attrs ( ) ) {
150- Some ( fold_expr ( self , expr) )
151- } else {
152- None
153- }
143+ self . configure ( expr) . map ( |expr| fold_expr ( self , expr) )
154144 }
155145
156146 fn fold_stmt ( & mut self , stmt : ast:: Stmt ) -> SmallVector < ast:: Stmt > {
157- if self . in_cfg ( stmt. node . attrs ( ) ) {
158- fold:: noop_fold_stmt ( stmt, self )
159- } else {
160- SmallVector :: zero ( )
161- }
147+ self . configure ( stmt) . map ( |stmt| fold:: noop_fold_stmt ( stmt, self ) )
148+ . unwrap_or ( SmallVector :: zero ( ) )
162149 }
163150
164151 fn fold_mac ( & mut self , mac : ast:: Mac ) -> ast:: Mac {
165152 fold:: noop_fold_mac ( mac, self )
166153 }
167154
168155 fn fold_item ( & mut self , item : P < ast:: Item > ) -> SmallVector < P < ast:: Item > > {
169- if self . in_cfg ( & item. attrs ) {
170- SmallVector :: one ( item. map ( |i| self . fold_item_simple ( i) ) )
171- } else {
172- SmallVector :: zero ( )
173- }
156+ self . configure ( item) . map ( |item| SmallVector :: one ( item. map ( |i| self . fold_item_simple ( i) ) ) )
157+ . unwrap_or ( SmallVector :: zero ( ) )
174158 }
175159}
176160
@@ -192,7 +176,7 @@ fn fold_expr<F: CfgFolder>(folder: &mut F, expr: P<ast::Expr>) -> P<ast::Expr> {
192176 node : match node {
193177 ast:: ExprKind :: Match ( m, arms) => {
194178 ast:: ExprKind :: Match ( m, arms. into_iter ( )
195- . filter ( |a| folder. in_cfg ( & a . attrs ) )
179+ . filter_map ( |a| folder. configure ( a ) )
196180 . collect ( ) )
197181 }
198182 _ => node
0 commit comments