Skip to content

Commit

Permalink
Fix invalid if let code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed May 14, 2024
1 parent 03690ab commit 4d18725
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,24 @@ impl<'a> Generator<'a> {

if let Some(target) = target {
let mut expr_buf = Buffer::new(0);
self.visit_expr(&mut expr_buf, expr)?;
buf.write("let ");
self.visit_target(buf, true, true, target);
buf.write(" = &(");
// If this is a chain condition, then we need to declare the variable after the
// left expression has been handled but before the right expression is handled
// but this one should have access to the let-bound variable.
match expr {
Expr::BinOp(op, ref left, ref right) if *op == "||" || *op == "&&" => {
self.visit_expr(&mut expr_buf, left)?;
self.visit_target(buf, true, true, target);
expr_buf.write(&format!(" {op} "));
self.visit_expr(&mut expr_buf, right)?;
}
_ => {
self.visit_expr(&mut expr_buf, expr)?;
self.visit_target(buf, true, true, target);
}
}
buf.write(" = &");
buf.write(&expr_buf.buf);
buf.write(")");
} else {
// The following syntax `*(&(...) as &bool)` is used to
// trigger Rust's automatic dereferencing, to coerce
Expand Down

0 comments on commit 4d18725

Please sign in to comment.