Fix new clippy lints (#2036)

## Issue Addressed

NA

## Proposed Changes

Fixes new clippy lints in the whole project (mainly [manual_strip](https://rust-lang.github.io/rust-clippy/master/index.html#manual_strip) and [unnecessary_lazy_evaluations](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations)). Furthermore, removes `to_string()` calls on literals when used with the `?`-operator.
This commit is contained in:
blacktemplar
2020-12-03 01:10:26 +00:00
parent d3f0a21436
commit d8cda2d86e
71 changed files with 314 additions and 364 deletions

View File

@@ -86,14 +86,12 @@ impl ValidatorDefinition {
let voting_keystore_path = voting_keystore_path.as_ref().into();
let keystore =
Keystore::from_json_file(&voting_keystore_path).map_err(Error::UnableToOpenKeystore)?;
let voting_public_key = keystore
.public_key()
.ok_or_else(|| Error::InvalidKeystorePubkey)?;
let voting_public_key = keystore.public_key().ok_or(Error::InvalidKeystorePubkey)?;
Ok(ValidatorDefinition {
enabled: true,
voting_public_key,
description: keystore.description().unwrap_or_else(|| "").to_string(),
description: keystore.description().unwrap_or("").to_string(),
signing_definition: SigningDefinition::LocalKeystore {
voting_keystore_path,
voting_keystore_password_path: None,
@@ -214,7 +212,7 @@ impl ValidatorDefinitions {
Some(ValidatorDefinition {
enabled: true,
voting_public_key,
description: keystore.description().unwrap_or_else(|| "").to_string(),
description: keystore.description().unwrap_or("").to_string(),
signing_definition: SigningDefinition::LocalKeystore {
voting_keystore_path,
voting_keystore_password_path,

View File

@@ -101,8 +101,8 @@ pub fn parse_ssz_optional<T: Decode>(
matches
.value_of(name)
.map(|val| {
if val.starts_with("0x") {
let vec = hex::decode(&val[2..])
if let Some(stripped) = val.strip_prefix("0x") {
let vec = hex::decode(stripped)
.map_err(|e| format!("Unable to parse {} as hex: {:?}", name, e))?;
T::from_ssz_bytes(&vec)

View File

@@ -86,7 +86,7 @@ pub fn download_deposit_contract(
let abi = contract
.get("abi")
.ok_or_else(|| "Response does not contain key: abi".to_string())?
.ok_or("Response does not contain key: abi")?
.to_string();
verify_checksum(abi.as_bytes(), abi_checksum);
@@ -97,7 +97,7 @@ pub fn download_deposit_contract(
let bytecode = contract
.get("bytecode")
.ok_or_else(|| "Response does not contain key: bytecode".to_string())?
.ok_or("Response does not contain key: bytecode")?
.to_string();
verify_checksum(bytecode.as_bytes(), bytecode_checksum);

View File

@@ -55,8 +55,7 @@ pub fn decode_eth1_tx_data(
) -> Result<(DepositData, Hash256), DecodeError> {
let abi = Contract::load(ABI)?;
let function = abi.function("deposit")?;
let mut tokens =
function.decode_input(bytes.get(4..).ok_or_else(|| DecodeError::InadequateBytes)?)?;
let mut tokens = function.decode_input(bytes.get(4..).ok_or(DecodeError::InadequateBytes)?)?;
macro_rules! decode_token {
($type: ty, $to_fn: ident) => {

View File

@@ -79,7 +79,7 @@ impl ValidatorClientHttpClient {
let sig = response
.headers()
.get("Signature")
.ok_or_else(|| Error::MissingSignatureHeader)?
.ok_or(Error::MissingSignatureHeader)?
.to_str()
.map_err(|_| Error::InvalidSignatureHeader)?
.to_string();
@@ -96,7 +96,7 @@ impl ValidatorClientHttpClient {
Some(secp256k1::verify(&message, &sig, &self.server_pubkey))
})
.filter(|is_valid| *is_valid)
.ok_or_else(|| Error::InvalidSignatureHeader)?;
.ok_or(Error::InvalidSignatureHeader)?;
Ok(body)
}

View File

@@ -106,8 +106,8 @@ impl TryInto<Keypair> for YamlKeypair {
}
fn string_to_bytes(string: &str) -> Result<Vec<u8>, String> {
let string = if string.starts_with("0x") {
&string[2..]
let string = if let Some(stripped) = string.strip_prefix("0x") {
stripped
} else {
string
};

View File

@@ -122,7 +122,7 @@ impl Eth2TestnetConfig {
let genesis_state_bytes = self
.genesis_state_bytes
.as_ref()
.ok_or_else(|| "Genesis state is unknown".to_string())?;
.ok_or("Genesis state is unknown")?;
BeaconState::from_ssz_bytes(genesis_state_bytes)
.map_err(|e| format!("Genesis state SSZ bytes are invalid: {:?}", e))
@@ -156,8 +156,8 @@ impl Eth2TestnetConfig {
//
// This allows us to play nice with other clients that are expecting
// plain-text, not YAML.
let no_doc_header = if yaml.starts_with("---\n") {
&yaml[4..]
let no_doc_header = if let Some(stripped) = yaml.strip_prefix("---\n") {
stripped
} else {
&yaml
};

View File

@@ -138,7 +138,7 @@ impl<'a> Builder<'a> {
pub fn build(self) -> Result<ValidatorDir, Error> {
let (voting_keystore, voting_password) = self
.voting_keystore
.ok_or_else(|| Error::UninitializedVotingKeystore)?;
.ok_or(Error::UninitializedVotingKeystore)?;
let dir = self
.base_validators_dir

View File

@@ -167,8 +167,8 @@ impl ValidatorDir {
.map_err(Error::UnableToReadDepositData)
.and_then(|hex_bytes| {
let hex = std::str::from_utf8(&hex_bytes).map_err(|_| Error::DepositDataNotUtf8)?;
if hex.starts_with("0x") {
hex::decode(&hex[2..]).map_err(Error::DepositDataInvalidHex)
if let Some(stripped) = hex.strip_prefix("0x") {
hex::decode(stripped).map_err(Error::DepositDataInvalidHex)
} else {
Err(Error::DepositDataMissing0xPrefix)
}