From 7daffb572ae09d65dfefd8689718659e9fb2b396 Mon Sep 17 00:00:00 2001 From: teor Date: Fri, 14 Oct 2022 18:53:40 +1000 Subject: [PATCH] chore(clippy): Fix clippy cast and closure warnings (#5378) * Fix clippy cast and closure warnings * Silence incorrect lints in Clippy 1.64 * Use correct clippy syntax Co-authored-by: Marek * Disable an incorrect Clippy 1.64 lint * Disable lint on the correct code * Try another place Co-authored-by: Marek --- zebra-chain/src/history_tree.rs | 2 +- zebra-chain/src/orchard/shielded_data.rs | 5 ++++- zebra-chain/src/transaction/arbitrary.rs | 2 +- zebra-chain/src/transaction/serialize.rs | 11 ++++++++--- zebra-chain/src/transaction/tests/vectors.rs | 4 ++-- zebra-network/src/protocol/external/codec.rs | 6 +++--- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/zebra-chain/src/history_tree.rs b/zebra-chain/src/history_tree.rs index 223d1b372..f5b105c12 100644 --- a/zebra-chain/src/history_tree.rs +++ b/zebra-chain/src/history_tree.rs @@ -275,7 +275,7 @@ impl NonEmptyHistoryTree { // /\ /\ /\ /\ /\ /\ /\ 0 // // We start by determining the altitude of the highest peak (A). - let mut alt = (32 - ((self.size + 1) as u32).leading_zeros() - 1) - 1; + let mut alt = (32 - (self.size + 1).leading_zeros() - 1) - 1; // We determine the position of the highest peak (A) by pretending it is the right // sibling in a tree, and its left-most leaf has position 0. Then the left sibling diff --git a/zebra-chain/src/orchard/shielded_data.rs b/zebra-chain/src/orchard/shielded_data.rs index 3ec59dd59..e9a02eab1 100644 --- a/zebra-chain/src/orchard/shielded_data.rs +++ b/zebra-chain/src/orchard/shielded_data.rs @@ -249,7 +249,10 @@ impl ZcashDeserialize for Flags { // Consensus rule: "In a version 5 transaction, // the reserved bits 2..7 of the flagsOrchard field MUST be zero." // https://zips.z.cash/protocol/protocol.pdf#txnencodingandconsensus + // + // Clippy 1.64 is wrong here, this lazy evaluation is necessary, constructors are functions. This is fixed in 1.66. + #[allow(clippy::unnecessary_lazy_evaluations)] Flags::from_bits(reader.read_u8()?) - .ok_or(SerializationError::Parse("invalid reserved orchard flags")) + .ok_or_else(|| SerializationError::Parse("invalid reserved orchard flags")) } } diff --git a/zebra-chain/src/transaction/arbitrary.rs b/zebra-chain/src/transaction/arbitrary.rs index d97f2bbdd..8413aed4f 100644 --- a/zebra-chain/src/transaction/arbitrary.rs +++ b/zebra-chain/src/transaction/arbitrary.rs @@ -535,7 +535,7 @@ impl Arbitrary for LockTime { (block::Height::MIN.0..=LockTime::MAX_HEIGHT.0) .prop_map(|n| LockTime::Height(block::Height(n))), (LockTime::MIN_TIMESTAMP..=LockTime::MAX_TIMESTAMP) - .prop_map(|n| { LockTime::Time(Utc.timestamp(n as i64, 0)) }) + .prop_map(|n| { LockTime::Time(Utc.timestamp(n, 0)) }) ] .boxed() } diff --git a/zebra-chain/src/transaction/serialize.rs b/zebra-chain/src/transaction/serialize.rs index 47b8e5bc9..b29cd675f 100644 --- a/zebra-chain/src/transaction/serialize.rs +++ b/zebra-chain/src/transaction/serialize.rs @@ -872,11 +872,16 @@ impl ZcashDeserialize for Transaction { } // Denoted as `nConsensusBranchId` in the spec. // Convert it to a NetworkUpgrade + // + // Clippy 1.64 is wrong here, this lazy evaluation is necessary, constructors are functions. This is fixed in 1.66. + #[allow(clippy::unnecessary_lazy_evaluations)] let network_upgrade = NetworkUpgrade::from_branch_id(limited_reader.read_u32::()?) - .ok_or(SerializationError::Parse( - "expected a valid network upgrade from the consensus branch id", - ))?; + .ok_or_else(|| { + SerializationError::Parse( + "expected a valid network upgrade from the consensus branch id", + ) + })?; // Denoted as `lock_time` in the spec. let lock_time = LockTime::zcash_deserialize(&mut limited_reader)?; diff --git a/zebra-chain/src/transaction/tests/vectors.rs b/zebra-chain/src/transaction/tests/vectors.rs index 29368f461..8d03cbc78 100644 --- a/zebra-chain/src/transaction/tests/vectors.rs +++ b/zebra-chain/src/transaction/tests/vectors.rs @@ -752,7 +752,7 @@ fn test_vec243_2() -> Result<()> { let lock_script = Script::new(&[]); let prevout = transparent::Output { value, lock_script }; - let index = input_ind as usize; + let index = input_ind; let all_previous_outputs = mock_pre_v5_output_list(prevout, input_ind); let alt_sighash = crate::primitives::zcash_primitives::sighash( @@ -805,7 +805,7 @@ fn test_vec243_3() -> Result<()> { "76a914507173527b4c3318a2aecd793bf1cfed705950cf88ac", )?); let prevout = transparent::Output { value, lock_script }; - let index = input_ind as usize; + let index = input_ind; let alt_sighash = crate::primitives::zcash_primitives::sighash( &transaction, diff --git a/zebra-network/src/protocol/external/codec.rs b/zebra-network/src/protocol/external/codec.rs index 6dea5ea99..a2bc6552d 100644 --- a/zebra-network/src/protocol/external/codec.rs +++ b/zebra-network/src/protocol/external/codec.rs @@ -465,6 +465,8 @@ impl Decoder for Codec { impl Codec { fn read_version(&self, mut reader: R) -> Result { + // Clippy 1.64 is wrong here, this lazy evaluation is necessary, constructors are functions. This is fixed in 1.66. + #[allow(clippy::unnecessary_lazy_evaluations)] Ok(VersionMessage { version: Version(reader.read_u32::()?), // Use from_bits_truncate to discard unknown service bits. @@ -472,9 +474,7 @@ impl Codec { timestamp: Utc .timestamp_opt(reader.read_i64::()?, 0) .single() - .ok_or(Error::Parse( - "version timestamp is out of range for DateTime", - ))?, + .ok_or_else(|| Error::Parse("version timestamp is out of range for DateTime"))?, address_recv: AddrInVersion::zcash_deserialize(&mut reader)?, address_from: AddrInVersion::zcash_deserialize(&mut reader)?, nonce: Nonce(reader.read_u64::()?),