Fix various clippy lints

This commit is contained in:
Paul Hauner
2019-02-13 07:32:31 +11:00
parent 5039001eba
commit 5c1d0dcea5
17 changed files with 105 additions and 110 deletions

View File

@@ -1,6 +1,5 @@
use super::{AggregatePublicKey, AggregateSignature, AttestationData, Bitfield, Hash256};
use crate::test_utils::TestRandom;
use bls::bls_verify_aggregate;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};

View File

@@ -283,7 +283,7 @@ impl BeaconState {
shuffled_active_validator_indices
.honey_badger_split(committees_per_epoch as usize)
.filter_map(|slice: &[usize]| Some(slice.to_vec()))
.map(|slice: &[usize]| slice.to_vec())
.collect()
}
@@ -522,11 +522,9 @@ impl BeaconState {
.filter(|i| eligible(*i))
.collect();
eligable_indices.sort_by_key(|i| self.validator_registry[*i].exit_epoch);
let mut withdrawn_so_far = 0;
for index in eligable_indices {
self.prepare_validator_for_withdrawal(index);
withdrawn_so_far += 1;
if withdrawn_so_far >= spec.max_withdrawals_per_epoch {
for (withdrawn_so_far, index) in eligable_indices.iter().enumerate() {
self.prepare_validator_for_withdrawal(*index);
if withdrawn_so_far as u64 >= spec.max_withdrawals_per_epoch {
break;
}
}
@@ -796,11 +794,7 @@ impl BeaconState {
for (i, a) in attestations.iter().enumerate() {
let participants =
self.get_attestation_participants(&a.data, &a.aggregation_bitfield, spec)?;
if participants
.iter()
.find(|i| **i == validator_index)
.is_some()
{
if participants.iter().any(|i| *i == validator_index) {
included_attestations.push(i);
}
}

View File

@@ -13,9 +13,10 @@ use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use slog;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use ssz::{hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use std::cmp::{Ord, Ordering};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::Iterator;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign};
@@ -243,6 +244,18 @@ macro_rules! impl_ssz {
};
}
macro_rules! impl_hash {
($type: ident) => {
// Implemented to stop clippy lint:
// https://rust-lang.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
impl Hash for $type {
fn hash<H: Hasher>(&self, state: &mut H) {
ssz_encode(self).hash(state)
}
}
};
}
macro_rules! impl_common {
($type: ident) => {
impl_from_into_u64!($type);
@@ -252,13 +265,14 @@ macro_rules! impl_common {
impl_math!($type);
impl_display!($type);
impl_ssz!($type);
impl_hash!($type);
};
}
#[derive(Eq, Debug, Clone, Copy, Default, Serialize, Hash)]
#[derive(Eq, Debug, Clone, Copy, Default, Serialize)]
pub struct Slot(u64);
#[derive(Eq, Debug, Clone, Copy, Default, Serialize, Hash)]
#[derive(Eq, Debug, Clone, Copy, Default, Serialize)]
pub struct Epoch(u64);
impl_common!(Slot);
@@ -269,7 +283,7 @@ impl Slot {
Slot(slot)
}
pub fn epoch(&self, epoch_length: u64) -> Epoch {
pub fn epoch(self, epoch_length: u64) -> Epoch {
Epoch::from(self.0 / epoch_length)
}
@@ -287,11 +301,11 @@ impl Epoch {
Epoch(u64::max_value())
}
pub fn start_slot(&self, epoch_length: u64) -> Slot {
pub fn start_slot(self, epoch_length: u64) -> Slot {
Slot::from(self.0.saturating_mul(epoch_length))
}
pub fn end_slot(&self, epoch_length: u64) -> Slot {
pub fn end_slot(self, epoch_length: u64) -> Slot {
Slot::from(
self.0
.saturating_add(1)

View File

@@ -78,7 +78,7 @@ impl Default for Validator {
impl<T: RngCore> TestRandom<T> for StatusFlags {
fn random_for_test(rng: &mut T) -> Self {
let options = vec![StatusFlags::InitiatedExit, StatusFlags::Withdrawable];
options[(rng.next_u32() as usize) % options.len()].clone()
options[(rng.next_u32() as usize) % options.len()]
}
}
@@ -130,7 +130,7 @@ impl TreeHash for Validator {
result.append(&mut self.exit_epoch.hash_tree_root());
result.append(&mut self.withdrawal_epoch.hash_tree_root());
result.append(&mut self.penalized_epoch.hash_tree_root());
result.append(&mut (status_flag_to_byte(self.status_flags) as u64).hash_tree_root());
result.append(&mut u64::from(status_flag_to_byte(self.status_flags)).hash_tree_root());
hash(&result)
}
}