From aafb5d1ac3543ccb617f2bc81695977d2750a5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Fri, 19 Apr 2024 18:44:08 +0200 Subject: [PATCH] core: clippy fixes --- crates/core/src/bytes.rs | 2 +- crates/core/src/chain.rs | 2 +- crates/core/src/eth_bridge_pool.rs | 2 +- crates/core/src/ethereum_events.rs | 2 ++ crates/core/src/ethereum_structs.rs | 2 ++ crates/core/src/keccak.rs | 2 +- crates/core/src/key/common.rs | 2 +- crates/core/src/key/secp256k1.rs | 12 ++++++++++-- crates/core/src/masp.rs | 2 +- crates/core/src/time.rs | 9 ++++++++- 10 files changed, 28 insertions(+), 9 deletions(-) diff --git a/crates/core/src/bytes.rs b/crates/core/src/bytes.rs index e2d9c27058e..e157c80428b 100644 --- a/crates/core/src/bytes.rs +++ b/crates/core/src/bytes.rs @@ -8,7 +8,7 @@ pub struct ByteBuf<'a>(pub &'a [u8]); impl<'a> std::fmt::LowerHex for ByteBuf<'a> { fn fmt( &self, - f: &mut std::fmt::Formatter, + f: &mut std::fmt::Formatter<'_>, ) -> std::result::Result<(), std::fmt::Error> { for byte in self.0 { f.write_fmt(format_args!("{:02x}", byte))?; diff --git a/crates/core/src/chain.rs b/crates/core/src/chain.rs index 511c23d7854..16f46b1d288 100644 --- a/crates/core/src/chain.rs +++ b/crates/core/src/chain.rs @@ -58,7 +58,7 @@ impl<'de> Deserialize<'de> for ProposalBytes { impl<'de> serde::de::Visitor<'de> for Visitor { type Value = ProposalBytes; - fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "a u64 in the range 1 - {}", diff --git a/crates/core/src/eth_bridge_pool.rs b/crates/core/src/eth_bridge_pool.rs index 704e37253fc..69cb0291ada 100644 --- a/crates/core/src/eth_bridge_pool.rs +++ b/crates/core/src/eth_bridge_pool.rs @@ -104,7 +104,7 @@ pub enum TransferToEthereumKind { } impl std::fmt::Display for TransferToEthereumKind { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Erc20 => write!(f, "ERC20"), Self::Nut => write!(f, "NUT"), diff --git a/crates/core/src/ethereum_events.rs b/crates/core/src/ethereum_events.rs index ec6984ac427..8c09f4234ca 100644 --- a/crates/core/src/ethereum_events.rs +++ b/crates/core/src/ethereum_events.rs @@ -113,6 +113,7 @@ impl From for Uint { impl Add for Uint { type Output = Self; + #[allow(clippy::arithmetic_side_effects)] fn add(self, rhs: u64) -> Self::Output { (ethUint(self.0) + rhs).into() } @@ -121,6 +122,7 @@ impl Add for Uint { impl Sub for Uint { type Output = Self; + #[allow(clippy::arithmetic_side_effects)] fn sub(self, rhs: u64) -> Self::Output { (ethUint(self.0) - rhs).into() } diff --git a/crates/core/src/ethereum_structs.rs b/crates/core/src/ethereum_structs.rs index 3236dfc95db..9c6fb34e808 100644 --- a/crates/core/src/ethereum_structs.rs +++ b/crates/core/src/ethereum_structs.rs @@ -137,12 +137,14 @@ impl<'a> From<&'a BlockHeight> for &'a Uint256 { impl Add for BlockHeight { type Output = BlockHeight; + #[allow(clippy::arithmetic_side_effects)] fn add(self, rhs: Self) -> Self::Output { Self(self.0 + rhs.0) } } impl AddAssign for BlockHeight { + #[allow(clippy::arithmetic_side_effects)] fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; } diff --git a/crates/core/src/keccak.rs b/crates/core/src/keccak.rs index 70ee83799b2..8ca44cc718c 100644 --- a/crates/core/src/keccak.rs +++ b/crates/core/src/keccak.rs @@ -131,7 +131,7 @@ impl<'de> Deserialize<'de> for KeccakHash { impl<'de> de::Visitor<'de> for KeccakVisitor { type Value = KeccakHash; - fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "a string containing a keccak hash") } diff --git a/crates/core/src/key/common.rs b/crates/core/src/key/common.rs index a88132b010a..5a758f4b071 100644 --- a/crates/core/src/key/common.rs +++ b/crates/core/src/key/common.rs @@ -281,7 +281,7 @@ impl RefTo for SecretKey { } impl Display for SecretKey { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", HEXLOWER.encode(&self.serialize_to_vec())) } } diff --git a/crates/core/src/key/secp256k1.rs b/crates/core/src/key/secp256k1.rs index fbc878f05a5..b8534338825 100644 --- a/crates/core/src/key/secp256k1.rs +++ b/crates/core/src/key/secp256k1.rs @@ -327,6 +327,8 @@ impl Serialize for Signature { // TODO: implement the line below, currently cannot support [u8; 64] // serde::Serialize::serialize(&arr, serializer) + // There is no way the bytes len + 1 will overflow + #[allow(clippy::arithmetic_side_effects)] let mut seq = serializer.serialize_tuple(arr.len() + 1)?; for elem in &arr[..] { seq.serialize_element(elem)?; @@ -346,7 +348,10 @@ impl<'de> Deserialize<'de> for Signature { impl<'de> Visitor<'de> for ByteArrayVisitor { type Value = [u8; SIGNATURE_SIZE]; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting( + &self, + formatter: &mut fmt::Formatter<'_>, + ) -> fmt::Result { formatter.write_str(&format!( "an array of length {}", SIGNATURE_SIZE, @@ -472,7 +477,10 @@ impl Signature { (self.0.s(), v) }; let r = self.0.r(); - (r.to_bytes().into(), s.to_bytes().into(), v + Self::V_FIX) + // Cannot overflow as `v` is 0 or 1 + #[allow(clippy::arithmetic_side_effects)] + let v = v + Self::V_FIX; + (r.to_bytes().into(), s.to_bytes().into(), v) } } diff --git a/crates/core/src/masp.rs b/crates/core/src/masp.rs index cc1e2657d20..04ceb10d465 100644 --- a/crates/core/src/masp.rs +++ b/crates/core/src/masp.rs @@ -159,7 +159,7 @@ impl string_encoding::Format for PaymentAddress { fn to_bytes(&self) -> Vec { let mut bytes = Vec::with_capacity(PAYMENT_ADDRESS_SIZE); - bytes.push(self.is_pinned() as u8); + bytes.push(u8::from(self.is_pinned())); bytes.extend_from_slice(self.0.to_bytes().as_slice()); bytes } diff --git a/crates/core/src/time.rs b/crates/core/src/time.rs index 38f07ccaf61..f3225535900 100644 --- a/crates/core/src/time.rs +++ b/crates/core/src/time.rs @@ -15,6 +15,7 @@ use namada_migrations::*; use serde::{Deserialize, Serialize}; /// Check if the given `duration` has passed since the given `start. +#[allow(clippy::arithmetic_side_effects)] pub fn duration_passed( current: DateTimeUtc, start: DateTimeUtc, @@ -182,8 +183,9 @@ impl DateTimeUtc { } /// Returns the DateTimeUtc corresponding to one second in the future + #[allow(clippy::arithmetic_side_effects)] pub fn next_second(&self) -> Self { - *self + DurationSecs(0) + *self + DurationSecs(1) } } @@ -198,6 +200,7 @@ impl FromStr for DateTimeUtc { impl Add for DateTimeUtc { type Output = DateTimeUtc; + #[allow(clippy::arithmetic_side_effects)] fn add(self, duration: DurationSecs) -> Self::Output { let duration_std = std::time::Duration::from_secs(duration.0); let duration_chrono = Duration::from_std(duration_std).expect( @@ -211,6 +214,7 @@ impl Add for DateTimeUtc { impl Add for DateTimeUtc { type Output = DateTimeUtc; + #[allow(clippy::arithmetic_side_effects)] fn add(self, rhs: Duration) -> Self::Output { (self.0 + rhs).into() } @@ -219,6 +223,7 @@ impl Add for DateTimeUtc { impl Sub for DateTimeUtc { type Output = DateTimeUtc; + #[allow(clippy::arithmetic_side_effects)] fn sub(self, rhs: Duration) -> Self::Output { (self.0 - rhs).into() } @@ -283,6 +288,8 @@ impl TryFrom for DateTimeUtc { impl From for prost_types::Timestamp { fn from(dt: DateTimeUtc) -> Self { let seconds = dt.0.timestamp(); + // The cast cannot wrap as the value is at most 1_999_999_999 + #[allow(clippy::cast_possible_wrap)] let nanos = dt.0.timestamp_subsec_nanos() as i32; prost_types::Timestamp { seconds, nanos } }