From 2d5955054c403f91a344b98969e06be3e86de5a9 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Thu, 9 May 2024 12:35:01 -0500 Subject: [PATCH] Address some lints, silence others --- src/fmt.rs | 10 +++++----- src/from_str.rs | 1 + src/lib.rs | 1 + src/serde.rs | 6 +++--- src/tests.rs | 12 ++++++------ src/tests_nostd.rs | 4 ++-- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/fmt.rs b/src/fmt.rs index 9e877ed..31a2335 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -246,11 +246,11 @@ impl SizeFormatter { y => { write!(fmt, "-")?; - // The absolute magnitude of T::min_value() for a signed number is one more than - // that of T::max_value(), meaning T::min_value().abs() will panic. + // The absolute magnitude of T::MIN for a signed number is one more than + // that of T::MAX, meaning T::MIN.abs() will panic. match y.checked_abs() { Some(abs) => abs as u64, - None => i64::max_value() as u64, + None => i64::MAX as u64, } } }; @@ -443,7 +443,7 @@ const BASE10_RULES: [FormatRule; 17] = [ unit: Unit::Petabyte, }, FormatRule { - less_than: u64::max_value(), + less_than: u64::MAX, formatter: |fmt, bytes| write!(fmt, "{:0}", bytes as f64 / (EXABYTE as f64)), unit: Unit::Exabyte, }, @@ -531,7 +531,7 @@ const BASE2_RULES: [FormatRule; 17] = [ unit: Unit::Pebibyte, }, FormatRule { - less_than: u64::max_value(), + less_than: u64::MAX, formatter: |fmt, bytes| write!(fmt, "{:0}", bytes as f64 / (EXBIBYTE as f64)), unit: Unit::Exbibyte, }, diff --git a/src/from_str.rs b/src/from_str.rs index 7c8f3d5..662526d 100644 --- a/src/from_str.rs +++ b/src/from_str.rs @@ -37,6 +37,7 @@ impl Size { /// let size = Size::from_str("12.34 KB").unwrap(); /// assert_eq!(size.bytes(), 12_340); /// ``` + #[allow(clippy::should_implement_trait)] pub fn from_str(s: &str) -> Result { FromStr::from_str(s) } diff --git a/src/lib.rs b/src/lib.rs index 3d019c5..bcd511b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(not(feature = "std"), allow(clippy::unnecessary_cast))] #![warn(missing_docs)] //! This crate provides an ergonomic, type-safe, and aesthetically-pleasing [`Size`] type that can diff --git a/src/serde.rs b/src/serde.rs index c67550e..43851ab 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -23,7 +23,7 @@ impl<'de> de::Visitor<'de> for SizeVisitor { where E: de::Error, { - if value > std::i64::MAX as u64 { + if value > i64::MAX as u64 { Err(E::custom(format!("u64 size {} is out of range", value))) } else { Ok(Size { @@ -36,7 +36,7 @@ impl<'de> de::Visitor<'de> for SizeVisitor { where E: de::Error, { - if value.is_infinite() || value > std::i64::MAX as f32 || value < std::i64::MIN as f32 { + if value.is_infinite() || value > i64::MAX as f32 || value < i64::MIN as f32 { Err(E::custom(format!("f32 size {} is out of range", value))) } else { Ok(Size { @@ -49,7 +49,7 @@ impl<'de> de::Visitor<'de> for SizeVisitor { where E: de::Error, { - if value.is_infinite() || value > std::i64::MAX as f64 || value < std::i64::MIN as f64 { + if value.is_infinite() || value > i64::MAX as f64 || value < i64::MIN as f64 { Err(E::custom(format!("f64 size {} is out of range", value))) } else { Ok(Size { diff --git a/src/tests.rs b/src/tests.rs index 8d18dba..70e9402 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -19,15 +19,15 @@ fn negative_tests() { #[test] fn integral_limits() { - assert_eq!("8 EiB", format!("{}", Size::from_bytes(i64::max_value()))); - assert_eq!("-8 EiB", format!("{}", Size::from_bytes(i64::min_value()))); + assert_eq!("8 EiB", format!("{}", Size::from_bytes(i64::MAX))); + assert_eq!("-8 EiB", format!("{}", Size::from_bytes(i64::MIN))); - assert_eq!("8 EiB", format!("{}", Size::from_kib(u64::max_value()))); - assert_eq!("0 bytes", format!("{}", Size::from_kib(u64::min_value()))); + assert_eq!("8 EiB", format!("{}", Size::from_kib(u64::MAX))); + assert_eq!("0 bytes", format!("{}", Size::from_kib(u64::MIN))); // Also test for the old-style API, which does no math at the point of creation - assert_eq!("8 EiB", format!("{}", Size::Bytes(u64::max_value()))); - assert_eq!("0 bytes", format!("{}", Size::Bytes(u64::min_value()))); + assert_eq!("8 EiB", format!("{}", Size::Bytes(u64::MAX))); + assert_eq!("0 bytes", format!("{}", Size::Bytes(u64::MIN))); } #[test] diff --git a/src/tests_nostd.rs b/src/tests_nostd.rs index b27f9b6..48338a8 100644 --- a/src/tests_nostd.rs +++ b/src/tests_nostd.rs @@ -37,8 +37,8 @@ fn nostd_bytes() { #[test] fn nostd_integral_limits() { // Test the old-style API, which does no math at the point of creation - assert_eq!(Size::from_bytes(i64::max_value()), Size::Bytes(u64::max_value())); - assert_eq!(Size::from_bytes(0), Size::Bytes(u64::min_value())); + assert_eq!(Size::from_bytes(i64::MAX), Size::Bytes(u64::MAX)); + assert_eq!(Size::from_bytes(0), Size::Bytes(u64::MIN)); assert_eq!(Size::from_bytes(i64::MAX), Size::Bytes(u64::MAX - 1)); }