From 37b8524a8f806e6d5ed663483e3af4474fbbad7e Mon Sep 17 00:00:00 2001 From: moricho Date: Thu, 5 Dec 2024 00:02:46 +0900 Subject: [PATCH 1/2] chore: Add clippy settings to `Cargo.toml` --- Cargo.toml | 18 ++++++++++++++++++ benches/bench.rs | 2 ++ src/hash_builder/mod.rs | 12 +++--------- src/proof/error.rs | 12 ++++++------ src/proof/verify.rs | 8 ++++---- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ebd9f2b..e20b74c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,24 @@ homepage = "https://github.com/alloy-rs/trie" repository = "https://github.com/alloy-rs/trie" exclude = [".github/", "deny.toml", "release.toml", "rustfmt.toml"] +[lints.rust] +missing-debug-implementations = "warn" +missing-docs = "warn" +unreachable-pub = "warn" +unused-must-use = "deny" +rust-2018-idioms = "deny" +unnameable-types = "warn" + +[lints.rustdoc] +all = "warn" + +[lints.clippy] +all = { level = "warn", priority = -1 } +missing-const-for-fn = "warn" +use-self = "warn" +option-if-let-else = "warn" +redundant-clone = "warn" + [dependencies] alloy-primitives = { version = "0.8.14", default-features = false, features = [ "rlp", diff --git a/benches/bench.rs b/benches/bench.rs index e7fead3..69e8dd7 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use alloy_trie::nodes::encode_path_leaf; use criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, diff --git a/src/hash_builder/mod.rs b/src/hash_builder/mod.rs index 17a17a1..d0ac43a 100644 --- a/src/hash_builder/mod.rs +++ b/src/hash_builder/mod.rs @@ -169,15 +169,9 @@ impl HashBuilder { } fn current_root(&self) -> B256 { - if let Some(node_ref) = self.stack.last() { - if let Some(hash) = node_ref.as_hash() { - hash - } else { - keccak256(node_ref) - } - } else { - EMPTY_ROOT_HASH - } + self.stack.last().map_or(EMPTY_ROOT_HASH, |node_ref| { + node_ref.as_hash().map_or_else(|| keccak256(node_ref), |hash| hash) + }) } /// Given a new element, it appends it to the stack and proceeds to loop through the stack state diff --git a/src/proof/error.rs b/src/proof/error.rs index 497ecc9..93b0a54 100644 --- a/src/proof/error.rs +++ b/src/proof/error.rs @@ -34,7 +34,7 @@ impl std::error::Error for ProofVerificationError { fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> { #[allow(deprecated)] match self { - ProofVerificationError::Rlp { 0: transparent } => { + Self::Rlp { 0: transparent } => { std::error::Error::source(transparent as &dyn std::error::Error) } _ => None, @@ -45,22 +45,22 @@ impl std::error::Error for ProofVerificationError { impl fmt::Display for ProofVerificationError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - ProofVerificationError::RootMismatch { got, expected } => { + Self::RootMismatch { got, expected } => { write!(f, "root mismatch. got: {got}. expected: {expected}") } - ProofVerificationError::ValueMismatch { path, got, expected } => { + Self::ValueMismatch { path, got, expected } => { write!(f, "value mismatch at path {path:?}. got: {got:?}. expected: {expected:?}") } - ProofVerificationError::UnexpectedEmptyRoot => { + Self::UnexpectedEmptyRoot => { write!(f, "unexpected empty root node") } - ProofVerificationError::Rlp(error) => fmt::Display::fmt(error, f), + Self::Rlp(error) => fmt::Display::fmt(error, f), } } } impl From for ProofVerificationError { fn from(source: alloy_rlp::Error) -> Self { - ProofVerificationError::Rlp(source) + Self::Rlp(source) } } diff --git a/src/proof/verify.rs b/src/proof/verify.rs index c1e612d..ef1c531 100644 --- a/src/proof/verify.rs +++ b/src/proof/verify.rs @@ -101,8 +101,8 @@ impl Deref for NodeDecodingResult { fn deref(&self) -> &Self::Target { match self { - NodeDecodingResult::Node(node) => node.as_slice(), - NodeDecodingResult::Value(value) => value, + Self::Node(node) => node.as_slice(), + Self::Value(value) => value, } } } @@ -332,7 +332,7 @@ mod tests { assert_eq!( verify_proof( root, - target.clone(), + target, Some(value.to_vec()), proof.into_nodes_sorted().iter().map(|(_, node)| node) ), @@ -510,7 +510,7 @@ mod tests { }, { buffer.clear(); - TrieNode::Leaf(LeafNode::new(Nibbles::from_nibbles([0xb]), value.clone())) + TrieNode::Leaf(LeafNode::new(Nibbles::from_nibbles([0xb]), value)) .rlp(&mut buffer) }, ], From 5c240ec4a5a4a7cf6892354b27d5deb36d8a42e4 Mon Sep 17 00:00:00 2001 From: moricho Date: Thu, 5 Dec 2024 00:24:57 +0900 Subject: [PATCH 2/2] Disable `option-if-let-els` lint --- Cargo.toml | 1 - src/hash_builder/mod.rs | 12 +++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e20b74c..97517e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,6 @@ all = "warn" all = { level = "warn", priority = -1 } missing-const-for-fn = "warn" use-self = "warn" -option-if-let-else = "warn" redundant-clone = "warn" [dependencies] diff --git a/src/hash_builder/mod.rs b/src/hash_builder/mod.rs index d0ac43a..17a17a1 100644 --- a/src/hash_builder/mod.rs +++ b/src/hash_builder/mod.rs @@ -169,9 +169,15 @@ impl HashBuilder { } fn current_root(&self) -> B256 { - self.stack.last().map_or(EMPTY_ROOT_HASH, |node_ref| { - node_ref.as_hash().map_or_else(|| keccak256(node_ref), |hash| hash) - }) + if let Some(node_ref) = self.stack.last() { + if let Some(hash) = node_ref.as_hash() { + hash + } else { + keccak256(node_ref) + } + } else { + EMPTY_ROOT_HASH + } } /// Given a new element, it appends it to the stack and proceeds to loop through the stack state