From 6938a61156d9289f249772f348274cdbb9634981 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Wed, 10 Jan 2024 15:35:10 +0100 Subject: [PATCH 1/2] Use `Self` Two simple steps: - run `cargo clippy --fix -- -D clippy::use_self` - In `either_or_both`, remove `Self::` as glob use is quite convenient. https://rust-lang.github.io/rust-clippy/master/index.html#/use_self Co-Authored-By: Marcel Hellwig --- benches/extra/zipslices.rs | 2 +- examples/iris.rs | 4 ++-- src/adaptors/mod.rs | 4 ++-- src/adaptors/multi_product.rs | 6 +++--- src/duplicates_impl.rs | 6 +++--- src/either_or_both.rs | 16 ++++++++-------- src/groupbylazy.rs | 2 +- src/kmerge_impl.rs | 4 ++-- src/lazy_buffer.rs | 4 ++-- src/lib.rs | 6 +++--- src/minmax.rs | 6 +++--- src/tuple_impl.rs | 2 +- src/zip_longest.rs | 2 +- tests/quick.rs | 26 +++++++++++++------------- tests/test_core.rs | 2 +- tests/test_std.rs | 14 +++++++------- 16 files changed, 53 insertions(+), 53 deletions(-) diff --git a/benches/extra/zipslices.rs b/benches/extra/zipslices.rs index a7120e168..2ec7bc0c0 100644 --- a/benches/extra/zipslices.rs +++ b/benches/extra/zipslices.rs @@ -58,7 +58,7 @@ where #[inline(always)] pub fn from_slices(a: T, b: U) -> Self { let minl = cmp::min(a.len(), b.len()); - ZipSlices { + Self { t: a, u: b, len: minl, diff --git a/examples/iris.rs b/examples/iris.rs index 798fdb08e..5f5525154 100644 --- a/examples/iris.rs +++ b/examples/iris.rs @@ -25,7 +25,7 @@ enum ParseError { impl From for ParseError { fn from(err: ParseFloatError) -> Self { - ParseError::Numeric(err) + Self::Numeric(err) } } @@ -34,7 +34,7 @@ impl FromStr for Iris { type Err = ParseError; fn from_str(s: &str) -> Result { - let mut iris = Iris { + let mut iris = Self { name: "".into(), data: [0.; 4], }; diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index a5b182d78..08d91e397 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -218,7 +218,7 @@ where /// Split the `PutBack` into its parts. #[inline] pub fn into_parts(self) -> (Option, I) { - let PutBack { top, iter } = self; + let Self { top, iter } = self; (top, iter) } @@ -689,7 +689,7 @@ pub struct Tuple1Combination { impl From for Tuple1Combination { fn from(iter: I) -> Self { - Tuple1Combination { iter } + Self { iter } } } diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index f02f8c740..74cb06d0e 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -95,7 +95,7 @@ where if last.in_progress() { true - } else if MultiProduct::iterate_last(rest, state) { + } else if Self::iterate_last(rest, state) { last.reset(); last.iterate(); // If iterator is None twice consecutively, then iterator is @@ -139,7 +139,7 @@ where I::Item: Clone, { fn new(iter: I) -> Self { - MultiProductIter { + Self { cur: None, iter: iter.clone(), iter_orig: iter, @@ -171,7 +171,7 @@ where type Item = Vec; fn next(&mut self) -> Option { - if MultiProduct::iterate_last(&mut self.0, MultiProductIterState::StartOfIter) { + if Self::iterate_last(&mut self.0, MultiProductIterState::StartOfIter) { Some(self.curr_iterator()) } else { None diff --git a/src/duplicates_impl.rs b/src/duplicates_impl.rs index 71ed21841..a0db15432 100644 --- a/src/duplicates_impl.rs +++ b/src/duplicates_impl.rs @@ -22,7 +22,7 @@ mod private { impl DuplicatesBy { pub(crate) fn new(iter: I, key_method: F) -> Self { - DuplicatesBy { + Self { iter, meta: Meta { used: HashMap::new(), @@ -77,7 +77,7 @@ mod private { type Item = I::Item; fn next(&mut self) -> Option { - let DuplicatesBy { iter, meta } = self; + let Self { iter, meta } = self; iter.find_map(|v| meta.filter(v)) } @@ -109,7 +109,7 @@ mod private { F: KeyMethod, { fn next_back(&mut self) -> Option { - let DuplicatesBy { iter, meta } = self; + let Self { iter, meta } = self; iter.rev().find_map(|v| meta.filter(v)) } } diff --git a/src/either_or_both.rs b/src/either_or_both.rs index 2232cd68c..d13763441 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -301,9 +301,9 @@ impl EitherOrBoth { B: Default, { match self { - EitherOrBoth::Left(l) => (l, B::default()), - EitherOrBoth::Right(r) => (A::default(), r), - EitherOrBoth::Both(l, r) => (l, r), + Left(l) => (l, B::default()), + Right(r) => (A::default(), r), + Both(l, r) => (l, r), } } @@ -498,9 +498,9 @@ impl EitherOrBoth { impl Into>> for EitherOrBoth { fn into(self) -> Option> { match self { - EitherOrBoth::Left(l) => Some(Either::Left(l)), - EitherOrBoth::Right(r) => Some(Either::Right(r)), - _ => None, + Left(l) => Some(Either::Left(l)), + Right(r) => Some(Either::Right(r)), + Both(..) => None, } } } @@ -508,8 +508,8 @@ impl Into>> for EitherOrBoth { impl From> for EitherOrBoth { fn from(either: Either) -> Self { match either { - Either::Left(l) => EitherOrBoth::Left(l), - Either::Right(l) => EitherOrBoth::Right(l), + Either::Left(l) => Left(l), + Either::Right(l) => Right(l), } } } diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs index 6cf33838c..3d1bfab5e 100644 --- a/src/groupbylazy.rs +++ b/src/groupbylazy.rs @@ -29,7 +29,7 @@ struct ChunkIndex { impl ChunkIndex { #[inline(always)] fn new(size: usize) -> Self { - ChunkIndex { + Self { size, index: 0, key: 0, diff --git a/src/kmerge_impl.rs b/src/kmerge_impl.rs index c077cdda1..4660caf07 100644 --- a/src/kmerge_impl.rs +++ b/src/kmerge_impl.rs @@ -27,9 +27,9 @@ where I: Iterator, { /// Constructs a `HeadTail` from an `Iterator`. Returns `None` if the `Iterator` is empty. - fn new(mut it: I) -> Option> { + fn new(mut it: I) -> Option { let head = it.next(); - head.map(|h| HeadTail { head: h, tail: it }) + head.map(|h| Self { head: h, tail: it }) } /// Get the next element and update `head`, returning the old head in `Some`. diff --git a/src/lazy_buffer.rs b/src/lazy_buffer.rs index 38c7d405b..5cb039a63 100644 --- a/src/lazy_buffer.rs +++ b/src/lazy_buffer.rs @@ -14,8 +14,8 @@ impl LazyBuffer where I: Iterator, { - pub fn new(it: I) -> LazyBuffer { - LazyBuffer { + pub fn new(it: I) -> Self { + Self { it: it.fuse(), buffer: Vec::new(), } diff --git a/src/lib.rs b/src/lib.rs index b2641d19d..c670e07b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4077,15 +4077,15 @@ impl FoldWhile { /// Return the value in the continue or done. pub fn into_inner(self) -> T { match self { - FoldWhile::Continue(x) | FoldWhile::Done(x) => x, + Self::Continue(x) | Self::Done(x) => x, } } /// Return true if `self` is `Done`, false if it is `Continue`. pub fn is_done(&self) -> bool { match *self { - FoldWhile::Continue(_) => false, - FoldWhile::Done(_) => true, + Self::Continue(_) => false, + Self::Done(_) => true, } } } diff --git a/src/minmax.rs b/src/minmax.rs index 9f02a49f9..5c9674e01 100644 --- a/src/minmax.rs +++ b/src/minmax.rs @@ -37,9 +37,9 @@ impl MinMaxResult { /// ``` pub fn into_option(self) -> Option<(T, T)> { match self { - MinMaxResult::NoElements => None, - MinMaxResult::OneElement(x) => Some((x.clone(), x)), - MinMaxResult::MinMax(x, y) => Some((x, y)), + Self::NoElements => None, + Self::OneElement(x) => Some((x.clone(), x)), + Self::MinMax(x, y) => Some((x, y)), } } } diff --git a/src/tuple_impl.rs b/src/tuple_impl.rs index 4a7cd4da8..ac04a1952 100644 --- a/src/tuple_impl.rs +++ b/src/tuple_impl.rs @@ -34,7 +34,7 @@ where T: HomogeneousTuple, { fn new(buf: T::Buffer) -> Self { - TupleBuffer { cur: 0, buf } + Self { cur: 0, buf } } } diff --git a/src/zip_longest.rs b/src/zip_longest.rs index 27d9f3ab6..e53733542 100644 --- a/src/zip_longest.rs +++ b/src/zip_longest.rs @@ -59,7 +59,7 @@ where Self: Sized, F: FnMut(B, Self::Item) -> B, { - let ZipLongest { a, mut b } = self; + let Self { a, mut b } = self; init = a.fold(init, |init, a| match b.next() { Some(b) => f(init, EitherOrBoth::Both(a, b)), None => f(init, EitherOrBoth::Left(a)), diff --git a/tests/quick.rs b/tests/quick.rs index 0ca32f49a..dffcd22f6 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -38,7 +38,7 @@ impl HintKind for Exact { impl qc::Arbitrary for Exact { fn arbitrary(_: &mut G) -> Self { - Exact {} + Self {} } } @@ -69,7 +69,7 @@ impl qc::Arbitrary for Inexact { // Compensate for quickcheck using extreme values too rarely let ue_choices = &[0, ue_value, usize::max_value()]; let oe_choices = &[0, oe_value, usize::max_value()]; - Inexact { + Self { underestimate: *ue_choices.choose(g).unwrap(), overestimate: *oe_choices.choose(g).unwrap(), } @@ -79,7 +79,7 @@ impl qc::Arbitrary for Inexact { let underestimate_value = self.underestimate; let overestimate_value = self.overestimate; Box::new(underestimate_value.shrink().flat_map(move |ue_value| { - overestimate_value.shrink().map(move |oe_value| Inexact { + overestimate_value.shrink().map(move |oe_value| Self { underestimate: ue_value, overestimate: oe_value, }) @@ -108,7 +108,7 @@ where HK: HintKind, { fn new(it: Range, hint_kind: HK) -> Self { - Iter { + Self { iterator: it, fuse_flag: 0, hint_kind, @@ -166,16 +166,16 @@ where HK: HintKind, { fn arbitrary(g: &mut G) -> Self { - Iter::new(T::arbitrary(g)..T::arbitrary(g), HK::arbitrary(g)) + Self::new(T::arbitrary(g)..T::arbitrary(g), HK::arbitrary(g)) } - fn shrink(&self) -> Box>> { + fn shrink(&self) -> Box> { let r = self.iterator.clone(); let hint_kind = self.hint_kind; Box::new(r.start.shrink().flat_map(move |a| { r.end .shrink() - .map(move |b| Iter::new(a.clone()..b, hint_kind)) + .map(move |b| Self::new(a.clone()..b, hint_kind)) })) } } @@ -231,7 +231,7 @@ where let iter_count = g.gen_range(0, MAX_ITER_COUNT + 1); let hint_kind = qc::Arbitrary::arbitrary(g); - ShiftRange { + Self { range_start, range_end, start_step, @@ -1307,14 +1307,14 @@ quickcheck! { #[derive(Clone, Debug, PartialEq, Eq)] struct Val(u32, u32); -impl PartialOrd for Val { - fn partial_cmp(&self, other: &Val) -> Option { +impl PartialOrd for Val { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Val { - fn cmp(&self, other: &Val) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) } } @@ -1322,10 +1322,10 @@ impl Ord for Val { impl qc::Arbitrary for Val { fn arbitrary(g: &mut G) -> Self { let (x, y) = <(u32, u32)>::arbitrary(g); - Val(x, y) + Self(x, y) } fn shrink(&self) -> Box> { - Box::new((self.0, self.1).shrink().map(|(x, y)| Val(x, y))) + Box::new((self.0, self.1).shrink().map(|(x, y)| Self(x, y))) } } diff --git a/tests/test_core.rs b/tests/test_core.rs index fc2c858a3..45a8df438 100644 --- a/tests/test_core.rs +++ b/tests/test_core.rs @@ -237,7 +237,7 @@ fn count_clones() { fn clone(&self) -> Self { let n = self.n.get(); self.n.set(n + 1); - Foo { + Self { n: Cell::new(n + 1), } } diff --git a/tests/test_std.rs b/tests/test_std.rs index 24429e919..063eca049 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -540,7 +540,7 @@ where impl qc::Arbitrary for RandIter { fn arbitrary(g: &mut G) -> Self { - RandIter { + Self { idx: 0, len: g.size(), rng: R::seed_from_u64(g.next_u64()), @@ -1263,14 +1263,14 @@ fn extrema_set() { #[derive(Clone, Debug, PartialEq, Eq)] struct Val(u32, u32); - impl PartialOrd for Val { - fn partial_cmp(&self, other: &Val) -> Option { + impl PartialOrd for Val { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Val { - fn cmp(&self, other: &Val) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) } } @@ -1312,14 +1312,14 @@ fn minmax() { #[derive(Clone, Debug, PartialEq, Eq)] struct Val(u32, u32); - impl PartialOrd for Val { - fn partial_cmp(&self, other: &Val) -> Option { + impl PartialOrd for Val { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Val { - fn cmp(&self, other: &Val) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) } } From a638f83d0bd42d85a5b2e0ba0c62b38fb568d208 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Thu, 7 Jul 2022 10:23:37 +0200 Subject: [PATCH 2/2] fix clippy::type_repetition_in_bounds https://rust-lang.github.io/rust-clippy/master/index.html#/type_repetition_in_bounds `CircularTupleWindows` even had a duplicate bound. Co-Authored-By: Marcel Hellwig --- src/adaptors/coalesce.rs | 5 +++-- src/free.rs | 4 ++-- src/groupbylazy.rs | 24 ++++++++++++++---------- src/lib.rs | 5 ++--- src/peeking_take_while.rs | 8 ++++---- src/tuple_impl.rs | 2 +- src/ziptuple.rs | 3 +-- tests/test_std.rs | 4 ++-- 8 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/adaptors/coalesce.rs b/src/adaptors/coalesce.rs index 21eaa016d..ad9df1192 100644 --- a/src/adaptors/coalesce.rs +++ b/src/adaptors/coalesce.rs @@ -17,9 +17,10 @@ where f: F, } -impl Clone for CoalesceBy +impl Clone for CoalesceBy where - I: Iterator, + I: Clone + Iterator, + F: Clone, C: CountItem, C::CItem: Clone, { diff --git a/src/free.rs b/src/free.rs index 5ce1b49b3..617a1977d 100644 --- a/src/free.rs +++ b/src/free.rs @@ -166,10 +166,10 @@ where /// /// assert_eq!(cloned(b"abc").next(), Some(b'a')); /// ``` -pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned +pub fn cloned<'a, I, T>(iterable: I) -> iter::Cloned where I: IntoIterator, - T: Clone, + T: Clone + 'a, { iterable.into_iter().cloned() } diff --git a/src/groupbylazy.rs b/src/groupbylazy.rs index 3d1bfab5e..c318ec7b3 100644 --- a/src/groupbylazy.rs +++ b/src/groupbylazy.rs @@ -7,9 +7,9 @@ trait KeyFunction { fn call_mut(&mut self, arg: A) -> Self::Key; } -impl KeyFunction for F +impl KeyFunction for F where - F: FnMut(A) -> K, + F: FnMut(A) -> K + ?Sized, { type Key = K; #[inline] @@ -370,10 +370,12 @@ where /// /// See [`.group_by()`](crate::Itertools::group_by) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct Groups<'a, K: 'a, I: 'a, F: 'a> +pub struct Groups<'a, K, I, F> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, + K: 'a, + F: 'a, { parent: &'a GroupBy, } @@ -409,10 +411,12 @@ where /// An iterator for the elements in a single group. /// /// Iterator element type is `I::Item`. -pub struct Group<'a, K: 'a, I: 'a, F: 'a> +pub struct Group<'a, K, I, F> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, + K: 'a, + F: 'a, { parent: &'a GroupBy, index: usize, @@ -537,9 +541,9 @@ where /// See [`.chunks()`](crate::Itertools::chunks) for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Clone)] -pub struct Chunks<'a, I: 'a> +pub struct Chunks<'a, I> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, { parent: &'a IntoChunks, @@ -568,9 +572,9 @@ where /// An iterator for the elements in a single chunk. /// /// Iterator element type is `I::Item`. -pub struct Chunk<'a, I: 'a> +pub struct Chunk<'a, I> where - I: Iterator, + I: Iterator + 'a, I::Item: 'a, { parent: &'a IntoChunks, diff --git a/src/lib.rs b/src/lib.rs index c670e07b3..a571f49b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2124,8 +2124,7 @@ pub trait Itertools: Iterator { /// ``` fn dropping_back(mut self, n: usize) -> Self where - Self: Sized, - Self: DoubleEndedIterator, + Self: Sized + DoubleEndedIterator, { if n > 0 { (&mut self).rev().nth(n - 1); @@ -3962,7 +3961,7 @@ pub trait Itertools: Iterator { } } -impl Itertools for T where T: Iterator {} +impl Itertools for T where T: Iterator + ?Sized {} /// Return `true` if both iterables produce equal sequences /// (elements pairwise equal and sequences of the same length), diff --git a/src/peeking_take_while.rs b/src/peeking_take_while.rs index b08794a8d..b4852c1c9 100644 --- a/src/peeking_take_while.rs +++ b/src/peeking_take_while.rs @@ -96,17 +96,17 @@ where /// See [`.peeking_take_while()`](crate::Itertools::peeking_take_while) /// for more information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct PeekingTakeWhile<'a, I: 'a, F> +pub struct PeekingTakeWhile<'a, I, F> where - I: Iterator, + I: Iterator + 'a, { iter: &'a mut I, f: F, } -impl<'a, I: 'a, F> std::fmt::Debug for PeekingTakeWhile<'a, I, F> +impl<'a, I, F> std::fmt::Debug for PeekingTakeWhile<'a, I, F> where - I: Iterator + std::fmt::Debug, + I: Iterator + std::fmt::Debug + 'a, { debug_fmt_fields!(PeekingTakeWhile, iter); } diff --git a/src/tuple_impl.rs b/src/tuple_impl.rs index ac04a1952..c0d556fc9 100644 --- a/src/tuple_impl.rs +++ b/src/tuple_impl.rs @@ -244,7 +244,7 @@ where /// information. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Debug, Clone)] -pub struct CircularTupleWindows +pub struct CircularTupleWindows where I: Iterator + Clone, T: TupleCollect + Clone, diff --git a/src/ziptuple.rs b/src/ziptuple.rs index 82760ae8f..5ff0fad33 100644 --- a/src/ziptuple.rs +++ b/src/ziptuple.rs @@ -39,8 +39,7 @@ pub struct Zip { /// [`izip!()`]: crate::izip pub fn multizip(t: U) -> Zip where - Zip: From, - Zip: Iterator, + Zip: From + Iterator, { Zip::from(t) } diff --git a/tests/test_std.rs b/tests/test_std.rs index 063eca049..be163e98d 100644 --- a/tests/test_std.rs +++ b/tests/test_std.rs @@ -364,9 +364,9 @@ fn test_rciter() { fn trait_pointers() { struct ByRef<'r, I: ?Sized>(&'r mut I); - impl<'r, X, I: ?Sized> Iterator for ByRef<'r, I> + impl<'r, X, I> Iterator for ByRef<'r, I> where - I: 'r + Iterator, + I: ?Sized + 'r + Iterator, { type Item = X; fn next(&mut self) -> Option {