Skip to content

Commit 233e3d8

Browse files
authored
Merge pull request #344 from dhardy/clippy
Implement various Clippy suggestions
2 parents 1960f83 + 71a5abb commit 233e3d8

20 files changed

+53
-49
lines changed

benches/distributions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ macro_rules! distr_float {
4343
let mut accum = 0.0;
4444
for _ in 0..::RAND_BENCH_N {
4545
let x: $ty = distr.sample(&mut rng);
46-
accum = accum + x;
46+
accum += x;
4747
}
4848
black_box(accum);
4949
});

examples/monty-hall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn simulate<R: Rng>(random_door: &Range<RangeInt<u32>>, rng: &mut R)
5858
choice = switch_door(choice, open);
5959
}
6060

61-
SimulationResult { win: choice == car, switch: switch }
61+
SimulationResult { win: choice == car, switch }
6262
}
6363

6464
// Returns the door the game host opens given our choice and knowledge of

src/distributions/binomial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Binomial {
4141
pub fn new(n: u64, p: f64) -> Binomial {
4242
assert!(p > 0.0, "Binomial::new called with p <= 0");
4343
assert!(p < 1.0, "Binomial::new called with p >= 1");
44-
Binomial { n: n, p: p }
44+
Binomial { n, p }
4545
}
4646
}
4747

src/distributions/float.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub(crate) trait IntoFloat {
2525
/// The resulting value will fall in a range that depends on the exponent.
2626
/// As an example the range with exponent 0 will be
2727
/// [2<sup>0</sup>..2<sup>1</sup>), which is [1..2).
28-
#[inline(always)]
2928
fn into_float_with_exponent(self, exponent: i32) -> Self::F;
3029
}
3130

src/distributions/gamma.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Gamma {
109109
} else {
110110
Large(GammaLargeShape::new_raw(shape, scale))
111111
};
112-
Gamma { repr: repr }
112+
Gamma { repr }
113113
}
114114
}
115115

@@ -126,9 +126,9 @@ impl GammaLargeShape {
126126
fn new_raw(shape: f64, scale: f64) -> GammaLargeShape {
127127
let d = shape - 1. / 3.;
128128
GammaLargeShape {
129-
scale: scale,
129+
scale,
130130
c: 1. / (9. * d).sqrt(),
131-
d: d
131+
d
132132
}
133133
}
134134
}
@@ -211,7 +211,7 @@ impl ChiSquared {
211211
assert!(k > 0.0, "ChiSquared::new called with `k` < 0");
212212
DoFAnythingElse(Gamma::new(0.5 * k, 2.0))
213213
};
214-
ChiSquared { repr: repr }
214+
ChiSquared { repr }
215215
}
216216
}
217217
impl Distribution<f64> for ChiSquared {

src/distributions/log_gamma.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ pub fn log_gamma(x: f64) -> f64 {
3939
// the first few terms of the series for Ag(x)
4040
let mut a = 1.000000000190015;
4141
let mut denom = x;
42-
for j in 0..6 {
42+
for coeff in &coefficients {
4343
denom += 1.0;
44-
a += coefficients[j] / denom;
44+
a += coeff / denom;
4545
}
4646

4747
// get everything together
4848
// a is Ag(x)
4949
// 2.5066... is sqrt(2pi)
50-
return log + (2.5066282746310005 * a / x).ln();
50+
log + (2.5066282746310005 * a / x).ln()
5151
}

src/distributions/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<'a, T: Clone> WeightedChoice<'a, T> {
273273
assert!(running_total != 0, "WeightedChoice::new called with a total weight of 0");
274274

275275
WeightedChoice {
276-
items: items,
276+
items,
277277
// we're likely to be generating numbers in this range
278278
// relatively often, so might as well cache it
279279
weight_range: Range::new(0, running_total)
@@ -320,7 +320,7 @@ impl<'a, T: Clone> Distribution<T> for WeightedChoice<'a, T> {
320320
}
321321
modifier /= 2;
322322
}
323-
return self.items[idx + 1].item.clone();
323+
self.items[idx + 1].item.clone()
324324
}
325325
}
326326

src/distributions/normal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ impl Normal {
104104
pub fn new(mean: f64, std_dev: f64) -> Normal {
105105
assert!(std_dev >= 0.0, "Normal::new called with `std_dev` < 0");
106106
Normal {
107-
mean: mean,
108-
std_dev: std_dev
107+
mean,
108+
std_dev
109109
}
110110
}
111111
}

src/distributions/other.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Distribution<char> for Uniform {
5858
impl Distribution<char> for Alphanumeric {
5959
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
6060
const RANGE: u32 = 26 + 26 + 10;
61-
const GEN_ASCII_STR_CHARSET: &'static [u8] =
61+
const GEN_ASCII_STR_CHARSET: &[u8] =
6262
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
6363
abcdefghijklmnopqrstuvwxyz\
6464
0123456789";

src/distributions/poisson.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ impl Poisson {
4646
assert!(lambda > 0.0, "Poisson::new called with lambda <= 0");
4747
let log_lambda = lambda.ln();
4848
Poisson {
49-
lambda: lambda,
49+
lambda,
5050
exp_lambda: (-lambda).exp(),
51-
log_lambda: log_lambda,
51+
log_lambda,
5252
sqrt_2lambda: (2.0 * lambda).sqrt(),
5353
magic_val: lambda * log_lambda - log_gamma(1.0 + lambda),
5454
}

src/entropy_rng.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ impl EntropyRng {
5555
}
5656
}
5757

58+
impl Default for EntropyRng {
59+
fn default() -> Self {
60+
EntropyRng::new()
61+
}
62+
}
63+
5864
impl RngCore for EntropyRng {
5965
fn next_u32(&mut self) -> u32 {
6066
impls::next_u32_via_fill(self)

src/jitter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl JitterRng {
196196
JitterRng {
197197
data: 0,
198198
rounds: 64,
199-
timer: timer,
199+
timer,
200200
mem_prev_index: 0,
201201
data_half_used: false,
202202
}
@@ -238,7 +238,7 @@ impl JitterRng {
238238
let mask = (1 << n_bits) - 1;
239239
for _ in 0..folds {
240240
rounds ^= time & mask;
241-
time = time >> n_bits;
241+
time >>= n_bits;
242242
}
243243

244244
rounds as u32
@@ -259,7 +259,7 @@ impl JitterRng {
259259
fn lfsr(mut data: u64, time: u64) -> u64{
260260
for i in 1..65 {
261261
let mut tmp = time << (64 - i);
262-
tmp = tmp >> (64 - 1);
262+
tmp >>= 64 - 1;
263263

264264
// Fibonacci LSFR with polynomial of
265265
// x^64 + x^61 + x^56 + x^31 + x^28 + x^23 + 1 which is

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ pub trait Rng: RngCore {
511511
fn gen_bool(&mut self, p: f64) -> bool {
512512
assert!(p >= 0.0 && p <= 1.0);
513513
// If `p` is constant, this will be evaluated at compile-time.
514-
let p_int = (p * core::u32::MAX as f64) as u32;
514+
let p_int = (p * f64::from(core::u32::MAX)) as u32;
515515
self.gen::<u32>() <= p_int
516516
}
517517

@@ -604,14 +604,14 @@ impl<R: RngCore + ?Sized> Rng for R {}
604604
/// [`try_fill`]: trait.Rng.html#method.try_fill
605605
pub trait AsByteSliceMut {
606606
/// Return a mutable reference to self as a byte slice
607-
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8];
607+
fn as_byte_slice_mut(&mut self) -> &mut [u8];
608608

609609
/// Call `to_le` on each element (i.e. byte-swap on Big Endian platforms).
610610
fn to_le(&mut self);
611611
}
612612

613613
impl AsByteSliceMut for [u8] {
614-
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
614+
fn as_byte_slice_mut(&mut self) -> &mut [u8] {
615615
self
616616
}
617617

@@ -621,7 +621,7 @@ impl AsByteSliceMut for [u8] {
621621
macro_rules! impl_as_byte_slice {
622622
($t:ty) => {
623623
impl AsByteSliceMut for [$t] {
624-
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
624+
fn as_byte_slice_mut(&mut self) -> &mut [u8] {
625625
unsafe {
626626
slice::from_raw_parts_mut(&mut self[0]
627627
as *mut $t
@@ -658,7 +658,7 @@ macro_rules! impl_as_byte_slice_arrays {
658658
impl_as_byte_slice_arrays!($n - 1, $($NN,)*);
659659

660660
impl<T> AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut {
661-
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
661+
fn as_byte_slice_mut(&mut self) -> &mut [u8] {
662662
self[..].as_byte_slice_mut()
663663
}
664664

@@ -711,7 +711,7 @@ impl<R: RngCore> Iterator for AsciiGenerator<R> {
711711
type Item = char;
712712

713713
fn next(&mut self) -> Option<char> {
714-
const GEN_ASCII_STR_CHARSET: &'static [u8] =
714+
const GEN_ASCII_STR_CHARSET: &[u8] =
715715
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
716716
abcdefghijklmnopqrstuvwxyz\
717717
0123456789";
@@ -820,7 +820,7 @@ impl SeedableRng for StdRng {
820820
}
821821

822822
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
823-
Hc128Rng::from_rng(rng).map(|result| StdRng(result))
823+
Hc128Rng::from_rng(rng).map(StdRng)
824824
}
825825
}
826826

@@ -901,7 +901,7 @@ impl SeedableRng for SmallRng {
901901
}
902902

903903
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
904-
XorShiftRng::from_rng(rng).map(|result| SmallRng(result))
904+
XorShiftRng::from_rng(rng).map(SmallRng)
905905
}
906906
}
907907

@@ -914,7 +914,7 @@ impl SeedableRng for SmallRng {
914914
/// seeded `Rng` for consistency over time you should pick one algorithm and
915915
/// create the `Rng` yourself.
916916
///
917-
/// This will seed the generator with randomness from thread_rng.
917+
/// This will seed the generator with randomness from `thread_rng`.
918918
#[deprecated(since="0.5.0", note="removed in favor of SmallRng")]
919919
#[cfg(feature="std")]
920920
pub fn weak_rng() -> XorShiftRng {

src/os.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,13 @@ mod imp {
330330
.read(true)
331331
.custom_flags(libc::O_NONBLOCK)
332332
.open("/dev/random")
333-
.map_err(|err| map_err(err))?;
333+
.map_err(map_err)?;
334334
let mut buf = [0u8; 1];
335-
file.read_exact(&mut buf).map_err(|err| map_err(err))?;
335+
file.read_exact(&mut buf).map_err(map_err)?;
336336
}
337337

338338
info!("OsRng: opening random device /dev/urandom");
339-
let file = File::open("/dev/urandom").map_err(|err| map_err(err))?;
339+
let file = File::open("/dev/urandom").map_err(map_err)?;
340340
*guard = Some(file);
341341
};
342342
Ok(())

src/prng/chacha.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl SeedableRng for ChaChaRng {
9595
}
9696

9797
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
98-
BlockRng::<ChaChaCore>::from_rng(rng).map(|result| ChaChaRng(result))
98+
BlockRng::<ChaChaCore>::from_rng(rng).map(ChaChaRng)
9999
}
100100
}
101101

src/prng/hc128.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl SeedableRng for Hc128Rng {
9292
}
9393

9494
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
95-
BlockRng::<Hc128Core>::from_rng(rng).map(|result| Hc128Rng(result))
95+
BlockRng::<Hc128Core>::from_rng(rng).map(Hc128Rng)
9696
}
9797
}
9898

@@ -303,7 +303,7 @@ impl Hc128Core {
303303
.wrapping_add(t[i-16]).wrapping_add(256 + i as u32);
304304
}
305305

306-
let mut core = Self { t: t, counter1024: 0 };
306+
let mut core = Self { t, counter1024: 0 };
307307

308308
// run the cipher 1024 steps
309309
for _ in 0..64 { core.sixteen_steps() };

src/prng/isaac.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl RngCore for IsaacRng {
260260
/// Creates a new ISAAC random number generator.
261261
///
262262
/// The author Bob Jenkins describes how to best initialize ISAAC here:
263-
/// https://rt.cpan.org/Public/Bug/Display.html?id=64324
263+
/// <https://rt.cpan.org/Public/Bug/Display.html?id=64324>
264264
/// The answer is included here just in case:
265265
///
266266
/// "No, you don't need a full 8192 bits of seed data. Normal key sizes will do
@@ -315,7 +315,7 @@ fn init(mut mem: [w32; RAND_SIZE], rounds: u32) -> IsaacRng {
315315

316316
let mut rng = IsaacRng {
317317
rsl: [0; RAND_SIZE],
318-
mem: mem,
318+
mem,
319319
a: w(0),
320320
b: w(0),
321321
c: w(0),

src/prng/isaac64.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
6464
/// }
6565
/// ```
6666
///
67-
/// See for more information the description in rand::prng::IsaacRng.
67+
/// See for more information the documentation of [`IsaacRng`].
6868
///
6969
/// [1]: Bob Jenkins, [*ISAAC and RC4*](
7070
/// http://burtleburtle.net/bob/rand/isaac.html)
71+
/// [`IsaacRng`]: prng/isaac/struct.IsaacRng.html
7172
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
7273
pub struct Isaac64Rng {
7374
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
@@ -294,7 +295,7 @@ fn init(mut mem: [w64; RAND_SIZE], rounds: u32) -> Isaac64Rng {
294295

295296
Isaac64Rng {
296297
rsl: [0; RAND_SIZE],
297-
mem: mem,
298+
mem,
298299
a: w(0),
299300
b: w(0),
300301
c: w(0),

src/reseeding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ where R: BlockRngCore + SeedableRng,
7777
BlockRng {
7878
core: ReseedingCore {
7979
inner: rng,
80-
reseeder: reseeder,
80+
reseeder,
8181
threshold: threshold as i64,
8282
bytes_until_reseed: threshold as i64,
8383
},

src/seq.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ pub fn sample_indices<R>(rng: &mut R, length: usize, amount: usize) -> Vec<usize
163163
/// This allocates the entire `length` of indices and randomizes only the first `amount`.
164164
/// It then truncates to `amount` and returns.
165165
///
166-
/// This is better than using a HashMap "cache" when `amount >= length / 2` since it does not
167-
/// require allocating an extra cache and is much faster.
166+
/// This is better than using a `HashMap` "cache" when `amount >= length / 2`
167+
/// since it does not require allocating an extra cache and is much faster.
168168
fn sample_indices_inplace<R>(rng: &mut R, length: usize, amount: usize) -> Vec<usize>
169169
where R: Rng + ?Sized,
170170
{
@@ -173,21 +173,19 @@ fn sample_indices_inplace<R>(rng: &mut R, length: usize, amount: usize) -> Vec<u
173173
indices.extend(0..length);
174174
for i in 0..amount {
175175
let j: usize = rng.gen_range(i, length);
176-
let tmp = indices[i];
177-
indices[i] = indices[j];
178-
indices[j] = tmp;
176+
indices.swap(i, j);
179177
}
180178
indices.truncate(amount);
181179
debug_assert_eq!(indices.len(), amount);
182180
indices
183181
}
184182

185183

186-
/// This method performs a partial fisher-yates on a range of indices using a HashMap
187-
/// as a cache to record potential collisions.
184+
/// This method performs a partial fisher-yates on a range of indices using a
185+
/// `HashMap` as a cache to record potential collisions.
188186
///
189187
/// The cache avoids allocating the entire `length` of values. This is especially useful when
190-
/// `amount <<< length`, i.e. select 3 non-repeating from 1_000_000
188+
/// `amount <<< length`, i.e. select 3 non-repeating from `1_000_000`
191189
fn sample_indices_cache<R>(
192190
rng: &mut R,
193191
length: usize,

0 commit comments

Comments
 (0)