Rollup merge of #94842 - tspiteri:there-is-no-try, r=Dylan-DPC

Remove unnecessary try_opt for operations that cannot fail

As indicated in the added comments, some operation cannot overflow, so using `try_opt!` for them is unnecessary.
This commit is contained in:
Dylan DPC 2022-03-11 13:38:39 +01:00 committed by GitHub
commit fb3d126458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 3 deletions

View File

@ -2166,7 +2166,8 @@ pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
let r = try_opt!(self.checked_rem(rhs)); let r = try_opt!(self.checked_rem(rhs));
let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) { let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
try_opt!(r.checked_add(rhs)) // r + rhs cannot overflow because they have opposite signs
r + rhs
} else { } else {
r r
}; };
@ -2174,7 +2175,8 @@ pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
if m == 0 { if m == 0 {
Some(self) Some(self)
} else { } else {
self.checked_add(try_opt!(rhs.checked_sub(m))) // rhs - m cannot overflow because m has the same sign as rhs
self.checked_add(rhs - m)
} }
} }

View File

@ -2119,7 +2119,8 @@ pub const fn next_multiple_of(self, rhs: Self) -> Self {
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> { pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
match try_opt!(self.checked_rem(rhs)) { match try_opt!(self.checked_rem(rhs)) {
0 => Some(self), 0 => Some(self),
r => self.checked_add(try_opt!(rhs.checked_sub(r))) // rhs - r cannot overflow because r is smaller than rhs
r => self.checked_add(rhs - r)
} }
} }