From 7d2ca7760b95fdbfcb889aa7bb5d8213e99cd131 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 18 Sep 2024 17:37:11 +0200 Subject: [PATCH 1/2] Do not prepend rust macro calls with `&` --- rinja_derive/src/generator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rinja_derive/src/generator.rs b/rinja_derive/src/generator.rs index 201b8640..954753a9 100644 --- a/rinja_derive/src/generator.rs +++ b/rinja_derive/src/generator.rs @@ -2732,7 +2732,7 @@ fn is_copyable_within_op(expr: &Expr<'_>, within_op: bool) -> bool { // The result of a call likely doesn't need to be borrowed, // as in that case the call is more likely to return a // reference in the first place then. - Expr::Call(..) | Expr::Path(..) | Expr::Filter(..) => true, + Expr::Call(..) | Expr::Path(..) | Expr::Filter(..) | Expr::RustMacro(..) => true, // If the `expr` is within a `Unary` or `BinOp` then // an assumption can be made that the operand is copy. // If not, then the value is moved and adding `.clone()` From 40d10cea5ebfc818a4c98a2139b3a16ad80f1c18 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 18 Sep 2024 17:37:26 +0200 Subject: [PATCH 2/2] Add regression test for rust macro calls not prepended with `&` --- testing/tests/let.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 testing/tests/let.rs diff --git a/testing/tests/let.rs b/testing/tests/let.rs new file mode 100644 index 00000000..28272099 --- /dev/null +++ b/testing/tests/let.rs @@ -0,0 +1,23 @@ +use rinja::Template; + +#[derive(Template)] +#[template( + source = r#"{%- let x -%} +{%- if y -%} + {%- let x = String::new() %} +{%- else -%} + {%- let x = format!("blob") %} +{%- endif -%} +{{ x }}"#, + ext = "html" +)] +struct A { + y: bool, +} + +// This test ensures that rust macro calls in `let`/`set` statements are not prepended with `&`. +#[test] +fn let_macro() { + let template = A { y: false }; + assert_eq!(template.render().unwrap(), "blob") +}