Skip to content

Remove unnecessary try_opt for operations that cannot fail #94842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2166,15 +2166,17 @@ macro_rules! int_impl {

let r = try_opt!(self.checked_rem(rhs));
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 {
r
};

if m == 0 {
Some(self)
} 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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2119,7 +2119,8 @@ macro_rules! uint_impl {
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
match try_opt!(self.checked_rem(rhs)) {
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)
}
}

Expand Down