diff options
author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-10-30 12:10:43 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-11-01 13:26:25 +0100 |
commit | b65082705fe3ae0888a3e7019c9558d4252837cf (patch) | |
tree | b5c15862a95d141efb7a555f9baecc188780e9c0 /askama_derive | |
parent | daa404713e8e1e5294a3cf65040172d33e43a0eb (diff) | |
download | askama-b65082705fe3ae0888a3e7019c9558d4252837cf.tar.gz askama-b65082705fe3ae0888a3e7019c9558d4252837cf.tar.bz2 askama-b65082705fe3ae0888a3e7019c9558d4252837cf.zip |
Remove unused `_did_loop` condition if `else` block is empty
Diffstat (limited to 'askama_derive')
-rw-r--r-- | askama_derive/src/generator.rs | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 9031e8b..eb293b0 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -601,9 +601,13 @@ impl<'a> Generator<'a> { let expr_code = self.visit_expr_root(&loop_block.iter)?; + let has_else_nodes = !loop_block.else_nodes.is_empty(); + let flushed = self.write_buf_writable(buf)?; buf.writeln("{")?; - buf.writeln("let mut _did_loop = false;")?; + if has_else_nodes { + buf.writeln("let mut _did_loop = false;")?; + } match loop_block.iter { Expr::Range(_, _, _) => buf.writeln(&format!("let _iter = {expr_code};")), Expr::Array(..) => buf.writeln(&format!("let _iter = {expr_code}.iter();")), @@ -639,20 +643,28 @@ impl<'a> Generator<'a> { self.visit_target(buf, true, true, &loop_block.var); buf.writeln(", _loop_item) in ::askama::helpers::TemplateLoop::new(_iter) {")?; - buf.writeln("_did_loop = true;")?; + if has_else_nodes { + buf.writeln("_did_loop = true;")?; + } let mut size_hint1 = self.handle(ctx, &loop_block.body, buf, AstLevel::Nested)?; self.handle_ws(loop_block.ws2); size_hint1 += self.write_buf_writable(buf)?; self.locals.pop(); buf.writeln("}")?; - buf.writeln("if !_did_loop {")?; - self.locals.push(); - let mut size_hint2 = self.handle(ctx, &loop_block.else_nodes, buf, AstLevel::Nested)?; - self.handle_ws(loop_block.ws3); - size_hint2 += self.write_buf_writable(buf)?; - self.locals.pop(); - buf.writeln("}")?; + let mut size_hint2; + if has_else_nodes { + buf.writeln("if !_did_loop {")?; + self.locals.push(); + size_hint2 = self.handle(ctx, &loop_block.else_nodes, buf, AstLevel::Nested)?; + self.handle_ws(loop_block.ws3); + size_hint2 += self.write_buf_writable(buf)?; + self.locals.pop(); + buf.writeln("}")?; + } else { + self.handle_ws(loop_block.ws3); + size_hint2 = self.write_buf_writable(buf)?; + } buf.writeln("}")?; |