Skip to content

Commit e5a3600

Browse files
committed
side-step potentially panic'ing negate in fn abs.
1 parent d9e0a57 commit e5a3600

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libcore/num/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,12 @@ macro_rules! int_impl {
12611261
#[stable(feature = "rust1", since = "1.0.0")]
12621262
#[inline]
12631263
pub fn abs(self) -> $T {
1264-
if self.is_negative() { -self } else { self }
1264+
if self.is_negative() {
1265+
// FIXME (#24420) use wrapping_neg instead after it lands.
1266+
(!self).wrapping_add(1)
1267+
} else {
1268+
self
1269+
}
12651270
}
12661271

12671272
/// Returns a number representing sign of `self`.

0 commit comments

Comments
 (0)