Skip to content

Commit

Permalink
Merge pull request #1363 from vks/clippy
Browse files Browse the repository at this point in the history
Fix clippy warnings
  • Loading branch information
vks committed Dec 14, 2023
2 parents ef89cbe + c427cff commit e0292f3
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion benches/seq_choose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn bench(c: &mut Criterion) {
}

fn bench_rng<Rng: RngCore + SeedableRng>(c: &mut Criterion, rng_name: &'static str) {
for length in [1, 2, 3, 10, 100, 1000].map(|x| black_box(x)) {
for length in [1, 2, 3, 10, 100, 1000].map(black_box) {
c.bench_function(
format!("choose_size-hinted_from_{length}_{rng_name}").as_str(),
|b| {
Expand Down
2 changes: 1 addition & 1 deletion benches/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn bench(c: &mut Criterion) {
}

fn bench_rng<Rng: RngCore + SeedableRng>(c: &mut Criterion, rng_name: &'static str) {
for length in [1, 2, 3, 10, 100, 1000, 10000].map(|x| black_box(x)) {
for length in [1, 2, 3, 10, 100, 1000, 10000].map(black_box) {
c.bench_function(format!("shuffle_{length}_{rng_name}").as_str(), |b| {
let mut rng = Rng::seed_from_u64(123);
let mut vec: Vec<usize> = (0..length).collect();
Expand Down
36 changes: 18 additions & 18 deletions rand_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,20 +470,20 @@ mod test {
let mut rng3 = rng1.clone();

let mut a = [0; 16];
(&mut a[..4]).copy_from_slice(&rng1.next_u32().to_le_bytes());
(&mut a[4..12]).copy_from_slice(&rng1.next_u64().to_le_bytes());
(&mut a[12..]).copy_from_slice(&rng1.next_u32().to_le_bytes());
a[..4].copy_from_slice(&rng1.next_u32().to_le_bytes());
a[4..12].copy_from_slice(&rng1.next_u64().to_le_bytes());
a[12..].copy_from_slice(&rng1.next_u32().to_le_bytes());

let mut b = [0; 16];
(&mut b[..4]).copy_from_slice(&rng2.next_u32().to_le_bytes());
(&mut b[4..8]).copy_from_slice(&rng2.next_u32().to_le_bytes());
(&mut b[8..]).copy_from_slice(&rng2.next_u64().to_le_bytes());
b[..4].copy_from_slice(&rng2.next_u32().to_le_bytes());
b[4..8].copy_from_slice(&rng2.next_u32().to_le_bytes());
b[8..].copy_from_slice(&rng2.next_u64().to_le_bytes());
assert_eq!(a, b);

let mut c = [0; 16];
(&mut c[..8]).copy_from_slice(&rng3.next_u64().to_le_bytes());
(&mut c[8..12]).copy_from_slice(&rng3.next_u32().to_le_bytes());
(&mut c[12..]).copy_from_slice(&rng3.next_u32().to_le_bytes());
c[..8].copy_from_slice(&rng3.next_u64().to_le_bytes());
c[8..12].copy_from_slice(&rng3.next_u32().to_le_bytes());
c[12..].copy_from_slice(&rng3.next_u32().to_le_bytes());
assert_eq!(a, c);
}

Expand Down Expand Up @@ -520,22 +520,22 @@ mod test {
let mut rng3 = rng1.clone();

let mut a = [0; 16];
(&mut a[..4]).copy_from_slice(&rng1.next_u32().to_le_bytes());
(&mut a[4..12]).copy_from_slice(&rng1.next_u64().to_le_bytes());
(&mut a[12..]).copy_from_slice(&rng1.next_u32().to_le_bytes());
a[..4].copy_from_slice(&rng1.next_u32().to_le_bytes());
a[4..12].copy_from_slice(&rng1.next_u64().to_le_bytes());
a[12..].copy_from_slice(&rng1.next_u32().to_le_bytes());

let mut b = [0; 16];
(&mut b[..4]).copy_from_slice(&rng2.next_u32().to_le_bytes());
(&mut b[4..8]).copy_from_slice(&rng2.next_u32().to_le_bytes());
(&mut b[8..]).copy_from_slice(&rng2.next_u64().to_le_bytes());
b[..4].copy_from_slice(&rng2.next_u32().to_le_bytes());
b[4..8].copy_from_slice(&rng2.next_u32().to_le_bytes());
b[8..].copy_from_slice(&rng2.next_u64().to_le_bytes());
assert_ne!(a, b);
assert_eq!(&a[..4], &b[..4]);
assert_eq!(&a[4..12], &b[8..]);

let mut c = [0; 16];
(&mut c[..8]).copy_from_slice(&rng3.next_u64().to_le_bytes());
(&mut c[8..12]).copy_from_slice(&rng3.next_u32().to_le_bytes());
(&mut c[12..]).copy_from_slice(&rng3.next_u32().to_le_bytes());
c[..8].copy_from_slice(&rng3.next_u64().to_le_bytes());
c[8..12].copy_from_slice(&rng3.next_u32().to_le_bytes());
c[12..].copy_from_slice(&rng3.next_u32().to_le_bytes());
assert_eq!(b, c);
}
}
4 changes: 2 additions & 2 deletions rand_distr/src/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Distribution<u64> for Binomial {
let mut u: f64 = rng.gen();
let mut x = 0;

while u > r as f64 {
while u > r {
u -= r;
x += 1;
if x > BINV_MAX_X {
Expand Down Expand Up @@ -332,7 +332,7 @@ mod test {
}

let mean = results.iter().sum::<f64>() / results.len() as f64;
assert!((mean as f64 - expected_mean).abs() < expected_mean / 50.0);
assert!((mean - expected_mean).abs() < expected_mean / 50.0);

let variance =
results.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / results.len() as f64;
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/src/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ mod test {
let distr = Cauchy::new(m, s).unwrap();
let mut rng = crate::test::rng(353);
for x in buf {
*x = rng.sample(&distr);
*x = rng.sample(distr);
}
}

Expand Down
6 changes: 3 additions & 3 deletions rand_distr/src/geometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Geometric {
/// Construct a new `Geometric` with the given shape parameter `p`
/// (probability of success on each trial).
pub fn new(p: f64) -> Result<Self, Error> {
if !p.is_finite() || p < 0.0 || p > 1.0 {
if !p.is_finite() || !(0.0..=1.0).contains(&p) {
Err(Error::InvalidProbability)
} else if p == 0.0 || p >= 2.0 / 3.0 {
Ok(Geometric { p, pi: p, k: 0 })
Expand Down Expand Up @@ -198,7 +198,7 @@ mod test {
}

let mean = results.iter().sum::<f64>() / results.len() as f64;
assert!((mean as f64 - expected_mean).abs() < expected_mean / 40.0);
assert!((mean - expected_mean).abs() < expected_mean / 40.0);

let variance =
results.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / results.len() as f64;
Expand Down Expand Up @@ -230,7 +230,7 @@ mod test {
}

let mean = results.iter().sum::<f64>() / results.len() as f64;
assert!((mean as f64 - expected_mean).abs() < expected_mean / 50.0);
assert!((mean - expected_mean).abs() < expected_mean / 50.0);

let variance =
results.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / results.len() as f64;
Expand Down
6 changes: 3 additions & 3 deletions rand_distr/src/hypergeometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ impl Distribution<u64> for Hypergeometric {
let mut u = rng.gen::<f64>();
while u > p && x < k as i64 { // the paper erroneously uses `until n < p`, which doesn't make any sense
u -= p;
p *= ((n1 as i64 - x as i64) * (k as i64 - x as i64)) as f64;
p /= ((x as i64 + 1) * (n2 as i64 - k as i64 + 1 + x as i64)) as f64;
p *= ((n1 as i64 - x) * (k as i64 - x)) as f64;
p /= ((x + 1) * (n2 as i64 - k as i64 + 1 + x)) as f64;
x += 1;
}
x
Expand Down Expand Up @@ -397,7 +397,7 @@ mod test {
}

let mean = results.iter().sum::<f64>() / results.len() as f64;
assert!((mean as f64 - expected_mean).abs() < expected_mean / 50.0);
assert!((mean - expected_mean).abs() < expected_mean / 50.0);

let variance =
results.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / results.len() as f64;
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/src/normal_inverse_gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
{
fn sample<R>(&self, rng: &mut R) -> F
where R: Rng + ?Sized {
let inv_gauss = rng.sample(&self.inverse_gaussian);
let inv_gauss = rng.sample(self.inverse_gaussian);

self.beta * inv_gauss + inv_gauss.sqrt() * rng.sample(StandardNormal)
}
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/src/skew_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ mod tests {
let mut rng = crate::test::rng(213);
let mut buf = [0.0; 4];
for x in &mut buf {
*x = rng.sample(&skew_normal);
*x = rng.sample(skew_normal);
}
for value in buf.iter() {
assert!(value.is_nan());
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/bernoulli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ mod test {
let distr = Bernoulli::new(0.4532).unwrap();
let mut buf = [false; 10];
for x in &mut buf {
*x = rng.sample(&distr);
*x = rng.sample(distr);
}
assert_eq!(buf, [
true, false, false, true, false, false, true, true, true, true
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ mod tests {
let mut rng = crate::test::rng(0x6f44f5646c2a7334);
let mut buf = [zero; 3];
for x in &mut buf {
*x = rng.sample(&distr);
*x = rng.sample(distr);
}
assert_eq!(&buf, expected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ mod tests {
let mut rng = crate::test::rng(807);
let mut buf = [zero; 5];
for x in &mut buf {
*x = rng.sample(&distr);
*x = rng.sample(distr);
}
assert_eq!(&buf, expected);
}
Expand Down
4 changes: 3 additions & 1 deletion src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,7 @@ mod tests {

#[test]
fn test_uniform_from_std_range_bad_limits() {
#![allow(clippy::reversed_empty_ranges)]
assert!(Uniform::try_from(100..10).is_err());
assert!(Uniform::try_from(100..100).is_err());
assert!(Uniform::try_from(100.0..10.0).is_err());
Expand All @@ -1695,6 +1696,7 @@ mod tests {

#[test]
fn test_uniform_from_std_range_inclusive_bad_limits() {
#![allow(clippy::reversed_empty_ranges)]
assert!(Uniform::try_from(100..=10).is_err());
assert!(Uniform::try_from(100..=99).is_err());
assert!(Uniform::try_from(100.0..=10.0).is_err());
Expand Down Expand Up @@ -1760,6 +1762,6 @@ mod tests {
assert_eq!(Uniform::new(1.0, 2.0).unwrap(), Uniform::new(1.0, 2.0).unwrap());

// To cover UniformInt
assert_eq!(Uniform::new(1 as u32, 2 as u32).unwrap(), Uniform::new(1 as u32, 2 as u32).unwrap());
assert_eq!(Uniform::new(1_u32, 2_u32).unwrap(), Uniform::new(1_u32, 2_u32).unwrap());
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#![allow(
clippy::float_cmp,
clippy::neg_cmp_op_on_partial_ord,
clippy::nonminimal_bool
)]

#[cfg(feature = "std")] extern crate std;
Expand Down
4 changes: 2 additions & 2 deletions src/seq/coin_flipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl<R: RngCore> CoinFlipper<R> {

if self.flip_c_heads(c) {
let numerator = 1 << c;
return self.gen_ratio(numerator, d);
self.gen_ratio(numerator, d)
} else {
return false;
false
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/seq/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ impl PartialEq for IndexVec {
fn eq(&self, other: &IndexVec) -> bool {
use self::IndexVec::*;
match (self, other) {
(&U32(ref v1), &U32(ref v2)) => v1 == v2,
(&USize(ref v1), &USize(ref v2)) => v1 == v2,
(&U32(ref v1), &USize(ref v2)) => {
(U32(v1), U32(v2)) => v1 == v2,
(USize(v1), USize(v2)) => v1 == v2,
(U32(v1), USize(v2)) => {
(v1.len() == v2.len()) && (v1.iter().zip(v2.iter()).all(|(x, y)| *x as usize == *y))
}
(&USize(ref v1), &U32(ref v2)) => {
(USize(v1), U32(v2)) => {
(v1.len() == v2.len()) && (v1.iter().zip(v2.iter()).all(|(x, y)| *x == *y as usize))
}
}
Expand Down

0 comments on commit e0292f3

Please sign in to comment.