Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove panic!() in loop.cycle([]) #617

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 with 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)