@@ -13,17 +13,33 @@ pub(crate) fn attribute(args: &TokenStream, input: Stmt, mutability: Mutability)
13
13
. unwrap_or_else ( |e| e. to_compile_error ( ) )
14
14
}
15
15
16
- fn parse ( mut stmt : Stmt , mutability : Mutability ) -> Result < TokenStream > {
17
- match & mut stmt {
16
+ fn replace_stmt ( stmt : & mut Stmt , mutability : Mutability ) -> Result < bool > {
17
+ match stmt {
18
18
Stmt :: Expr ( Expr :: Match ( expr) ) | Stmt :: Semi ( Expr :: Match ( expr) , _) => {
19
- Context :: new ( mutability) . replace_expr_match ( expr)
19
+ Context :: new ( mutability) . replace_expr_match ( expr) ;
20
+ return Ok ( true )
21
+ }
22
+ Stmt :: Expr ( Expr :: If ( expr_if) ) => {
23
+ if let Expr :: Let ( ref mut expr) = & mut * expr_if. cond {
24
+ Context :: new ( mutability) . replace_expr_let ( expr) ;
25
+ return Ok ( true ) ;
26
+ }
20
27
}
21
28
Stmt :: Local ( local) => Context :: new ( mutability) . replace_local ( local) ?,
22
- Stmt :: Item ( Item :: Fn ( item) ) => replace_item_fn ( item, mutability) ?,
23
- Stmt :: Item ( Item :: Impl ( item) ) => replace_item_impl ( item, mutability) ,
24
- Stmt :: Item ( Item :: Use ( item) ) => replace_item_use ( item, mutability) ?,
25
29
_ => { }
26
30
}
31
+ Ok ( false )
32
+ }
33
+
34
+ fn parse ( mut stmt : Stmt , mutability : Mutability ) -> Result < TokenStream > {
35
+ if !replace_stmt ( & mut stmt, mutability) ? {
36
+ match & mut stmt {
37
+ Stmt :: Item ( Item :: Fn ( item) ) => replace_item_fn ( item, mutability) ?,
38
+ Stmt :: Item ( Item :: Impl ( item) ) => replace_item_impl ( item, mutability) ,
39
+ Stmt :: Item ( Item :: Use ( item) ) => replace_item_use ( item, mutability) ?,
40
+ _ => { }
41
+ }
42
+ }
27
43
28
44
Ok ( stmt. into_token_stream ( ) )
29
45
}
@@ -73,6 +89,10 @@ impl Context {
73
89
Ok ( ( ) )
74
90
}
75
91
92
+ fn replace_expr_let ( & mut self , expr : & mut ExprLet ) {
93
+ self . replace_pat ( & mut expr. pat , true )
94
+ }
95
+
76
96
fn replace_expr_match ( & mut self , expr : & mut ExprMatch ) {
77
97
expr. arms . iter_mut ( ) . for_each ( |Arm { pat, .. } | self . replace_pat ( pat, true ) )
78
98
}
@@ -195,17 +215,18 @@ impl FnVisitor {
195
215
expr. attrs . find_remove ( self . name ( ) ) ?
196
216
}
197
217
Stmt :: Local ( local) => local. attrs . find_remove ( self . name ( ) ) ?,
218
+ Stmt :: Expr ( Expr :: If ( expr_if) ) => {
219
+ if let Expr :: Let ( _) = & * expr_if. cond {
220
+ expr_if. attrs . find_remove ( self . name ( ) ) ?
221
+ } else {
222
+ None
223
+ }
224
+ }
198
225
_ => return Ok ( ( ) ) ,
199
226
} ;
200
227
if let Some ( attr) = attr {
201
228
parse_as_empty ( & attr. tokens ) ?;
202
- match node {
203
- Stmt :: Expr ( Expr :: Match ( expr) ) | Stmt :: Semi ( Expr :: Match ( expr) , _) => {
204
- Context :: new ( self . mutability ) . replace_expr_match ( expr)
205
- }
206
- Stmt :: Local ( local) => Context :: new ( self . mutability ) . replace_local ( local) ?,
207
- _ => unreachable ! ( ) ,
208
- }
229
+ replace_stmt ( node, self . mutability ) ?;
209
230
}
210
231
Ok ( ( ) )
211
232
}
0 commit comments