fix: clippy warnings, or_fun_call and unnecessary_lazy_evaluations (#846)

This commit is contained in:
Kim, JinSan
2023-01-13 19:10:02 +09:00
committed by GitHub
parent e6ca4c56c6
commit c4bafe809b
8 changed files with 28 additions and 38 deletions

View File

@ -240,7 +240,7 @@ async fn handle_events(mut events: impl Stream<Item = NodeEvent> + Unpin) {
},
NodeEvent::Network(NetworkEvent::SessionClosed { peer_id, reason }) => {
state.connected_peers -= 1;
let reason = reason.map(|s| s.to_string()).unwrap_or("None".to_string());
let reason = reason.map(|s| s.to_string()).unwrap_or_else(|| "None".to_string());
warn!(target: "reth::cli", connected_peers = state.connected_peers, peer_id = %peer_id, %reason, "Peer disconnected.");
},
NodeEvent::Pipeline(PipelineEvent::Running { stage_id, stage_progress }) => {
@ -266,7 +266,7 @@ async fn handle_events(mut events: impl Stream<Item = NodeEvent> + Unpin) {
}
},
_ = interval.tick() => {
let stage = state.current_stage.map(|id| id.to_string()).unwrap_or("None".to_string());
let stage = state.current_stage.map(|id| id.to_string()).unwrap_or_else(|| "None".to_string());
info!(target: "reth::cli", connected_peers = state.connected_peers, %stage, checkpoint = state.current_checkpoint, "Status");
}
}

View File

@ -205,9 +205,10 @@ pub async fn run_test(path: PathBuf) -> eyre::Result<()> {
tracing::trace!("Our storage:{:?}", storage);
for (address, test_account) in state.iter() {
// check account
let our_account = tx.get::<tables::PlainAccountState>(*address)?.ok_or(
eyre!("Account is missing: {address} expected: {:?}", test_account),
)?;
let our_account =
tx.get::<tables::PlainAccountState>(*address)?.ok_or_else(|| {
eyre!("Account is missing: {address} expected: {:?}", test_account)
})?;
if test_account.balance.0 != our_account.balance {
return Err(eyre!(
"Account {address} balance diff, expected {} got {}",
@ -244,15 +245,19 @@ pub async fn run_test(path: PathBuf) -> eyre::Result<()> {
for (JsonU256(key), JsonU256(value)) in test_account.storage.iter() {
let our_value = storage
.get(address)
.ok_or(eyre!(
"Missing storage from test {storage:?} got {:?}",
test_account.storage
))?
.ok_or_else(|| {
eyre!(
"Missing storage from test {storage:?} got {:?}",
test_account.storage
)
})?
.get(key)
.ok_or(eyre!(
"Slot is missing from table {storage:?} got:{:?}",
test_account.storage
))?;
.ok_or_else(|| {
eyre!(
"Slot is missing from table {storage:?} got:{:?}",
test_account.storage
)
})?;
if value != our_value {
return Err(eyre!(
"Storage diff we got {address}: {storage:?} but expect: {:?}",

View File

@ -109,12 +109,12 @@ pub fn random_block(
let mut rng = thread_rng();
// Generate transactions
let tx_count = tx_count.unwrap_or(rng.gen::<u8>());
let tx_count = tx_count.unwrap_or_else(|| rng.gen::<u8>());
let transactions: Vec<TransactionSigned> = (0..tx_count).map(|_| random_signed_tx()).collect();
let total_gas = transactions.iter().fold(0, |sum, tx| sum + tx.transaction.gas_limit());
// Generate ommers
let ommers_count = ommers_count.unwrap_or(rng.gen_range(0..2));
let ommers_count = ommers_count.unwrap_or_else(|| rng.gen_range(0..2));
let ommers =
(0..ommers_count).map(|_| random_header(number, parent).unseal()).collect::<Vec<_>>();

View File

@ -97,7 +97,7 @@ fn parse_metrics_attr(node: &DeriveInput) -> Result<MetricsAttr> {
}
}
let scope = scope.ok_or(Error::new_spanned(node, "`scope = ..` must be set."))?;
let scope = scope.ok_or_else(|| Error::new_spanned(node, "`scope = ..` must be set."))?;
Ok(MetricsAttr { scope, separator })
}

View File

@ -53,7 +53,7 @@ where
// `Framed` returns `None` if the underlying stream is no longer readable, and the codec is
// unable to decode another message from the (partially filled) buffer. This usually happens
// if the remote drops the TcpStream.
let msg = msg.ok_or_else(|| ECIESErrorImpl::UnreadableStream)?;
let msg = msg.ok_or(ECIESErrorImpl::UnreadableStream)?;
trace!("parsing ecies ack ...");
if matches!(msg, IngressECIESValue::Ack) {

View File

@ -7,9 +7,10 @@ use std::fmt::Display;
use thiserror::Error;
/// RLPx disconnect reason.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum DisconnectReason {
/// Disconnect requested by the local node or remote peer.
#[default]
DisconnectRequested = 0x00,
/// TCP related error
TcpSubsystemError = 0x01,
@ -65,12 +66,6 @@ impl Display for DisconnectReason {
}
}
impl Default for DisconnectReason {
fn default() -> Self {
DisconnectReason::DisconnectRequested
}
}
/// This represents an unknown disconnect reason with the given code.
#[derive(Debug, Clone, Error)]
#[error("unknown disconnect reason: {0}")]

View File

@ -715,11 +715,12 @@ impl TryFrom<u8> for P2PMessageID {
}
/// RLPx `p2p` protocol version
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProtocolVersion {
/// `p2p` version 4
V4 = 4,
/// `p2p` version 5
#[default]
V5 = 5,
}
@ -744,12 +745,6 @@ impl Decodable for ProtocolVersion {
}
}
impl Default for ProtocolVersion {
fn default() -> Self {
ProtocolVersion::V5
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -3,7 +3,7 @@ use std::str::FromStr;
/// Ethereum mainnet hardforks
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub enum Hardfork {
Frontier,
Homestead,
@ -19,6 +19,7 @@ pub enum Hardfork {
London,
ArrowGlacier,
GrayGlacier,
#[default]
Latest,
}
@ -155,12 +156,6 @@ impl FromStr for Hardfork {
}
}
impl Default for Hardfork {
fn default() -> Self {
Hardfork::Latest
}
}
impl From<BlockNumber> for Hardfork {
fn from(num: BlockNumber) -> Hardfork {
match num {