@@ -13,12 +13,12 @@ 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 replace_stmt ( stmt : & mut Stmt , mutability : Mutability ) -> Result < ( ) > {
17
- match stmt {
18
- Stmt :: Expr ( Expr :: Match ( expr) ) | Stmt :: Semi ( Expr :: Match ( expr ) , _ ) => {
16
+ fn replace_expr ( expr : & mut Expr , mutability : Mutability ) {
17
+ match expr {
18
+ Expr :: Match ( expr) => {
19
19
Context :: new ( mutability) . replace_expr_match ( expr) ;
20
20
}
21
- Stmt :: Expr ( Expr :: If ( expr_if) ) => {
21
+ Expr :: If ( expr_if) => {
22
22
let mut expr_if = expr_if;
23
23
while let Expr :: Let ( ref mut expr) = & mut * expr_if. cond {
24
24
Context :: new ( mutability) . replace_expr_let ( expr) ;
@@ -31,15 +31,18 @@ fn replace_stmt(stmt: &mut Stmt, mutability: Mutability) -> Result<()> {
31
31
break ;
32
32
}
33
33
}
34
- Stmt :: Local ( local) => Context :: new ( mutability) . replace_local ( local) ?,
35
34
_ => { }
36
35
}
37
- Ok ( ( ) )
36
+ }
37
+
38
+ fn replace_local ( local : & mut Local , mutability : Mutability ) -> Result < ( ) > {
39
+ Context :: new ( mutability) . replace_local ( local)
38
40
}
39
41
40
42
fn parse ( mut stmt : Stmt , mutability : Mutability ) -> Result < TokenStream > {
41
- replace_stmt ( & mut stmt, mutability) ?;
42
43
match & mut stmt {
44
+ Stmt :: Expr ( expr) | Stmt :: Semi ( expr, _) => replace_expr ( expr, mutability) ,
45
+ Stmt :: Local ( local) => replace_local ( local, mutability) ?,
43
46
Stmt :: Item ( Item :: Fn ( item) ) => replace_item_fn ( item, mutability) ?,
44
47
Stmt :: Item ( Item :: Impl ( item) ) => replace_item_impl ( item, mutability) ,
45
48
Stmt :: Item ( Item :: Use ( item) ) => replace_item_use ( item, mutability) ?,
@@ -219,12 +222,28 @@ impl FnVisitor {
219
222
}
220
223
221
224
fn visit_stmt ( & mut self , node : & mut Stmt ) -> Result < ( ) > {
222
- let attr = match node {
223
- Stmt :: Expr ( Expr :: Match ( expr) ) | Stmt :: Semi ( Expr :: Match ( expr) , _) => {
224
- expr. attrs . find_remove ( self . name ( ) ) ?
225
+ match node {
226
+ Stmt :: Expr ( expr) | Stmt :: Semi ( expr, _) => {
227
+ visit_mut:: visit_expr_mut ( self , expr) ;
228
+ self . visit_expr ( expr)
229
+ }
230
+ Stmt :: Local ( local) => {
231
+ visit_mut:: visit_local_mut ( self , local) ;
232
+ if let Some ( attr) = local. attrs . find_remove ( self . name ( ) ) ? {
233
+ parse_as_empty ( & attr. tokens ) ?;
234
+ replace_local ( local, self . mutability ) ?;
235
+ }
236
+ Ok ( ( ) )
225
237
}
226
- Stmt :: Local ( local) => local. attrs . find_remove ( self . name ( ) ) ?,
227
- Stmt :: Expr ( Expr :: If ( expr_if) ) => {
238
+ // Do not recurse into nested items.
239
+ Stmt :: Item ( _) => Ok ( ( ) ) ,
240
+ }
241
+ }
242
+
243
+ fn visit_expr ( & mut self , node : & mut Expr ) -> Result < ( ) > {
244
+ let attr = match node {
245
+ Expr :: Match ( expr) => expr. attrs . find_remove ( self . name ( ) ) ?,
246
+ Expr :: If ( expr_if) => {
228
247
if let Expr :: Let ( _) = & * expr_if. cond {
229
248
expr_if. attrs . find_remove ( self . name ( ) ) ?
230
249
} else {
@@ -235,7 +254,7 @@ impl FnVisitor {
235
254
} ;
236
255
if let Some ( attr) = attr {
237
256
parse_as_empty ( & attr. tokens ) ?;
238
- replace_stmt ( node, self . mutability ) ? ;
257
+ replace_expr ( node, self . mutability ) ;
239
258
}
240
259
Ok ( ( ) )
241
260
}
@@ -246,14 +265,20 @@ impl VisitMut for FnVisitor {
246
265
if self . res . is_err ( ) {
247
266
return ;
248
267
}
249
-
250
- visit_mut:: visit_stmt_mut ( self , node) ;
251
-
252
268
if let Err ( e) = self . visit_stmt ( node) {
253
269
self . res = Err ( e)
254
270
}
255
271
}
256
272
273
+ fn visit_expr_mut ( & mut self , node : & mut Expr ) {
274
+ if self . res . is_err ( ) {
275
+ return ;
276
+ }
277
+ if let Err ( e) = self . visit_expr ( node) {
278
+ self . res = Err ( e)
279
+ }
280
+ }
281
+
257
282
fn visit_item_mut ( & mut self , _: & mut Item ) {
258
283
// Do not recurse into nested items.
259
284
}
0 commit comments