Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 84fe381

Browse files
Kijewskidjc
authored andcommitted
Remove panic!() in loop.cycle([])
1 parent 0e9c9ae commit 84fe381

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

askama_shared/src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
13281328
"cycle" => match args {
13291329
[arg] => {
13301330
if matches!(arg, Expr::Array(arr) if arr.is_empty()) {
1331-
panic!("loop.cycle(…) cannot use an empty array.");
1331+
return Err("loop.cycle(…) cannot use an empty array".into());
13321332
}
13331333
buf.write("({");
13341334
buf.write("let _cycle = &(");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Nb. this test fails because currently an empty array "[]" is always a syntax error in askama,
2+
// but even if this changes, this test should keep failing, but possibly with another error message
3+
4+
use askama::Template;
5+
6+
#[derive(Template)]
7+
#[template(
8+
source = r#"{% for v in values %}{{ loop.cycle([]) }}{{ v }},{% endfor %}"#,
9+
ext = "txt"
10+
)]
11+
struct ForCycleEmpty;
12+
13+
fn main() {
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: problems parsing template source at row 1, column 34 near:
2+
"([]) }}{{ v }},{% endfor %}"
3+
--> tests/ui/loop_cycle_empty.rs:6:10
4+
|
5+
6 | #[derive(Template)]
6+
| ^^^^^^^^
7+
|
8+
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)