From b9e659350ba4262450ea01841f8739b9502ab1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 1 Jan 2023 19:47:04 +0100 Subject: [PATCH] Parser - Re-use for loop body if it's a block and an incr statement is needed --- src/parser.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index b3e1c6b..111b788 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -236,10 +236,14 @@ impl Parser { // inside a specific block if the initializer declares a variable. let body_stmt = self.parse_statement()?; let body_with_incr = if let Some(incr) = increment { - ast::StmtNode::Block(vec![ - Box::new(body_stmt), - Box::new(ast::StmtNode::Expression(incr)), - ]) + let incr_stmt = Box::new(ast::StmtNode::Expression(incr)); + let body_block = if let ast::StmtNode::Block(mut body_block) = body_stmt { + body_block.push(incr_stmt); + body_block + } else { + vec![Box::new(body_stmt), incr_stmt] + }; + ast::StmtNode::Block(body_block) } else { body_stmt };