From 18e352d24b56f04eb0b1425d879df1aed94ffdc6 Mon Sep 17 00:00:00 2001 From: kinto-b Date: Mon, 15 Apr 2024 16:50:20 +1000 Subject: [PATCH] Ensure panic on length mismatch --- src/zip_eq_impl.rs | 11 ++++++++--- tests/zip.rs | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/zip_eq_impl.rs b/src/zip_eq_impl.rs index 8139c2cce..2736d3854 100644 --- a/src/zip_eq_impl.rs +++ b/src/zip_eq_impl.rs @@ -52,7 +52,6 @@ where } } - #[inline] fn fold(self, init: B, mut f: F) -> B where Self: Sized, @@ -60,10 +59,16 @@ where { let Self { a, mut b } = self; - a.fold(init, |acc, x| match b.next() { + let acc = a.fold(init, |acc, x| match b.next() { Some(y) => f(acc, (x, y)), None => panic!("itertools: .zip_eq() reached end of one iterator before the other"), - }) + }); + + if b.next().is_some() { + panic!("itertools: .zip_eq() reached end of one iterator before the other") + } + + acc } fn size_hint(&self) -> (usize, Option) { diff --git a/tests/zip.rs b/tests/zip.rs index 9efc03fc4..be886da7a 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -82,3 +82,12 @@ fn zip_eq_panic3() { zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y); } + +#[should_panic] +#[test] +fn zip_eq_panic4() { + let b = [1, 2]; + let a = [1, 2, 3]; + + zip_eq(&a, &b).fold(0, |acc, (x, y)| acc + x * y); +}