diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs index b9f794431fb18..451a2e14fe950 100644 --- a/library/core/src/num/int_macros.rs +++ b/library/core/src/num/int_macros.rs @@ -451,7 +451,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_add(self, rhs: Self) -> Option { let (a, b) = self.overflowing_add(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict integer addition. Computes `self + rhs`, panicking @@ -461,7 +461,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -484,7 +484,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_add(self, rhs: Self) -> Self { let (a, b) = self.overflowing_add(rhs); - if unlikely!(b) {overflow_panic::add()} else {a} + if unlikely!(b) { overflow_panic::add() } else { a } } /// Unchecked integer addition. Computes `self + rhs`, assuming overflow @@ -531,7 +531,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option { let (a, b) = self.overflowing_add_unsigned(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict addition with an unsigned integer. Computes `self + rhs`, @@ -541,7 +541,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -564,7 +564,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self { let (a, b) = self.overflowing_add_unsigned(rhs); - if unlikely!(b) {overflow_panic::add()} else {a} + if unlikely!(b) { overflow_panic::add() } else { a } } /// Checked integer subtraction. Computes `self - rhs`, returning `None` if @@ -585,7 +585,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_sub(self, rhs: Self) -> Option { let (a, b) = self.overflowing_sub(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict integer subtraction. Computes `self - rhs`, panicking if @@ -595,7 +595,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -618,7 +618,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_sub(self, rhs: Self) -> Self { let (a, b) = self.overflowing_sub(rhs); - if unlikely!(b) {overflow_panic::sub()} else {a} + if unlikely!(b) { overflow_panic::sub() } else { a } } /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow @@ -665,7 +665,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option { let (a, b) = self.overflowing_sub_unsigned(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict subtraction with an unsigned integer. Computes `self - rhs`, @@ -675,7 +675,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -698,7 +698,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self { let (a, b) = self.overflowing_sub_unsigned(rhs); - if unlikely!(b) {overflow_panic::sub()} else {a} + if unlikely!(b) { overflow_panic::sub() } else { a } } /// Checked integer multiplication. Computes `self * rhs`, returning `None` if @@ -719,7 +719,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_mul(self, rhs: Self) -> Option { let (a, b) = self.overflowing_mul(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict integer multiplication. Computes `self * rhs`, panicking if @@ -729,7 +729,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -752,7 +752,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_mul(self, rhs: Self) -> Self { let (a, b) = self.overflowing_mul(rhs); - if unlikely!(b) {overflow_panic::mul()} else {a} + if unlikely!(b) { overflow_panic::mul() } else { a } } /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow @@ -816,7 +816,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// The only case where such an overflow can occur is when one divides `MIN / -1` on a signed type (where /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value @@ -848,7 +848,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_div(self, rhs: Self) -> Self { let (a, b) = self.overflowing_div(rhs); - if unlikely!(b) {overflow_panic::div()} else {a} + if unlikely!(b) { overflow_panic::div() } else { a } } /// Checked Euclidean division. Computes `self.div_euclid(rhs)`, @@ -886,7 +886,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// The only case where such an overflow can occur is when one divides `MIN / -1` on a signed type (where /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value @@ -918,7 +918,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_div_euclid(self, rhs: Self) -> Self { let (a, b) = self.overflowing_div_euclid(rhs); - if unlikely!(b) {overflow_panic::div()} else {a} + if unlikely!(b) { overflow_panic::div() } else { a } } /// Checked integer remainder. Computes `self % rhs`, returning `None` if @@ -956,7 +956,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// The only case where such an overflow can occur is `x % y` for `MIN / -1` on a /// signed type (where `MIN` is the negative minimal value), which is invalid due to implementation artifacts. @@ -987,7 +987,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_rem(self, rhs: Self) -> Self { let (a, b) = self.overflowing_rem(rhs); - if unlikely!(b) {overflow_panic::rem()} else {a} + if unlikely!(b) { overflow_panic::rem() } else { a } } /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None` @@ -1025,7 +1025,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// The only case where such an overflow can occur is `x % y` for `MIN / -1` on a /// signed type (where `MIN` is the negative minimal value), which is invalid due to implementation artifacts. @@ -1056,7 +1056,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_rem_euclid(self, rhs: Self) -> Self { let (a, b) = self.overflowing_rem_euclid(rhs); - if unlikely!(b) {overflow_panic::rem()} else {a} + if unlikely!(b) { overflow_panic::rem() } else { a } } /// Checked negation. Computes `-self`, returning `None` if `self == MIN`. @@ -1076,7 +1076,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_neg(self) -> Option { let (a, b) = self.overflowing_neg(); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Unchecked negation. Computes `-self`, assuming overflow cannot occur. @@ -1110,7 +1110,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -1133,7 +1133,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_neg(self) -> Self { let (a, b) = self.overflowing_neg(); - if unlikely!(b) {overflow_panic::neg()} else {a} + if unlikely!(b) { overflow_panic::neg() } else { a } } /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger @@ -1154,7 +1154,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_shl(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shl(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict shift left. Computes `self << rhs`, panicking if `rhs` is larger @@ -1164,7 +1164,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -1187,7 +1187,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_shl(self, rhs: u32) -> Self { let (a, b) = self.overflowing_shl(rhs); - if unlikely!(b) {overflow_panic::shl()} else {a} + if unlikely!(b) { overflow_panic::shl() } else { a } } /// Unchecked shift left. Computes `self << rhs`, assuming that @@ -1235,7 +1235,7 @@ macro_rules! int_impl { #[inline] pub const fn checked_shr(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shr(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict shift right. Computes `self >> rhs`, panicking `rhs` is @@ -1245,7 +1245,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -1268,7 +1268,7 @@ macro_rules! int_impl { #[track_caller] pub const fn strict_shr(self, rhs: u32) -> Self { let (a, b) = self.overflowing_shr(rhs); - if unlikely!(b) {overflow_panic::shr()} else {a} + if unlikely!(b) { overflow_panic::shr() } else { a } } /// Unchecked shift right. Computes `self >> rhs`, assuming that @@ -1329,7 +1329,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -1403,7 +1403,7 @@ macro_rules! int_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// diff --git a/library/core/src/num/overflow_panic.rs b/library/core/src/num/overflow_panic.rs index b941fdb212a84..203037ffb43ea 100644 --- a/library/core/src/num/overflow_panic.rs +++ b/library/core/src/num/overflow_panic.rs @@ -1,3 +1,7 @@ +//! Functions for panicking on overflow. +//! +//! In particular, these are used by the `strict_` methods on integers. + #[cold] #[track_caller] pub const fn add() -> ! { diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 8d9713856057e..9915ce385d943 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -459,7 +459,7 @@ macro_rules! uint_impl { #[inline] pub const fn checked_add(self, rhs: Self) -> Option { let (a, b) = self.overflowing_add(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict integer addition. Computes `self + rhs`, panicking @@ -469,7 +469,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -492,8 +492,8 @@ macro_rules! uint_impl { #[track_caller] pub const fn strict_add(self, rhs: Self) -> Self { let (a, b) = self.overflowing_add(rhs); - if unlikely!(b) {overflow_panic::add()} else {a} - } + if unlikely!(b) { overflow_panic ::add()} else {a} + } /// Unchecked integer addition. Computes `self + rhs`, assuming overflow /// cannot occur. @@ -540,7 +540,7 @@ macro_rules! uint_impl { #[inline] pub const fn checked_add_signed(self, rhs: $SignedT) -> Option { let (a, b) = self.overflowing_add_signed(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict addition with a signed integer. Computes `self + rhs`, @@ -550,7 +550,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -578,8 +578,8 @@ macro_rules! uint_impl { #[track_caller] pub const fn strict_add_signed(self, rhs: $SignedT) -> Self { let (a, b) = self.overflowing_add_signed(rhs); - if unlikely!(b) {overflow_panic::add()} else {a} - } + if unlikely!(b) { overflow_panic ::add()} else {a} + } /// Checked integer subtraction. Computes `self - rhs`, returning /// `None` if overflow occurred. @@ -599,7 +599,7 @@ macro_rules! uint_impl { #[inline] pub const fn checked_sub(self, rhs: Self) -> Option { let (a, b) = self.overflowing_sub(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict integer subtraction. Computes `self - rhs`, panicking if @@ -609,7 +609,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -632,8 +632,8 @@ macro_rules! uint_impl { #[track_caller] pub const fn strict_sub(self, rhs: Self) -> Self { let (a, b) = self.overflowing_sub(rhs); - if unlikely!(b) {overflow_panic::sub()} else {a} - } + if unlikely!(b) { overflow_panic ::sub()} else {a} + } /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow /// cannot occur. @@ -679,7 +679,7 @@ macro_rules! uint_impl { #[inline] pub const fn checked_mul(self, rhs: Self) -> Option { let (a, b) = self.overflowing_mul(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict integer multiplication. Computes `self * rhs`, panicking if @@ -689,7 +689,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -712,8 +712,8 @@ macro_rules! uint_impl { #[track_caller] pub const fn strict_mul(self, rhs: Self) -> Self { let (a, b) = self.overflowing_mul(rhs); - if unlikely!(b) {overflow_panic::mul()} else {a} - } + if unlikely!(b) { overflow_panic ::mul()} else {a} + } /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow /// cannot occur. @@ -1147,7 +1147,7 @@ macro_rules! uint_impl { #[inline] pub const fn checked_neg(self) -> Option { let (a, b) = self.overflowing_neg(); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict negation. Computes `-self`, panicking unless `self == @@ -1159,7 +1159,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -1182,7 +1182,7 @@ macro_rules! uint_impl { #[track_caller] pub const fn strict_neg(self) -> Self { let (a, b) = self.overflowing_neg(); - if unlikely!(b) {overflow_panic::neg()} else {a} + if unlikely!(b) { overflow_panic::neg() } else { a } } /// Checked shift left. Computes `self << rhs`, returning `None` @@ -1203,7 +1203,7 @@ macro_rules! uint_impl { #[inline] pub const fn checked_shl(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shl(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict shift left. Computes `self << rhs`, panicking if `rhs` is larger @@ -1213,7 +1213,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -1236,7 +1236,7 @@ macro_rules! uint_impl { #[track_caller] pub const fn strict_shl(self, rhs: u32) -> Self { let (a, b) = self.overflowing_shl(rhs); - if unlikely!(b) {overflow_panic::shl()} else {a} + if unlikely!(b) { overflow_panic::shl() } else { a } } /// Unchecked shift left. Computes `self << rhs`, assuming that @@ -1284,7 +1284,7 @@ macro_rules! uint_impl { #[inline] pub const fn checked_shr(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shr(rhs); - if unlikely!(b) {None} else {Some(a)} + if unlikely!(b) { None } else { Some(a) } } /// Strict shift right. Computes `self >> rhs`, panicking `rhs` is @@ -1294,7 +1294,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples /// @@ -1317,7 +1317,7 @@ macro_rules! uint_impl { #[track_caller] pub const fn strict_shr(self, rhs: u32) -> Self { let (a, b) = self.overflowing_shr(rhs); - if unlikely!(b) {overflow_panic::shr()} else {a} + if unlikely!(b) { overflow_panic::shr() } else { a } } /// Unchecked shift right. Computes `self >> rhs`, assuming that @@ -1393,7 +1393,7 @@ macro_rules! uint_impl { /// /// ## Overflow behavior /// - /// This function will always panic on overflow, regardless of if overflow checks are enabled. + /// This function will always panic on overflow, regardless of whether overflow checks are enabled. /// /// # Examples ///