diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index 26604cbcc..c4672eac7 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -240,7 +240,7 @@ async fn handle_events(mut events: impl Stream + 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 + 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"); } } diff --git a/bin/reth/src/test_eth_chain/runner.rs b/bin/reth/src/test_eth_chain/runner.rs index 877d1d17a..17f9baf55 100644 --- a/bin/reth/src/test_eth_chain/runner.rs +++ b/bin/reth/src/test_eth_chain/runner.rs @@ -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::(*address)?.ok_or( - eyre!("Account is missing: {address} expected: {:?}", test_account), - )?; + let our_account = + tx.get::(*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: {:?}", diff --git a/crates/interfaces/src/test_utils/generators.rs b/crates/interfaces/src/test_utils/generators.rs index 1a88b3b42..3e8ed597e 100644 --- a/crates/interfaces/src/test_utils/generators.rs +++ b/crates/interfaces/src/test_utils/generators.rs @@ -109,12 +109,12 @@ pub fn random_block( let mut rng = thread_rng(); // Generate transactions - let tx_count = tx_count.unwrap_or(rng.gen::()); + let tx_count = tx_count.unwrap_or_else(|| rng.gen::()); let transactions: Vec = (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::>(); diff --git a/crates/metrics/metrics-derive/src/expand.rs b/crates/metrics/metrics-derive/src/expand.rs index 7e93211c1..b2b2e3e0b 100644 --- a/crates/metrics/metrics-derive/src/expand.rs +++ b/crates/metrics/metrics-derive/src/expand.rs @@ -97,7 +97,7 @@ fn parse_metrics_attr(node: &DeriveInput) -> Result { } } - 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 }) } diff --git a/crates/net/ecies/src/stream.rs b/crates/net/ecies/src/stream.rs index ad31cebe0..dd9274b18 100644 --- a/crates/net/ecies/src/stream.rs +++ b/crates/net/ecies/src/stream.rs @@ -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) { diff --git a/crates/net/eth-wire/src/disconnect.rs b/crates/net/eth-wire/src/disconnect.rs index c8a460e73..3ab34eea7 100644 --- a/crates/net/eth-wire/src/disconnect.rs +++ b/crates/net/eth-wire/src/disconnect.rs @@ -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}")] diff --git a/crates/net/eth-wire/src/p2pstream.rs b/crates/net/eth-wire/src/p2pstream.rs index a7fff8ca0..9869ca933 100644 --- a/crates/net/eth-wire/src/p2pstream.rs +++ b/crates/net/eth-wire/src/p2pstream.rs @@ -715,11 +715,12 @@ impl TryFrom 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::*; diff --git a/crates/primitives/src/hardfork.rs b/crates/primitives/src/hardfork.rs index e3e55bfa7..a10abd239 100644 --- a/crates/primitives/src/hardfork.rs +++ b/crates/primitives/src/hardfork.rs @@ -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 for Hardfork { fn from(num: BlockNumber) -> Hardfork { match num {