diff --git a/CHANGELOG.md b/CHANGELOG.md index 841e0376ef..02e633ea32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,11 @@ and this project adheres to - cosmwasm-std: Implement `PartialEq` for `Addr == &Addr` and `&Addr == Addr` as well as `Event == &Event` and `&Event == Event` ([#1672]). +- cosmwasm-std: Add `#[must_use]` annotations to `Uint64`, `Uint128`, `Uint256`, + `Uint512`, `Decimal` and `Decimal256` math operations ([#1678]) [#1672]: https://github.com/CosmWasm/cosmwasm/pull/1672 +[#1678]: https://github.com/CosmWasm/cosmwasm/pull/1678 ### Deprecated diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index e39edc3e3e..3c3842128c 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -158,6 +158,7 @@ impl Decimal { } } + #[must_use] pub const fn is_zero(&self) -> bool { self.0.is_zero() } @@ -180,6 +181,7 @@ impl Decimal { /// assert_eq!(b.decimal_places(), 18); /// assert_eq!(b.atomics(), Uint128::new(1)); /// ``` + #[must_use] #[inline] pub const fn atomics(&self) -> Uint128 { self.0 @@ -189,17 +191,20 @@ impl Decimal { /// but this could potentially change as the type evolves. /// /// See also [`Decimal::atomics()`]. + #[must_use] #[inline] pub const fn decimal_places(&self) -> u32 { Self::DECIMAL_PLACES } /// Rounds value down after decimal places. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn floor(&self) -> Self { Self((self.0 / Self::DECIMAL_FRACTIONAL) * Self::DECIMAL_FRACTIONAL) } /// Rounds value up after decimal places. Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn ceil(&self) -> Self { match self.checked_ceil() { Ok(value) => value, @@ -248,6 +253,7 @@ impl Decimal { } /// Raises a value to the power of `exp`, panics if an overflow occurred. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn pow(self, exp: u32) -> Self { match self.checked_pow(exp) { Ok(value) => value, @@ -302,6 +308,7 @@ impl Decimal { /// Returns the approximate square root as a Decimal. /// /// This should not overflow or panic. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn sqrt(&self) -> Self { // Algorithm described in https://hackmd.io/@webmaster128/SJThlukj_ // We start with the highest precision possible and lower it until @@ -321,6 +328,7 @@ impl Decimal { /// Precision *must* be a number between 0 and 9 (inclusive). /// /// Returns `None` if the internal multiplication overflows. + #[must_use = "this returns the result of the operation, without modifying the original"] fn sqrt_with_precision(&self, precision: u32) -> Option { let inner_mul = 100u128.pow(precision); self.0.checked_mul(inner_mul.into()).ok().map(|inner| { @@ -329,10 +337,12 @@ impl Decimal { }) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs_diff(self, other: Self) -> Self { Self(self.0.abs_diff(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_add(self, other: Self) -> Self { match self.checked_add(other) { Ok(value) => value, @@ -340,6 +350,7 @@ impl Decimal { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_sub(self, other: Self) -> Self { match self.checked_sub(other) { Ok(value) => value, @@ -347,6 +358,7 @@ impl Decimal { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_mul(self, other: Self) -> Self { match self.checked_mul(other) { Ok(value) => value, @@ -354,6 +366,7 @@ impl Decimal { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_pow(self, exp: u32) -> Self { match self.checked_pow(exp) { Ok(value) => value, @@ -379,6 +392,7 @@ impl Decimal { /// let d = Decimal::from_str("75.0").unwrap(); /// assert_eq!(d.to_uint_floor(), Uint128::new(75)); /// ``` + #[must_use] pub fn to_uint_floor(self) -> Uint128 { self.0 / Self::DECIMAL_FRACTIONAL } @@ -401,6 +415,7 @@ impl Decimal { /// let d = Decimal::from_str("75.0").unwrap(); /// assert_eq!(d.to_uint_ceil(), Uint128::new(75)); /// ``` + #[must_use] pub fn to_uint_ceil(self) -> Uint128 { // Using `q = 1 + ((x - 1) / y); // if x != 0` with unsigned integers x, y, q // from https://stackoverflow.com/a/2745086/2013738. We know `x + y` CAN overflow. @@ -1976,7 +1991,7 @@ mod tests { #[test] #[should_panic] fn decimal_pow_overflow_panics() { - Decimal::MAX.pow(2u32); + _ = Decimal::MAX.pow(2u32); } #[test] diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index eaea9b39b3..bd6109718a 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -170,6 +170,7 @@ impl Decimal256 { } } + #[must_use] pub const fn is_zero(&self) -> bool { self.0.is_zero() } @@ -192,6 +193,7 @@ impl Decimal256 { /// assert_eq!(b.decimal_places(), 18); /// assert_eq!(b.atomics(), Uint256::from(1u128)); /// ``` + #[must_use] #[inline] pub const fn atomics(&self) -> Uint256 { self.0 @@ -201,17 +203,20 @@ impl Decimal256 { /// but this could potentially change as the type evolves. /// /// See also [`Decimal256::atomics()`]. + #[must_use] #[inline] pub const fn decimal_places(&self) -> u32 { Self::DECIMAL_PLACES } /// Rounds value down after decimal places. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn floor(&self) -> Self { Self((self.0 / Self::DECIMAL_FRACTIONAL) * Self::DECIMAL_FRACTIONAL) } /// Rounds value up after decimal places. Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn ceil(&self) -> Self { match self.checked_ceil() { Ok(value) => value, @@ -260,6 +265,7 @@ impl Decimal256 { } /// Raises a value to the power of `exp`, panics if an overflow occurred. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn pow(self, exp: u32) -> Self { match self.checked_pow(exp) { Ok(value) => value, @@ -314,6 +320,7 @@ impl Decimal256 { /// Returns the approximate square root as a Decimal256. /// /// This should not overflow or panic. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn sqrt(&self) -> Self { // Algorithm described in https://hackmd.io/@webmaster128/SJThlukj_ // We start with the highest precision possible and lower it until @@ -333,6 +340,7 @@ impl Decimal256 { /// Precision *must* be a number between 0 and 9 (inclusive). /// /// Returns `None` if the internal multiplication overflows. + #[must_use = "this returns the result of the operation, without modifying the original"] fn sqrt_with_precision(&self, precision: u32) -> Option { let inner_mul = Uint256::from(100u128).pow(precision); self.0.checked_mul(inner_mul).ok().map(|inner| { @@ -341,6 +349,7 @@ impl Decimal256 { }) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn abs_diff(self, other: Self) -> Self { if self < other { other - self @@ -349,6 +358,7 @@ impl Decimal256 { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_add(self, other: Self) -> Self { match self.checked_add(other) { Ok(value) => value, @@ -356,6 +366,7 @@ impl Decimal256 { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_sub(self, other: Self) -> Self { match self.checked_sub(other) { Ok(value) => value, @@ -363,6 +374,7 @@ impl Decimal256 { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_mul(self, other: Self) -> Self { match self.checked_mul(other) { Ok(value) => value, @@ -370,6 +382,7 @@ impl Decimal256 { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_pow(self, exp: u32) -> Self { match self.checked_pow(exp) { Ok(value) => value, @@ -395,6 +408,7 @@ impl Decimal256 { /// let d = Decimal256::from_str("75.0").unwrap(); /// assert_eq!(d.to_uint_floor(), Uint256::from(75u64)); /// ``` + #[must_use] pub fn to_uint_floor(self) -> Uint256 { self.0 / Self::DECIMAL_FRACTIONAL } @@ -417,6 +431,7 @@ impl Decimal256 { /// let d = Decimal256::from_str("75.0").unwrap(); /// assert_eq!(d.to_uint_ceil(), Uint256::from(75u64)); /// ``` + #[must_use] pub fn to_uint_ceil(self) -> Uint256 { // Using `q = 1 + ((x - 1) / y); // if x != 0` with unsigned integers x, y, q // from https://stackoverflow.com/a/2745086/2013738. We know `x + y` CAN overflow. @@ -2125,7 +2140,7 @@ mod tests { #[test] #[should_panic] fn decimal256_pow_overflow_panics() { - Decimal256::MAX.pow(2u32); + _ = Decimal256::MAX.pow(2u32); } #[test] diff --git a/packages/std/src/math/fraction.rs b/packages/std/src/math/fraction.rs index 0056f4a0a0..c13c746182 100644 --- a/packages/std/src/math/fraction.rs +++ b/packages/std/src/math/fraction.rs @@ -10,6 +10,7 @@ pub trait Fraction: Sized { /// Returns the multiplicative inverse `q/p` for fraction `p/q`. /// /// If `p` is zero, None is returned. + #[must_use = "this returns the result of the operation, without modifying the original"] fn inv(&self) -> Option; } @@ -58,6 +59,7 @@ macro_rules! impl_mul_fraction { } /// Same operation as `checked_mul_floor` except unwrapped + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn mul_floor, T: Into<$Uint>>(self, rhs: F) -> Self { self.checked_mul_floor(rhs).unwrap() } @@ -89,6 +91,7 @@ macro_rules! impl_mul_fraction { } /// Same operation as `checked_mul_ceil` except unwrapped + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn mul_ceil, T: Into<$Uint>>(self, rhs: F) -> Self { self.checked_mul_ceil(rhs).unwrap() } @@ -119,6 +122,7 @@ macro_rules! impl_mul_fraction { } /// Same operation as `checked_div_floor` except unwrapped + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn div_floor, T: Into<$Uint>>(self, rhs: F) -> Self where Self: Sized, @@ -156,6 +160,7 @@ macro_rules! impl_mul_fraction { } /// Same operation as `checked_div_ceil` except unwrapped + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn div_ceil, T: Into<$Uint>>(self, rhs: F) -> Self where Self: Sized, diff --git a/packages/std/src/math/isqrt.rs b/packages/std/src/math/isqrt.rs index 6bf4b01f64..6e44ce1695 100644 --- a/packages/std/src/math/isqrt.rs +++ b/packages/std/src/math/isqrt.rs @@ -6,6 +6,7 @@ use crate::{Uint128, Uint256, Uint512, Uint64}; /// [integer square root](https://en.wikipedia.org/wiki/Integer_square_root). pub trait Isqrt { /// The [integer square root](https://en.wikipedia.org/wiki/Integer_square_root). + #[must_use = "this returns the result of the operation, without modifying the original"] fn isqrt(self) -> Self; } diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 713c7609a9..319ea92525 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -69,19 +69,23 @@ impl Uint128 { } /// Returns a copy of the number as big endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_be_bytes(self) -> [u8; 16] { self.0.to_be_bytes() } /// Returns a copy of the number as little endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_le_bytes(self) -> [u8; 16] { self.0.to_le_bytes() } + #[must_use] pub const fn is_zero(&self) -> bool { self.0 == 0 } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn pow(self, exp: u32) -> Self { self.0.pow(exp).into() } @@ -90,6 +94,7 @@ impl Uint128 { /// /// Due to the nature of the integer division involved, the result is always floored. /// E.g. 5 * 99/100 = 4. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn multiply_ratio, B: Into>( &self, numerator: A, @@ -136,6 +141,7 @@ impl Uint128 { /// let result = a.full_mul(2u32); /// assert_eq!(result.to_string(), "680564733841876926926749214863536422910"); /// ``` + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn full_mul(self, rhs: impl Into) -> Uint256 { Uint256::from(self.u128()) .checked_mul(Uint256::from(rhs.into())) @@ -191,42 +197,51 @@ impl Uint128 { .ok_or_else(|| DivideByZeroError::new(self)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_add(self, other: Self) -> Self { Self(self.0.wrapping_add(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_sub(self, other: Self) -> Self { Self(self.0.wrapping_sub(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_mul(self, other: Self) -> Self { Self(self.0.wrapping_mul(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_pow(self, other: u32) -> Self { Self(self.0.wrapping_pow(other)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_add(self, other: Self) -> Self { Self(self.0.saturating_add(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_sub(self, other: Self) -> Self { Self(self.0.saturating_sub(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_mul(self, other: Self) -> Self { Self(self.0.saturating_mul(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_pow(self, exp: u32) -> Self { Self(self.0.saturating_pow(exp)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs_diff(self, other: Self) -> Self { Self(if self.0 < other.0 { other.0 - self.0 @@ -813,7 +828,7 @@ mod tests { #[test] #[should_panic] fn uint128_pow_overflow_panics() { - Uint128::MAX.pow(2u32); + _ = Uint128::MAX.pow(2u32); } #[test] @@ -859,7 +874,7 @@ mod tests { #[test] #[should_panic(expected = "Denominator must not be zero")] fn uint128_multiply_ratio_panics_for_zero_denominator() { - Uint128(500).multiply_ratio(1u128, 0u128); + _ = Uint128(500).multiply_ratio(1u128, 0u128); } #[test] @@ -1087,7 +1102,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn mul_floor_panics_on_overflow() { let fraction = (21u128, 8u128); - Uint128::MAX.mul_floor(fraction); + _ = Uint128::MAX.mul_floor(fraction); } #[test] @@ -1107,7 +1122,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn mul_floor_panics_on_zero_div() { let fraction = (21u128, 0u128); - Uint128::new(123456).mul_floor(fraction); + _ = Uint128::new(123456).mul_floor(fraction); } #[test] @@ -1170,7 +1185,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn mul_ceil_panics_on_overflow() { let fraction = (21u128, 8u128); - Uint128::MAX.mul_ceil(fraction); + _ = Uint128::MAX.mul_ceil(fraction); } #[test] @@ -1190,7 +1205,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn mul_ceil_panics_on_zero_div() { let fraction = (21u128, 0u128); - Uint128::new(123456).mul_ceil(fraction); + _ = Uint128::new(123456).mul_ceil(fraction); } #[test] @@ -1208,7 +1223,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn div_floor_raises_with_zero() { let fraction = (Uint128::zero(), Uint128::new(21)); - Uint128::new(123456).div_floor(fraction); + _ = Uint128::new(123456).div_floor(fraction); } #[test] @@ -1259,7 +1274,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn div_floor_panics_on_overflow() { let fraction = (8u128, 21u128); - Uint128::MAX.div_floor(fraction); + _ = Uint128::MAX.div_floor(fraction); } #[test] @@ -1279,7 +1294,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn div_ceil_raises_with_zero() { let fraction = (Uint128::zero(), Uint128::new(21)); - Uint128::new(123456).div_ceil(fraction); + _ = Uint128::new(123456).div_ceil(fraction); } #[test] @@ -1330,7 +1345,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn div_ceil_panics_on_overflow() { let fraction = (8u128, 21u128); - Uint128::MAX.div_ceil(fraction); + _ = Uint128::MAX.div_ceil(fraction); } #[test] diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 2aa51db8d4..201342e387 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -79,6 +79,7 @@ impl Uint256 { ]) } + #[must_use] pub const fn from_be_bytes(data: [u8; 32]) -> Self { let words: [u64; 4] = [ u64::from_le_bytes([ @@ -97,6 +98,7 @@ impl Uint256 { Self(U256(words)) } + #[must_use] pub const fn from_le_bytes(data: [u8; 32]) -> Self { let words: [u64; 4] = [ u64::from_le_bytes([ @@ -117,6 +119,7 @@ impl Uint256 { /// A conversion from `u128` that, unlike the one provided by the `From` trait, /// can be used in a `const` context. + #[must_use] pub const fn from_u128(num: u128) -> Self { let bytes = num.to_le_bytes(); @@ -129,11 +132,13 @@ impl Uint256 { /// A conversion from `Uint128` that, unlike the one provided by the `From` trait, /// can be used in a `const` context. + #[must_use] pub const fn from_uint128(num: Uint128) -> Self { Self::from_u128(num.u128()) } /// Returns a copy of the number as big endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_be_bytes(self) -> [u8; 32] { let words = [ (self.0).0[3].to_be_bytes(), @@ -145,6 +150,7 @@ impl Uint256 { } /// Returns a copy of the number as little endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_le_bytes(self) -> [u8; 32] { let words = [ (self.0).0[0].to_le_bytes(), @@ -155,11 +161,13 @@ impl Uint256 { unsafe { std::mem::transmute::<[[u8; 8]; 4], [u8; 32]>(words) } } + #[must_use] pub const fn is_zero(&self) -> bool { let words = (self.0).0; words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn pow(self, exp: u32) -> Self { let res = self.0.pow(exp.into()); Self(res) @@ -169,6 +177,7 @@ impl Uint256 { /// /// Due to the nature of the integer division involved, the result is always floored. /// E.g. 5 * 99/100 = 4. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn multiply_ratio, B: Into>( &self, numerator: A, @@ -218,6 +227,7 @@ impl Uint256 { /// "231584178474632390847141970017375815706539969331281128078915168015826259279870", /// ); /// ``` + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn full_mul(self, rhs: impl Into) -> Uint512 { Uint512::from(self) .checked_mul(Uint512::from(rhs.into())) @@ -286,42 +296,50 @@ impl Uint256 { Ok(Self(self.0.shl(other))) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_add(self, other: Self) -> Self { let (value, _did_overflow) = self.0.overflowing_add(other.0); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_sub(self, other: Self) -> Self { let (value, _did_overflow) = self.0.overflowing_sub(other.0); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_mul(self, other: Self) -> Self { let (value, _did_overflow) = self.0.overflowing_mul(other.0); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_pow(self, other: u32) -> Self { let (value, _did_overflow) = self.0.overflowing_pow(other.into()); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_add(self, other: Self) -> Self { Self(self.0.saturating_add(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_sub(self, other: Self) -> Self { Self(self.0.saturating_sub(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_mul(self, other: Self) -> Self { Self(self.0.saturating_mul(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_pow(self, exp: u32) -> Self { match self.checked_pow(exp) { Ok(value) => value, @@ -329,6 +347,7 @@ impl Uint256 { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn abs_diff(self, other: Self) -> Self { if self < other { other - self @@ -1389,7 +1408,7 @@ mod tests { #[test] #[should_panic] fn uint256_pow_overflow_panics() { - Uint256::MAX.pow(2u32); + _ = Uint256::MAX.pow(2u32); } #[test] @@ -1441,7 +1460,7 @@ mod tests { #[test] #[should_panic(expected = "Denominator must not be zero")] fn uint256_multiply_ratio_panics_for_zero_denominator() { - Uint256::from(500u32).multiply_ratio(1u128, 0u128); + _ = Uint256::from(500u32).multiply_ratio(1u128, 0u128); } #[test] @@ -1717,7 +1736,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn mul_floor_panics_on_overflow() { let fraction = (21u128, 8u128); - Uint256::MAX.mul_floor(fraction); + _ = Uint256::MAX.mul_floor(fraction); } #[test] @@ -1739,7 +1758,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn mul_floor_panics_on_zero_div() { let fraction = (21u128, 0u128); - Uint256::from(123456u32).mul_floor(fraction); + _ = Uint256::from(123456u32).mul_floor(fraction); } #[test] @@ -1812,7 +1831,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn mul_ceil_panics_on_overflow() { let fraction = (21u128, 8u128); - Uint256::MAX.mul_ceil(fraction); + _ = Uint256::MAX.mul_ceil(fraction); } #[test] @@ -1834,7 +1853,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn mul_ceil_panics_on_zero_div() { let fraction = (21u128, 0u128); - Uint256::from(123456u32).mul_ceil(fraction); + _ = Uint256::from(123456u32).mul_ceil(fraction); } #[test] @@ -1852,7 +1871,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn div_floor_raises_with_zero() { let fraction = (Uint256::zero(), Uint256::from(21u32)); - Uint256::from(123456u128).div_floor(fraction); + _ = Uint256::from(123456u128).div_floor(fraction); } #[test] @@ -1906,7 +1925,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn div_floor_panics_on_overflow() { let fraction = (8u128, 21u128); - Uint256::MAX.div_floor(fraction); + _ = Uint256::MAX.div_floor(fraction); } #[test] @@ -1928,7 +1947,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn div_ceil_raises_with_zero() { let fraction = (Uint256::zero(), Uint256::from(21u128)); - Uint256::from(123456u128).div_ceil(fraction); + _ = Uint256::from(123456u128).div_ceil(fraction); } #[test] @@ -1982,7 +2001,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn div_ceil_panics_on_overflow() { let fraction = (8u128, 21u128); - Uint256::MAX.div_ceil(fraction); + _ = Uint256::MAX.div_ceil(fraction); } #[test] diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index c75c83d5f8..73a0512f26 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -80,6 +80,7 @@ impl Uint512 { ]) } + #[must_use] pub const fn from_be_bytes(data: [u8; 64]) -> Self { let words: [u64; 8] = [ u64::from_le_bytes([ @@ -110,6 +111,7 @@ impl Uint512 { Self(U512(words)) } + #[must_use] pub const fn from_le_bytes(data: [u8; 64]) -> Self { let words: [u64; 8] = [ u64::from_le_bytes([ @@ -142,6 +144,7 @@ impl Uint512 { /// A conversion from `Uint256` that, unlike the one provided by the `From` trait, /// can be used in a `const` context. + #[must_use] pub const fn from_uint256(num: Uint256) -> Self { let bytes = num.to_le_bytes(); Self::from_le_bytes([ @@ -155,6 +158,7 @@ impl Uint512 { } /// Returns a copy of the number as big endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_be_bytes(self) -> [u8; 64] { let words = [ (self.0).0[7].to_be_bytes(), @@ -170,6 +174,7 @@ impl Uint512 { } /// Returns a copy of the number as little endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_le_bytes(self) -> [u8; 64] { let words = [ (self.0).0[0].to_le_bytes(), @@ -184,6 +189,7 @@ impl Uint512 { unsafe { std::mem::transmute::<[[u8; 8]; 8], [u8; 64]>(words) } } + #[must_use] pub const fn is_zero(&self) -> bool { let words = (self.0).0; words[0] == 0 @@ -196,6 +202,7 @@ impl Uint512 { && words[7] == 0 } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn pow(self, exp: u32) -> Self { let res = self.0.pow(exp.into()); Self(res) @@ -255,42 +262,50 @@ impl Uint512 { Ok(Self(self.0.shr(other))) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_add(self, other: Self) -> Self { let (value, _did_overflow) = self.0.overflowing_add(other.0); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_sub(self, other: Self) -> Self { let (value, _did_overflow) = self.0.overflowing_sub(other.0); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_mul(self, other: Self) -> Self { let (value, _did_overflow) = self.0.overflowing_mul(other.0); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_pow(self, other: u32) -> Self { let (value, _did_overflow) = self.0.overflowing_pow(other.into()); Self(value) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_add(self, other: Self) -> Self { Self(self.0.saturating_add(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_sub(self, other: Self) -> Self { Self(self.0.saturating_sub(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_mul(self, other: Self) -> Self { Self(self.0.saturating_mul(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_pow(self, exp: u32) -> Self { match self.checked_pow(exp) { Ok(value) => value, @@ -298,6 +313,7 @@ impl Uint512 { } } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn abs_diff(self, other: Self) -> Self { if self < other { other - self @@ -1075,7 +1091,7 @@ mod tests { #[test] #[should_panic] fn uint512_pow_overflow_panics() { - Uint512::MAX.pow(2u32); + _ = Uint512::MAX.pow(2u32); } #[test] diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index e7f500ae3c..e7a50339c0 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -62,19 +62,23 @@ impl Uint64 { } /// Returns a copy of the number as big endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_be_bytes(self) -> [u8; 8] { self.0.to_be_bytes() } /// Returns a copy of the number as little endian bytes. + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn to_le_bytes(self) -> [u8; 8] { self.0.to_le_bytes() } + #[must_use] pub const fn is_zero(&self) -> bool { self.0 == 0 } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn pow(self, exp: u32) -> Self { self.0.pow(exp).into() } @@ -83,6 +87,7 @@ impl Uint64 { /// /// Due to the nature of the integer division involved, the result is always floored. /// E.g. 5 * 99/100 = 4. + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn multiply_ratio, B: Into>( &self, numerator: A, @@ -129,6 +134,7 @@ impl Uint64 { /// let result = a.full_mul(2u32); /// assert_eq!(result.to_string(), "36893488147419103230"); /// ``` + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn full_mul(self, rhs: impl Into) -> Uint128 { Uint128::from(self.u64()) .checked_mul(Uint128::from(rhs.into())) @@ -184,42 +190,51 @@ impl Uint64 { .ok_or_else(|| DivideByZeroError::new(self)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_add(self, other: Self) -> Self { Self(self.0.wrapping_add(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_sub(self, other: Self) -> Self { Self(self.0.wrapping_sub(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_mul(self, other: Self) -> Self { Self(self.0.wrapping_mul(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub fn wrapping_pow(self, other: u32) -> Self { Self(self.0.wrapping_pow(other)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_add(self, other: Self) -> Self { Self(self.0.saturating_add(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_sub(self, other: Self) -> Self { Self(self.0.saturating_sub(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_mul(self, other: Self) -> Self { Self(self.0.saturating_mul(other.0)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub fn saturating_pow(self, exp: u32) -> Self { Self(self.0.saturating_pow(exp)) } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs_diff(self, other: Self) -> Self { Self(if self.0 < other.0 { other.0 - self.0 @@ -716,7 +731,7 @@ mod tests { #[test] #[should_panic] fn uint64_pow_overflow_panics() { - Uint64::MAX.pow(2u32); + _ = Uint64::MAX.pow(2u32); } #[test] @@ -770,7 +785,7 @@ mod tests { #[test] #[should_panic(expected = "Denominator must not be zero")] fn uint64_multiply_ratio_panics_for_zero_denominator() { - Uint64(500).multiply_ratio(1u64, 0u64); + _ = Uint64(500).multiply_ratio(1u64, 0u64); } #[test] @@ -989,7 +1004,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn mul_floor_panics_on_overflow() { let fraction = (21u64, 8u64); - Uint64::MAX.mul_floor(fraction); + _ = Uint64::MAX.mul_floor(fraction); } #[test] @@ -1009,7 +1024,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn mul_floor_panics_on_zero_div() { let fraction = (21u64, 0u64); - Uint64::new(123456).mul_floor(fraction); + _ = Uint64::new(123456).mul_floor(fraction); } #[test] @@ -1062,7 +1077,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn mul_ceil_panics_on_overflow() { let fraction = (21u64, 8u64); - Uint64::MAX.mul_ceil(fraction); + _ = Uint64::MAX.mul_ceil(fraction); } #[test] @@ -1082,7 +1097,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn mul_ceil_panics_on_zero_div() { let fraction = (21u64, 0u64); - Uint64::new(123456).mul_ceil(fraction); + _ = Uint64::new(123456).mul_ceil(fraction); } #[test] @@ -1100,7 +1115,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn div_floor_raises_with_zero() { let fraction = (Uint64::zero(), Uint64::new(21)); - Uint64::new(123456).div_floor(fraction); + _ = Uint64::new(123456).div_floor(fraction); } #[test] @@ -1135,7 +1150,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn div_floor_panics_on_overflow() { let fraction = (8u64, 21u64); - Uint64::MAX.div_floor(fraction); + _ = Uint64::MAX.div_floor(fraction); } #[test] @@ -1155,7 +1170,7 @@ mod tests { #[should_panic(expected = "DivideByZeroError")] fn div_ceil_raises_with_zero() { let fraction = (Uint64::zero(), Uint64::new(21)); - Uint64::new(123456).div_ceil(fraction); + _ = Uint64::new(123456).div_ceil(fraction); } #[test] @@ -1190,7 +1205,7 @@ mod tests { #[should_panic(expected = "ConversionOverflowError")] fn div_ceil_panics_on_overflow() { let fraction = (8u64, 21u64); - Uint64::MAX.div_ceil(fraction); + _ = Uint64::MAX.div_ceil(fraction); } #[test]