Skip to content

Commit

Permalink
core: clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Apr 19, 2024
1 parent 91d4699 commit aafb5d1
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 - {}",
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/eth_bridge_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/ethereum_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl From<u64> for Uint {
impl Add<u64> for Uint {
type Output = Self;

#[allow(clippy::arithmetic_side_effects)]
fn add(self, rhs: u64) -> Self::Output {
(ethUint(self.0) + rhs).into()
}
Expand All @@ -121,6 +122,7 @@ impl Add<u64> for Uint {
impl Sub<u64> for Uint {
type Output = Self;

#[allow(clippy::arithmetic_side_effects)]
fn sub(self, rhs: u64) -> Self::Output {
(ethUint(self.0) - rhs).into()
}
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/ethereum_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/key/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl RefTo<PublicKey> 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()))
}
}
Expand Down
12 changes: 10 additions & 2 deletions crates/core/src/key/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl string_encoding::Format for PaymentAddress {

fn to_bytes(&self) -> Vec<u8> {
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
}
Expand Down
9 changes: 8 additions & 1 deletion crates/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -198,6 +200,7 @@ impl FromStr for DateTimeUtc {
impl Add<DurationSecs> 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(
Expand All @@ -211,6 +214,7 @@ impl Add<DurationSecs> for DateTimeUtc {
impl Add<Duration> for DateTimeUtc {
type Output = DateTimeUtc;

#[allow(clippy::arithmetic_side_effects)]
fn add(self, rhs: Duration) -> Self::Output {
(self.0 + rhs).into()
}
Expand All @@ -219,6 +223,7 @@ impl Add<Duration> for DateTimeUtc {
impl Sub<Duration> for DateTimeUtc {
type Output = DateTimeUtc;

#[allow(clippy::arithmetic_side_effects)]
fn sub(self, rhs: Duration) -> Self::Output {
(self.0 - rhs).into()
}
Expand Down Expand Up @@ -283,6 +288,8 @@ impl TryFrom<prost_types::Timestamp> for DateTimeUtc {
impl From<DateTimeUtc> 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 }
}
Expand Down

0 comments on commit aafb5d1

Please sign in to comment.