Skip to content

Commit 1915d0e

Browse files
authored
Merge pull request CosmWasm#1678 from CosmWasm/chipshort/issue1609
Add #[must_use] to applicable math operations
2 parents 3651064 + fbba7f6 commit 1915d0e

File tree

9 files changed

+137
-33
lines changed

9 files changed

+137
-33
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ and this project adheres to
1010

1111
- cosmwasm-std: Implement `PartialEq` for `Addr == &Addr` and `&Addr == Addr` as
1212
well as `Event == &Event` and `&Event == Event` ([#1672]).
13+
- cosmwasm-std: Add `#[must_use]` annotations to `Uint64`, `Uint128`, `Uint256`,
14+
`Uint512`, `Decimal` and `Decimal256` math operations ([#1678])
1315

1416
[#1672]: https://github.com/CosmWasm/cosmwasm/pull/1672
17+
[#1678]: https://github.com/CosmWasm/cosmwasm/pull/1678
1518

1619
### Deprecated
1720

packages/std/src/math/decimal.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ impl Decimal {
158158
}
159159
}
160160

161+
#[must_use]
161162
pub const fn is_zero(&self) -> bool {
162163
self.0.is_zero()
163164
}
@@ -180,6 +181,7 @@ impl Decimal {
180181
/// assert_eq!(b.decimal_places(), 18);
181182
/// assert_eq!(b.atomics(), Uint128::new(1));
182183
/// ```
184+
#[must_use]
183185
#[inline]
184186
pub const fn atomics(&self) -> Uint128 {
185187
self.0
@@ -189,17 +191,20 @@ impl Decimal {
189191
/// but this could potentially change as the type evolves.
190192
///
191193
/// See also [`Decimal::atomics()`].
194+
#[must_use]
192195
#[inline]
193196
pub const fn decimal_places(&self) -> u32 {
194197
Self::DECIMAL_PLACES
195198
}
196199

197200
/// Rounds value down after decimal places.
201+
#[must_use = "this returns the result of the operation, without modifying the original"]
198202
pub fn floor(&self) -> Self {
199203
Self((self.0 / Self::DECIMAL_FRACTIONAL) * Self::DECIMAL_FRACTIONAL)
200204
}
201205

202206
/// Rounds value up after decimal places. Panics on overflow.
207+
#[must_use = "this returns the result of the operation, without modifying the original"]
203208
pub fn ceil(&self) -> Self {
204209
match self.checked_ceil() {
205210
Ok(value) => value,
@@ -248,6 +253,7 @@ impl Decimal {
248253
}
249254

250255
/// Raises a value to the power of `exp`, panics if an overflow occurred.
256+
#[must_use = "this returns the result of the operation, without modifying the original"]
251257
pub fn pow(self, exp: u32) -> Self {
252258
match self.checked_pow(exp) {
253259
Ok(value) => value,
@@ -302,6 +308,7 @@ impl Decimal {
302308
/// Returns the approximate square root as a Decimal.
303309
///
304310
/// This should not overflow or panic.
311+
#[must_use = "this returns the result of the operation, without modifying the original"]
305312
pub fn sqrt(&self) -> Self {
306313
// Algorithm described in https://hackmd.io/@webmaster128/SJThlukj_
307314
// We start with the highest precision possible and lower it until
@@ -321,6 +328,7 @@ impl Decimal {
321328
/// Precision *must* be a number between 0 and 9 (inclusive).
322329
///
323330
/// Returns `None` if the internal multiplication overflows.
331+
#[must_use = "this returns the result of the operation, without modifying the original"]
324332
fn sqrt_with_precision(&self, precision: u32) -> Option<Self> {
325333
let inner_mul = 100u128.pow(precision);
326334
self.0.checked_mul(inner_mul.into()).ok().map(|inner| {
@@ -329,31 +337,36 @@ impl Decimal {
329337
})
330338
}
331339

340+
#[must_use = "this returns the result of the operation, without modifying the original"]
332341
pub const fn abs_diff(self, other: Self) -> Self {
333342
Self(self.0.abs_diff(other.0))
334343
}
335344

345+
#[must_use = "this returns the result of the operation, without modifying the original"]
336346
pub fn saturating_add(self, other: Self) -> Self {
337347
match self.checked_add(other) {
338348
Ok(value) => value,
339349
Err(_) => Self::MAX,
340350
}
341351
}
342352

353+
#[must_use = "this returns the result of the operation, without modifying the original"]
343354
pub fn saturating_sub(self, other: Self) -> Self {
344355
match self.checked_sub(other) {
345356
Ok(value) => value,
346357
Err(_) => Self::zero(),
347358
}
348359
}
349360

361+
#[must_use = "this returns the result of the operation, without modifying the original"]
350362
pub fn saturating_mul(self, other: Self) -> Self {
351363
match self.checked_mul(other) {
352364
Ok(value) => value,
353365
Err(_) => Self::MAX,
354366
}
355367
}
356368

369+
#[must_use = "this returns the result of the operation, without modifying the original"]
357370
pub fn saturating_pow(self, exp: u32) -> Self {
358371
match self.checked_pow(exp) {
359372
Ok(value) => value,
@@ -379,6 +392,7 @@ impl Decimal {
379392
/// let d = Decimal::from_str("75.0").unwrap();
380393
/// assert_eq!(d.to_uint_floor(), Uint128::new(75));
381394
/// ```
395+
#[must_use]
382396
pub fn to_uint_floor(self) -> Uint128 {
383397
self.0 / Self::DECIMAL_FRACTIONAL
384398
}
@@ -401,6 +415,7 @@ impl Decimal {
401415
/// let d = Decimal::from_str("75.0").unwrap();
402416
/// assert_eq!(d.to_uint_ceil(), Uint128::new(75));
403417
/// ```
418+
#[must_use]
404419
pub fn to_uint_ceil(self) -> Uint128 {
405420
// Using `q = 1 + ((x - 1) / y); // if x != 0` with unsigned integers x, y, q
406421
// from https://stackoverflow.com/a/2745086/2013738. We know `x + y` CAN overflow.
@@ -1976,7 +1991,7 @@ mod tests {
19761991
#[test]
19771992
#[should_panic]
19781993
fn decimal_pow_overflow_panics() {
1979-
Decimal::MAX.pow(2u32);
1994+
_ = Decimal::MAX.pow(2u32);
19801995
}
19811996

19821997
#[test]

packages/std/src/math/decimal256.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ impl Decimal256 {
170170
}
171171
}
172172

173+
#[must_use]
173174
pub const fn is_zero(&self) -> bool {
174175
self.0.is_zero()
175176
}
@@ -192,6 +193,7 @@ impl Decimal256 {
192193
/// assert_eq!(b.decimal_places(), 18);
193194
/// assert_eq!(b.atomics(), Uint256::from(1u128));
194195
/// ```
196+
#[must_use]
195197
#[inline]
196198
pub const fn atomics(&self) -> Uint256 {
197199
self.0
@@ -201,17 +203,20 @@ impl Decimal256 {
201203
/// but this could potentially change as the type evolves.
202204
///
203205
/// See also [`Decimal256::atomics()`].
206+
#[must_use]
204207
#[inline]
205208
pub const fn decimal_places(&self) -> u32 {
206209
Self::DECIMAL_PLACES
207210
}
208211

209212
/// Rounds value down after decimal places.
213+
#[must_use = "this returns the result of the operation, without modifying the original"]
210214
pub fn floor(&self) -> Self {
211215
Self((self.0 / Self::DECIMAL_FRACTIONAL) * Self::DECIMAL_FRACTIONAL)
212216
}
213217

214218
/// Rounds value up after decimal places. Panics on overflow.
219+
#[must_use = "this returns the result of the operation, without modifying the original"]
215220
pub fn ceil(&self) -> Self {
216221
match self.checked_ceil() {
217222
Ok(value) => value,
@@ -260,6 +265,7 @@ impl Decimal256 {
260265
}
261266

262267
/// Raises a value to the power of `exp`, panics if an overflow occurred.
268+
#[must_use = "this returns the result of the operation, without modifying the original"]
263269
pub fn pow(self, exp: u32) -> Self {
264270
match self.checked_pow(exp) {
265271
Ok(value) => value,
@@ -314,6 +320,7 @@ impl Decimal256 {
314320
/// Returns the approximate square root as a Decimal256.
315321
///
316322
/// This should not overflow or panic.
323+
#[must_use = "this returns the result of the operation, without modifying the original"]
317324
pub fn sqrt(&self) -> Self {
318325
// Algorithm described in https://hackmd.io/@webmaster128/SJThlukj_
319326
// We start with the highest precision possible and lower it until
@@ -333,6 +340,7 @@ impl Decimal256 {
333340
/// Precision *must* be a number between 0 and 9 (inclusive).
334341
///
335342
/// Returns `None` if the internal multiplication overflows.
343+
#[must_use = "this returns the result of the operation, without modifying the original"]
336344
fn sqrt_with_precision(&self, precision: u32) -> Option<Self> {
337345
let inner_mul = Uint256::from(100u128).pow(precision);
338346
self.0.checked_mul(inner_mul).ok().map(|inner| {
@@ -341,6 +349,7 @@ impl Decimal256 {
341349
})
342350
}
343351

352+
#[must_use = "this returns the result of the operation, without modifying the original"]
344353
pub fn abs_diff(self, other: Self) -> Self {
345354
if self < other {
346355
other - self
@@ -349,27 +358,31 @@ impl Decimal256 {
349358
}
350359
}
351360

361+
#[must_use = "this returns the result of the operation, without modifying the original"]
352362
pub fn saturating_add(self, other: Self) -> Self {
353363
match self.checked_add(other) {
354364
Ok(value) => value,
355365
Err(_) => Self::MAX,
356366
}
357367
}
358368

369+
#[must_use = "this returns the result of the operation, without modifying the original"]
359370
pub fn saturating_sub(self, other: Self) -> Self {
360371
match self.checked_sub(other) {
361372
Ok(value) => value,
362373
Err(_) => Self::zero(),
363374
}
364375
}
365376

377+
#[must_use = "this returns the result of the operation, without modifying the original"]
366378
pub fn saturating_mul(self, other: Self) -> Self {
367379
match self.checked_mul(other) {
368380
Ok(value) => value,
369381
Err(_) => Self::MAX,
370382
}
371383
}
372384

385+
#[must_use = "this returns the result of the operation, without modifying the original"]
373386
pub fn saturating_pow(self, exp: u32) -> Self {
374387
match self.checked_pow(exp) {
375388
Ok(value) => value,
@@ -395,6 +408,7 @@ impl Decimal256 {
395408
/// let d = Decimal256::from_str("75.0").unwrap();
396409
/// assert_eq!(d.to_uint_floor(), Uint256::from(75u64));
397410
/// ```
411+
#[must_use]
398412
pub fn to_uint_floor(self) -> Uint256 {
399413
self.0 / Self::DECIMAL_FRACTIONAL
400414
}
@@ -417,6 +431,7 @@ impl Decimal256 {
417431
/// let d = Decimal256::from_str("75.0").unwrap();
418432
/// assert_eq!(d.to_uint_ceil(), Uint256::from(75u64));
419433
/// ```
434+
#[must_use]
420435
pub fn to_uint_ceil(self) -> Uint256 {
421436
// Using `q = 1 + ((x - 1) / y); // if x != 0` with unsigned integers x, y, q
422437
// from https://stackoverflow.com/a/2745086/2013738. We know `x + y` CAN overflow.
@@ -2125,7 +2140,7 @@ mod tests {
21252140
#[test]
21262141
#[should_panic]
21272142
fn decimal256_pow_overflow_panics() {
2128-
Decimal256::MAX.pow(2u32);
2143+
_ = Decimal256::MAX.pow(2u32);
21292144
}
21302145

21312146
#[test]

packages/std/src/math/fraction.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub trait Fraction<T>: Sized {
1010
/// Returns the multiplicative inverse `q/p` for fraction `p/q`.
1111
///
1212
/// If `p` is zero, None is returned.
13+
#[must_use = "this returns the result of the operation, without modifying the original"]
1314
fn inv(&self) -> Option<Self>;
1415
}
1516

@@ -58,6 +59,7 @@ macro_rules! impl_mul_fraction {
5859
}
5960

6061
/// Same operation as `checked_mul_floor` except unwrapped
62+
#[must_use = "this returns the result of the operation, without modifying the original"]
6163
pub fn mul_floor<F: Fraction<T>, T: Into<$Uint>>(self, rhs: F) -> Self {
6264
self.checked_mul_floor(rhs).unwrap()
6365
}
@@ -89,6 +91,7 @@ macro_rules! impl_mul_fraction {
8991
}
9092

9193
/// Same operation as `checked_mul_ceil` except unwrapped
94+
#[must_use = "this returns the result of the operation, without modifying the original"]
9295
pub fn mul_ceil<F: Fraction<T>, T: Into<$Uint>>(self, rhs: F) -> Self {
9396
self.checked_mul_ceil(rhs).unwrap()
9497
}
@@ -119,6 +122,7 @@ macro_rules! impl_mul_fraction {
119122
}
120123

121124
/// Same operation as `checked_div_floor` except unwrapped
125+
#[must_use = "this returns the result of the operation, without modifying the original"]
122126
pub fn div_floor<F: Fraction<T>, T: Into<$Uint>>(self, rhs: F) -> Self
123127
where
124128
Self: Sized,
@@ -156,6 +160,7 @@ macro_rules! impl_mul_fraction {
156160
}
157161

158162
/// Same operation as `checked_div_ceil` except unwrapped
163+
#[must_use = "this returns the result of the operation, without modifying the original"]
159164
pub fn div_ceil<F: Fraction<T>, T: Into<$Uint>>(self, rhs: F) -> Self
160165
where
161166
Self: Sized,

packages/std/src/math/isqrt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{Uint128, Uint256, Uint512, Uint64};
66
/// [integer square root](https://en.wikipedia.org/wiki/Integer_square_root).
77
pub trait Isqrt {
88
/// The [integer square root](https://en.wikipedia.org/wiki/Integer_square_root).
9+
#[must_use = "this returns the result of the operation, without modifying the original"]
910
fn isqrt(self) -> Self;
1011
}
1112

0 commit comments

Comments
 (0)