From 0941f68efcd7dfe66124ad0c1934277f47da9090 Mon Sep 17 00:00:00 2001 From: hinto-janai Date: Mon, 2 Sep 2024 17:46:11 -0400 Subject: [PATCH] helper: fix clippy (#265) * helper: fix lints * fix tests --- helper/src/cast.rs | 16 +++++++++------- helper/src/map.rs | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/helper/src/cast.rs b/helper/src/cast.rs index 81d0836..99b7f53 100644 --- a/helper/src/cast.rs +++ b/helper/src/cast.rs @@ -4,14 +4,16 @@ //! //! `#[no_std]` compatible. +#![allow(clippy::cast_possible_truncation)] + #[rustfmt::skip] //============================ SAFETY: DO NOT REMOVE ===========================// // // // // -// Only allow building 64-bit targets. // -// This allows us to assume 64-bit invariants in this file. // +// Only allow building 64-bit targets. // +// This allows us to assume 64-bit invariants in this file. // #[cfg(not(target_pointer_width = "64"))] - compile_error!("Cuprate is only compatible with 64-bit CPUs"); + compile_error!("Cuprate is only compatible with 64-bit CPUs"); // // // // //============================ SAFETY: DO NOT REMOVE ===========================// @@ -60,8 +62,8 @@ mod test { #[test] fn max_unsigned() { - assert_eq!(u32_to_usize(u32::MAX), u32::MAX as usize); - assert_eq!(usize_to_u64(u32_to_usize(u32::MAX)), u32::MAX as u64); + assert_eq!(u32_to_usize(u32::MAX), usize::try_from(u32::MAX).unwrap()); + assert_eq!(usize_to_u64(u32_to_usize(u32::MAX)), u64::from(u32::MAX)); assert_eq!(u64_to_usize(u64::MAX), usize::MAX); assert_eq!(usize_to_u64(u64_to_usize(u64::MAX)), u64::MAX); @@ -72,8 +74,8 @@ mod test { #[test] fn max_signed() { - assert_eq!(i32_to_isize(i32::MAX), i32::MAX as isize); - assert_eq!(isize_to_i64(i32_to_isize(i32::MAX)), i32::MAX as i64); + assert_eq!(i32_to_isize(i32::MAX), isize::try_from(i32::MAX).unwrap()); + assert_eq!(isize_to_i64(i32_to_isize(i32::MAX)), i64::from(i32::MAX)); assert_eq!(i64_to_isize(i64::MAX), isize::MAX); assert_eq!(isize_to_i64(i64_to_isize(i64::MAX)), i64::MAX); diff --git a/helper/src/map.rs b/helper/src/map.rs index ea6dfc4..7805ea6 100644 --- a/helper/src/map.rs +++ b/helper/src/map.rs @@ -76,7 +76,7 @@ pub const fn combine_low_high_bits_to_u128(low_bits: u64, high_bits: u64) -> u12 /// assert_eq!(u64_to_timelock(499_999_999), Timelock::Block(499_999_999)); /// assert_eq!(u64_to_timelock(500_000_000), Timelock::Time(500_000_000)); /// ``` -pub fn u64_to_timelock(u: u64) -> Timelock { +pub const fn u64_to_timelock(u: u64) -> Timelock { if u == 0 { Timelock::None } else if u < 500_000_000 { @@ -97,7 +97,7 @@ pub fn u64_to_timelock(u: u64) -> Timelock { /// assert_eq!(timelock_to_u64(Timelock::Block(499_999_999)), 499_999_999); /// assert_eq!(timelock_to_u64(Timelock::Time(500_000_000)), 500_000_000); /// ``` -pub fn timelock_to_u64(timelock: Timelock) -> u64 { +pub const fn timelock_to_u64(timelock: Timelock) -> u64 { match timelock { Timelock::None => 0, Timelock::Block(u) => usize_to_u64(u),