Skip to content

Commit

Permalink
refactor: resolve clippy lints
Browse files Browse the repository at this point in the history
Co-authored-by: warren2k <846021+warren2k@users.noreply.github.com>
  • Loading branch information
mightyiam and warren2k committed Sep 17, 2023
1 parent 100c90b commit 4b428f1
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 44 deletions.
10 changes: 9 additions & 1 deletion benches/bench1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ fn merge_default(c: &mut Criterion) {
let mut data1 = vec![0; 1024];
let mut data2 = vec![0; 800];
let mut x = 0;

#[allow(clippy::explicit_counter_loop)]
for (_, elt) in data1.iter_mut().enumerate() {
*elt = x;
x += 1;
Expand All @@ -501,6 +503,8 @@ fn merge_by_cmp(c: &mut Criterion) {
let mut data1 = vec![0; 1024];
let mut data2 = vec![0; 800];
let mut x = 0;

#[allow(clippy::explicit_counter_loop)]
for (_, elt) in data1.iter_mut().enumerate() {
*elt = x;
x += 1;
Expand All @@ -527,6 +531,8 @@ fn merge_by_lt(c: &mut Criterion) {
let mut data1 = vec![0; 1024];
let mut data2 = vec![0; 800];
let mut x = 0;

#[allow(clippy::explicit_counter_loop)]
for (_, elt) in data1.iter_mut().enumerate() {
*elt = x;
x += 1;
Expand All @@ -553,6 +559,8 @@ fn kmerge_default(c: &mut Criterion) {
let mut data1 = vec![0; 1024];
let mut data2 = vec![0; 800];
let mut x = 0;

#[allow(clippy::explicit_counter_loop)]
for (_, elt) in data1.iter_mut().enumerate() {
*elt = x;
x += 1;
Expand Down Expand Up @@ -592,7 +600,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);
Expand Down
2 changes: 2 additions & 0 deletions benches/extra/zipslices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ where

/// A helper trait to let `ZipSlices` accept both `&[T]` and `&mut [T]`.
///
/// # Safety
///
/// Unsafe trait because:
///
/// - Implementors must guarantee that `get_unchecked` is valid for all indices `0..len()`.
Expand Down
10 changes: 8 additions & 2 deletions benches/fold_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ 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(|| {
#[allow(clippy::unnecessary_fold)]
arr.iter().intersperse(&0).fold(0, |acc, x| acc + x)
})
});
}

pub fn internal_unspecialized(c: &mut Criterion) {
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(|| {
#[allow(clippy::unnecessary_fold)]
Unspecialized(arr.iter().intersperse(&0)).fold(0, |acc, x| acc + x)
})
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/iris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ where
type Item = usize;

fn next(&mut self) -> Option<Self::Item> {
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) {
Expand Down
6 changes: 3 additions & 3 deletions src/adaptors/multi_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ pub trait Itertools: Iterator {
J: IntoIterator<Item = Self::Item>,
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
Expand Down Expand Up @@ -2047,6 +2047,7 @@ pub trait Itertools: Iterator {
/// let data : Option<usize> = None;
/// assert_eq!(data.into_iter().all_equal_value(), Err(None));
/// ```
#[allow(clippy::type_complexity)]
fn all_equal_value(&mut self) -> Result<Self::Item, Option<(Self::Item, Self::Item)>>
where
Self: Sized,
Expand Down Expand Up @@ -4000,7 +4001,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!(
Expand Down
2 changes: 1 addition & 1 deletion src/tuple_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
buffer
.iter()
.position(|x| x.is_none())
.unwrap_or_else(|| buffer.len())
.unwrap_or(buffer.len())
};
(len, Some(len))
}
Expand Down
4 changes: 2 additions & 2 deletions src/unique_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
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);
Expand Down Expand Up @@ -115,7 +115,7 @@ where
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
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(());
Expand Down
11 changes: 5 additions & 6 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,8 @@ quickcheck! {
quickcheck! {
fn size_put_back(a: Vec<u8>, x: Option<u8>) -> bool {
let mut it = put_back(a.into_iter());
match x {
Some(t) => it.put_back(t),
None => {}
if let Some(t) = x {
it.put_back(t)
}
correct_size_hint(it)
}
Expand Down Expand Up @@ -920,7 +919,7 @@ quickcheck! {
}
}
}
cmb.next() == None
cmb.next().is_none()
}
}

Expand Down Expand Up @@ -1207,7 +1206,7 @@ struct Val(u32, u32);

impl PartialOrd<Val> for Val {
fn partial_cmp(&self, other: &Val) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
Some(self.cmp(other))
}
}

Expand Down Expand Up @@ -1310,7 +1309,7 @@ quickcheck! {
fn at_most_one_i32(a: Vec<i32>) -> 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())),
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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]
Expand Down

0 comments on commit 4b428f1

Please sign in to comment.