Skip to content

Commit

Permalink
Remove panic!() in loop.cycle([])
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Jan 31, 2022
1 parent cd744f0 commit 8013389
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion askama_shared/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
"cycle" => match args {
[arg] => {
if matches!(arg, Expr::Array(arr) if arr.is_empty()) {
panic!("loop.cycle(…) cannot use an empty array.");
return Err("loop.cycle(…) cannot use an empty array".into());
}
buf.write("({");
buf.write("let _cycle = &(");
Expand Down
14 changes: 14 additions & 0 deletions testing/tests/ui/loop_cycle_empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Nb. this test fails because currently an empty array "[]" is always a syntax error in askama,
// but even if this changes, this test should keep failing, but possibly this another error message

use askama::Template;

#[derive(Template)]
#[template(
source = r#"{% for v in values %}{{ loop.cycle([]) }}{{ v }},{% endfor %}"#,
ext = "txt"
)]
struct ForCycleEmpty;

fn main() {
}
8 changes: 8 additions & 0 deletions testing/tests/ui/loop_cycle_empty.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: problems parsing template source at row 1, column 34 near:
"([]) }}{{ v }},{% endfor %}"
--> tests/ui/loop_cycle_empty.rs:6:10
|
6 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 8013389

Please sign in to comment.