Skip to content

Commit ed10356

Browse files
committed
remove unnecessary try_opt for operations that cannot fail
1 parent f58d51b commit ed10356

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

library/core/src/num/int_macros.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2166,15 +2166,17 @@ macro_rules! int_impl {
21662166

21672167
let r = try_opt!(self.checked_rem(rhs));
21682168
let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2169-
try_opt!(r.checked_add(rhs))
2169+
// r + rhs cannot overflow because they have opposite signs
2170+
r + rhs
21702171
} else {
21712172
r
21722173
};
21732174

21742175
if m == 0 {
21752176
Some(self)
21762177
} else {
2177-
self.checked_add(try_opt!(rhs.checked_sub(m)))
2178+
// rhs - m cannot overflow because m has the same sign as rhs
2179+
self.checked_add(rhs - m)
21782180
}
21792181
}
21802182

library/core/src/num/uint_macros.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,8 @@ macro_rules! uint_impl {
21192119
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
21202120
match try_opt!(self.checked_rem(rhs)) {
21212121
0 => Some(self),
2122-
r => self.checked_add(try_opt!(rhs.checked_sub(r)))
2122+
// rhs - r cannot overflow because r is smaller than rhs
2123+
r => self.checked_add(rhs - r)
21232124
}
21242125
}
21252126

0 commit comments

Comments
 (0)