From 6d5defc73648e30a6c2a2dac011eac3f8ebdb703 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 14 Jun 2022 16:21:07 +1000 Subject: [PATCH] fix(clippy): Resolve some lifetime and reference lints (#4578) * Fix significant drop in match scrutinee https://github.com/rust-lang/rust/issues/93883 * Fix deref immutable value * Fix explicit 0 index when first() would do Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- zebra-chain/src/orchard/tree.rs | 3 ++- zebra-chain/src/sapling/tree.rs | 3 ++- zebra-chain/src/sprout/tree.rs | 3 ++- zebra-chain/src/transparent/serialize.rs | 2 +- zebra-state/src/service/chain_tip.rs | 12 +++++++++--- .../src/service/finalized_state/disk_format.rs | 2 +- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/zebra-chain/src/orchard/tree.rs b/zebra-chain/src/orchard/tree.rs index 42ad806c9ad..eb7c1dfd989 100644 --- a/zebra-chain/src/orchard/tree.rs +++ b/zebra-chain/src/orchard/tree.rs @@ -335,7 +335,8 @@ impl NoteCommitmentTree { .cached_root .write() .expect("a thread that previously held exclusive lock access panicked"); - match *write_root { + let read_root = write_root.as_ref().cloned(); + match read_root { // Another thread got write access first, return cached root. Some(root) => root, None => { diff --git a/zebra-chain/src/sapling/tree.rs b/zebra-chain/src/sapling/tree.rs index b0cfb302b21..628039a1ed8 100644 --- a/zebra-chain/src/sapling/tree.rs +++ b/zebra-chain/src/sapling/tree.rs @@ -340,7 +340,8 @@ impl NoteCommitmentTree { .cached_root .write() .expect("a thread that previously held exclusive lock access panicked"); - match *write_root { + let read_root = write_root.as_ref().cloned(); + match read_root { // Another thread got write access first, return cached root. Some(root) => root, None => { diff --git a/zebra-chain/src/sprout/tree.rs b/zebra-chain/src/sprout/tree.rs index ad99f9f968b..da01af506f5 100644 --- a/zebra-chain/src/sprout/tree.rs +++ b/zebra-chain/src/sprout/tree.rs @@ -274,7 +274,8 @@ impl NoteCommitmentTree { .cached_root .write() .expect("a thread that previously held exclusive lock access panicked"); - match *write_root { + let read_root = write_root.as_ref().cloned(); + match read_root { // Another thread got write access first, return cached root. Some(root) => root, None => { diff --git a/zebra-chain/src/transparent/serialize.rs b/zebra-chain/src/transparent/serialize.rs index 11cd98964e1..f896c5062ab 100644 --- a/zebra-chain/src/transparent/serialize.rs +++ b/zebra-chain/src/transparent/serialize.rs @@ -98,7 +98,7 @@ pub(crate) fn parse_coinbase_height( mut data: Vec, ) -> Result<(block::Height, CoinbaseData), SerializationError> { use block::Height; - match (data.get(0), data.len()) { + match (data.first(), data.len()) { // Blocks 1 through 16 inclusive encode block height with OP_N opcodes. (Some(op_n @ 0x51..=0x60), len) if len >= 1 => Ok(( Height((op_n - 0x50) as u32), diff --git a/zebra-state/src/service/chain_tip.rs b/zebra-state/src/service/chain_tip.rs index b7e82343d49..3657440c3a8 100644 --- a/zebra-state/src/service/chain_tip.rs +++ b/zebra-state/src/service/chain_tip.rs @@ -195,12 +195,18 @@ impl ChainTipSender { // a read-lock being created and living beyond the `self.sender.send(..)` call. If that // happens, the `send` method will attempt to obtain a write-lock and will dead-lock. // Without the binding, the guard is dropped at the end of the expression. - let needs_update = match (new_tip.as_ref(), self.sender.borrow().as_ref()) { + let active_hash = self + .sender + .borrow() + .as_ref() + .map(|active_value| active_value.hash); + + let needs_update = match (new_tip.as_ref(), active_hash) { // since the blocks have been contextually validated, // we know their hashes cover all the block data - (Some(new_tip), Some(active_value)) => new_tip.hash != active_value.hash, + (Some(new_tip), Some(active_hash)) => new_tip.hash != active_hash, (Some(_new_tip), None) => true, - (None, _active_value) => false, + (None, _active_value_hash) => false, }; if needs_update { diff --git a/zebra-state/src/service/finalized_state/disk_format.rs b/zebra-state/src/service/finalized_state/disk_format.rs index 8782b323775..c211cd5492f 100644 --- a/zebra-state/src/service/finalized_state/disk_format.rs +++ b/zebra-state/src/service/finalized_state/disk_format.rs @@ -73,7 +73,7 @@ where type Bytes = T::Bytes; fn as_bytes(&self) -> Self::Bytes { - T::as_bytes(&*self) + T::as_bytes(self) } }