From f9edb994e9c270a37f0a730be7b3cd84b979722f Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Thu, 21 Sep 2023 07:40:04 +0100 Subject: [PATCH 1/3] address lints --- benches/bench1.rs | 18 +++++------------- benches/fold_specialization.rs | 4 ++-- examples/iris.rs | 4 ++-- src/adaptors/mod.rs | 2 +- src/adaptors/multi_product.rs | 6 +++--- src/either_or_both.rs | 10 ++-------- src/lib.rs | 4 ++-- src/unique_impl.rs | 4 ++-- tests/quick.rs | 7 +++---- tests/test_core.rs | 2 +- tests/test_std.rs | 12 ++++++------ 11 files changed, 29 insertions(+), 44 deletions(-) diff --git a/benches/bench1.rs b/benches/bench1.rs index 084567177..73840b08a 100644 --- a/benches/bench1.rs +++ b/benches/bench1.rs @@ -474,10 +474,8 @@ fn equal(c: &mut Criterion) { fn merge_default(c: &mut Criterion) { let mut data1 = vec![0; 1024]; let mut data2 = vec![0; 800]; - let mut x = 0; - for (_, elt) in data1.iter_mut().enumerate() { + for (x, elt) in data1.iter_mut().enumerate() { *elt = x; - x += 1; } let mut y = 0; @@ -500,10 +498,8 @@ fn merge_default(c: &mut Criterion) { fn merge_by_cmp(c: &mut Criterion) { let mut data1 = vec![0; 1024]; let mut data2 = vec![0; 800]; - let mut x = 0; - for (_, elt) in data1.iter_mut().enumerate() { + for (x, elt) in data1.iter_mut().enumerate() { *elt = x; - x += 1; } let mut y = 0; @@ -526,10 +522,8 @@ fn merge_by_cmp(c: &mut Criterion) { fn merge_by_lt(c: &mut Criterion) { let mut data1 = vec![0; 1024]; let mut data2 = vec![0; 800]; - let mut x = 0; - for (_, elt) in data1.iter_mut().enumerate() { + for (x, elt) in data1.iter_mut().enumerate() { *elt = x; - x += 1; } let mut y = 0; @@ -552,10 +546,8 @@ fn merge_by_lt(c: &mut Criterion) { fn kmerge_default(c: &mut Criterion) { let mut data1 = vec![0; 1024]; let mut data2 = vec![0; 800]; - let mut x = 0; - for (_, elt) in data1.iter_mut().enumerate() { + for (x, elt) in data1.iter_mut().enumerate() { *elt = x; - x += 1; } let mut y = 0; @@ -592,7 +584,7 @@ fn kmerge_tenway(c: &mut Criterion) { let mut chunks = Vec::new(); let mut rest = &mut data[..]; - while rest.len() > 0 { + while !rest.is_empty() { let chunk_len = 1 + rng(&mut state) % 512; let chunk_len = cmp::min(rest.len(), chunk_len as usize); let (fst, tail) = { rest }.split_at_mut(chunk_len); diff --git a/benches/fold_specialization.rs b/benches/fold_specialization.rs index ef7f47c43..3666cb258 100644 --- a/benches/fold_specialization.rs +++ b/benches/fold_specialization.rs @@ -44,7 +44,7 @@ mod specialization { let arr = [1; 1024]; c.bench_function("internal specialized", move |b| { - b.iter(|| arr.iter().intersperse(&0).fold(0, |acc, x| acc + x)) + b.iter(|| arr.iter().intersperse(&0).sum::()) }); } @@ -52,7 +52,7 @@ mod specialization { let arr = [1; 1024]; c.bench_function("internal unspecialized", move |b| { - b.iter(|| Unspecialized(arr.iter().intersperse(&0)).fold(0, |acc, x| acc + x)) + b.iter(|| Unspecialized(arr.iter().intersperse(&0)).sum::()) }); } } diff --git a/examples/iris.rs b/examples/iris.rs index af64322d6..798fdb08e 100644 --- a/examples/iris.rs +++ b/examples/iris.rs @@ -9,7 +9,7 @@ use std::iter::repeat; use std::num::ParseFloatError; use std::str::FromStr; -static DATA: &'static str = include_str!("iris.data"); +static DATA: &str = include_str!("iris.data"); #[derive(Clone, Debug)] struct Iris { @@ -38,7 +38,7 @@ impl FromStr for Iris { name: "".into(), data: [0.; 4], }; - let mut parts = s.split(",").map(str::trim); + let mut parts = s.split(',').map(str::trim); // using Iterator::by_ref() for (index, part) in parts.by_ref().take(4).enumerate() { diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 4bc50eaa0..4fb248ebe 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -936,7 +936,7 @@ where type Item = usize; fn next(&mut self) -> Option { - while let Some(v) = self.iter.next() { + for v in self.iter.by_ref() { let i = self.count; self.count = i + 1; if (self.f)(v) { diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index ef7fadba8..f02f8c740 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -220,9 +220,9 @@ where self.0.iter().fold( (0, Some(0)), |acc, - &MultiProductIter { - ref iter, - ref iter_orig, + MultiProductIter { + iter, + iter_orig, cur: _, }| { let cur_size = iter.size_hint(); diff --git a/src/either_or_both.rs b/src/either_or_both.rs index d39747eaa..ff95ef355 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -29,19 +29,13 @@ impl EitherOrBoth { /// If `Left`, return true. Otherwise, return false. /// Exclusive version of [`has_left`](EitherOrBoth::has_left). pub fn is_left(&self) -> bool { - match *self { - Left(_) => true, - _ => false, - } + matches!(*self, Left(_)) } /// If `Right`, return true. Otherwise, return false. /// Exclusive version of [`has_right`](EitherOrBoth::has_right). pub fn is_right(&self) -> bool { - match *self { - Right(_) => true, - _ => false, - } + matches!(*self, Right(_)) } /// If `Both`, return true. Otherwise, return false. diff --git a/src/lib.rs b/src/lib.rs index 458ffe67c..37ff6da02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1010,7 +1010,7 @@ pub trait Itertools: Iterator { J: IntoIterator, F: FnMut(&Self::Item, &Self::Item) -> bool, { - merge_join::merge_by_new(self, other.into_iter(), is_first) + merge_join::merge_by_new(self, other, is_first) } /// Create an iterator that merges items from both this and the specified @@ -4002,7 +4002,7 @@ where (None, None) => return, (a, b) => { let equal = match (&a, &b) { - (&Some(ref a), &Some(ref b)) => a == b, + (Some(a), Some(b)) => a == b, _ => false, }; assert!( diff --git a/src/unique_impl.rs b/src/unique_impl.rs index a0b46b785..76b804541 100644 --- a/src/unique_impl.rs +++ b/src/unique_impl.rs @@ -61,7 +61,7 @@ where type Item = I::Item; fn next(&mut self) -> Option { - while let Some(v) = self.iter.next() { + for v in self.iter.by_ref() { let key = (self.f)(&v); if self.used.insert(key, ()).is_none() { return Some(v); @@ -115,7 +115,7 @@ where type Item = I::Item; fn next(&mut self) -> Option { - while let Some(v) = self.iter.iter.next() { + for v in self.iter.iter.by_ref() { if let Entry::Vacant(entry) = self.iter.used.entry(v) { let elt = entry.key().clone(); entry.insert(()); diff --git a/tests/quick.rs b/tests/quick.rs index 3dc23d704..4d6a731c6 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -817,9 +817,8 @@ quickcheck! { quickcheck! { fn size_put_back(a: Vec, x: Option) -> bool { let mut it = put_back(a.into_iter()); - match x { - Some(t) => it.put_back(t), - None => {} + if let Some(t) { + it.put_back(t) } correct_size_hint(it) } @@ -1320,7 +1319,7 @@ quickcheck! { fn at_most_one_i32(a: Vec) -> TestResult { let ret = a.iter().cloned().at_most_one(); match a.len() { - 0 => TestResult::from_bool(ret.unwrap() == None), + 0 => TestResult::from_bool(ret.unwrap().is_none()), 1 => TestResult::from_bool(ret.unwrap() == Some(a[0])), _ => TestResult::from_bool(ret.unwrap_err().eq(a.iter().cloned())), } diff --git a/tests/test_core.rs b/tests/test_core.rs index 729d7c3b8..209d646f5 100644 --- a/tests/test_core.rs +++ b/tests/test_core.rs @@ -26,7 +26,7 @@ fn product2() { assert!(prod.next() == Some(('α', 1))); assert!(prod.next() == Some(('β', 0))); assert!(prod.next() == Some(('β', 1))); - assert!(prod.next() == None); + assert!(prod.next().is_none()); } #[test] diff --git a/tests/test_std.rs b/tests/test_std.rs index 900d7174e..7e67b6eb0 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -38,19 +38,19 @@ fn product3() { fn interleave_shortest() { let v0: Vec = vec![0, 2, 4]; let v1: Vec = vec![1, 3, 5, 7]; - let it = v0.into_iter().interleave_shortest(v1.into_iter()); + let it = v0.into_iter().interleave_shortest(v1); assert_eq!(it.size_hint(), (6, Some(6))); assert_eq!(it.collect_vec(), vec![0, 1, 2, 3, 4, 5]); let v0: Vec = vec![0, 2, 4, 6, 8]; let v1: Vec = vec![1, 3, 5]; - let it = v0.into_iter().interleave_shortest(v1.into_iter()); + let it = v0.into_iter().interleave_shortest(v1); assert_eq!(it.size_hint(), (7, Some(7))); assert_eq!(it.collect_vec(), vec![0, 1, 2, 3, 4, 5, 6]); let i0 = ::std::iter::repeat(0); let v1: Vec<_> = vec![1, 3, 5]; - let it = i0.interleave_shortest(v1.into_iter()); + let it = i0.interleave_shortest(v1); assert_eq!(it.size_hint(), (7, Some(7))); let v0: Vec<_> = vec![0, 2, 4]; @@ -144,7 +144,7 @@ fn intersperse() { let ys = [0, 1, 2, 3]; let mut it = ys[..0].iter().copied().intersperse(1); - assert!(it.next() == None); + assert!(it.next().is_none()); } #[test] @@ -415,9 +415,9 @@ fn merge_by_btree() { let mut bt2 = BTreeMap::new(); bt2.insert("foo", 2); bt2.insert("bar", 4); - let results = bt1.into_iter().merge_by(bt2.into_iter(), |a, b| a.0 <= b.0); + let results = bt1.into_iter().merge_by(bt2, |a, b| a.0 <= b.0); let expected = vec![("bar", 4), ("foo", 2), ("hello", 1), ("world", 3)]; - it::assert_equal(results, expected.into_iter()); + it::assert_equal(results, expected); } #[allow(deprecated)] From e635d451c9aec7eea5d47a34597f1973a59bfa6d Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Thu, 21 Sep 2023 07:40:44 +0100 Subject: [PATCH 2/3] fixup --- tests/quick.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/quick.rs b/tests/quick.rs index 4d6a731c6..83e37cb60 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -817,7 +817,7 @@ quickcheck! { quickcheck! { fn size_put_back(a: Vec, x: Option) -> bool { let mut it = put_back(a.into_iter()); - if let Some(t) { + if let Some(t) = x { it.put_back(t) } correct_size_hint(it) From 78e125cdfeed061c2aedd8bddc76d7d03d331662 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Thu, 21 Sep 2023 07:47:29 +0100 Subject: [PATCH 3/3] fixup --- src/either_or_both.rs | 6 +++--- src/tuple_impl.rs | 2 +- tests/quick.rs | 2 +- tests/test_std.rs | 30 +++++++++++++++--------------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index ff95ef355..0d6af43b7 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -494,9 +494,9 @@ impl EitherOrBoth { } } -impl Into>> for EitherOrBoth { - fn into(self) -> Option> { - match self { +impl From> for Option> { + fn from(value: EitherOrBoth) -> Self { + match value { EitherOrBoth::Left(l) => Some(Either::Left(l)), EitherOrBoth::Right(r) => Some(Either::Right(r)), _ => None, diff --git a/src/tuple_impl.rs b/src/tuple_impl.rs index ba1c69df6..10e33b205 100644 --- a/src/tuple_impl.rs +++ b/src/tuple_impl.rs @@ -61,7 +61,7 @@ where buffer .iter() .position(|x| x.is_none()) - .unwrap_or_else(|| buffer.len()) + .unwrap_or(buffer.len()) }; (len, Some(len)) } diff --git a/tests/quick.rs b/tests/quick.rs index 83e37cb60..6e4297820 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -917,7 +917,7 @@ quickcheck! { } } } - cmb.next() == None + cmb.next().is_none() } } diff --git a/tests/test_std.rs b/tests/test_std.rs index 7e67b6eb0..a400a37d0 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -91,7 +91,7 @@ fn duplicates() { let ys_rev = [1, 0]; it::assert_equal(ys_rev.iter(), xs.iter().duplicates().rev()); - let xs = vec![0, 1, 2, 1, 2]; + let xs = [0, 1, 2, 1, 2]; let ys = vec![1, 2]; assert_eq!(ys, xs.iter().duplicates().cloned().collect_vec()); assert_eq!( @@ -165,7 +165,7 @@ fn dedup() { #[test] fn coalesce() { - let data = vec![-1., -2., -3., 3., 1., 0., -1.]; + let data = [-1., -2., -3., 3., 1., 0., -1.]; let it = data.iter().cloned().coalesce(|x, y| { if (x >= 0.) == (y >= 0.) { Ok(x + y) @@ -393,8 +393,8 @@ fn trait_pointers() { #[test] fn merge_by() { let odd: Vec<(u32, &str)> = vec![(1, "hello"), (3, "world"), (5, "!")]; - let even = vec![(2, "foo"), (4, "bar"), (6, "baz")]; - let expected = vec![ + let even = [(2, "foo"), (4, "bar"), (6, "baz")]; + let expected = [ (1, "hello"), (2, "foo"), (3, "world"), @@ -649,7 +649,7 @@ fn test_multipeek_reset() { #[test] fn test_multipeek_peeking_next() { use crate::it::PeekingNext; - let nums = vec![1u8, 2, 3, 4, 5, 6, 7]; + let nums = [1u8, 2, 3, 4, 5, 6, 7]; let mut mp = multipeek(nums.iter().copied()); assert_eq!(mp.peeking_next(|&x| x != 0), Some(1)); @@ -709,7 +709,7 @@ fn test_peek_nth() { #[test] fn test_peek_nth_peeking_next() { use it::PeekingNext; - let nums = vec![1u8, 2, 3, 4, 5, 6, 7]; + let nums = [1u8, 2, 3, 4, 5, 6, 7]; let mut iter = peek_nth(nums.iter().copied()); assert_eq!(iter.peeking_next(|&x| x != 0), Some(1)); @@ -832,12 +832,12 @@ fn group_by() { #[test] fn group_by_lazy_2() { - let data = vec![0, 1]; + let data = [0, 1]; let groups = data.iter().group_by(|k| *k); let gs = groups.into_iter().collect_vec(); it::assert_equal(data.iter(), gs.into_iter().flat_map(|(_k, g)| g)); - let data = vec![0, 1, 1, 0, 0]; + let data = [0, 1, 1, 0, 0]; let groups = data.iter().group_by(|k| *k); let mut gs = groups.into_iter().collect_vec(); gs[1..].reverse(); @@ -852,7 +852,7 @@ fn group_by_lazy_2() { } it::assert_equal(&mut groups[0], &[1, 1]); - let data = vec![0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; + let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; let grouper = data.iter().group_by(|k| *k); let mut groups = Vec::new(); for (i, (_, group)) in grouper.into_iter().enumerate() { @@ -869,7 +869,7 @@ fn group_by_lazy_2() { it::assert_equal(&mut groups[2], &[3, 3]); // use groups as chunks - let data = vec![0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; + let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; let mut i = 0; let grouper = data.iter().group_by(move |_| { let k = i / 3; @@ -890,7 +890,7 @@ fn group_by_lazy_2() { #[test] fn group_by_lazy_3() { // test consuming each group on the lap after it was produced - let data = vec![0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2]; + let data = [0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2]; let grouper = data.iter().group_by(|elt| *elt); let mut last = None; for (key, group) in &grouper { @@ -905,7 +905,7 @@ fn group_by_lazy_3() { #[test] fn chunks() { - let data = vec![0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; + let data = [0, 0, 0, 1, 1, 0, 0, 2, 2, 3, 3]; let grouper = data.iter().chunks(3); for (i, chunk) in grouper.into_iter().enumerate() { match i { @@ -1183,7 +1183,7 @@ fn powerset() { #[test] fn diff_mismatch() { - let a = vec![1, 2, 3, 4]; + let a = [1, 2, 3, 4]; let b = vec![1.0, 5.0, 3.0, 4.0]; let b_map = b.into_iter().map(|f| f as i32); let diff = it::diff_with(a.iter(), b_map, |a, b| *a == b); @@ -1197,7 +1197,7 @@ fn diff_mismatch() { #[test] fn diff_longer() { - let a = vec![1, 2, 3, 4]; + let a = [1, 2, 3, 4]; let b = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; let b_map = b.into_iter().map(|f| f as i32); let diff = it::diff_with(a.iter(), b_map, |a, b| *a == b); @@ -1210,7 +1210,7 @@ fn diff_longer() { #[test] fn diff_shorter() { - let a = vec![1, 2, 3, 4]; + let a = [1, 2, 3, 4]; let b = vec![1.0, 2.0]; let b_map = b.into_iter().map(|f| f as i32); let diff = it::diff_with(a.iter(), b_map, |a, b| *a == b);