Skip to content

Commit

Permalink
reword panic comments and add spaces to unlikely branches
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehri01 committed Nov 28, 2023
1 parent 8c7a5b0 commit 6d17169
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 60 deletions.
68 changes: 34 additions & 34 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
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
Expand All @@ -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
///
Expand All @@ -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
Expand Down Expand Up @@ -531,7 +531,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
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`,
Expand All @@ -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
///
Expand All @@ -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
Expand All @@ -585,7 +585,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
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
Expand All @@ -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
///
Expand All @@ -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
Expand Down Expand Up @@ -665,7 +665,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
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`,
Expand All @@ -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
///
Expand All @@ -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
Expand All @@ -719,7 +719,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
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
Expand All @@ -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
///
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)`,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
Expand All @@ -1076,7 +1076,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_neg(self) -> Option<Self> {
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.
Expand Down Expand Up @@ -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
///
Expand All @@ -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
Expand All @@ -1154,7 +1154,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
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
Expand All @@ -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
///
Expand All @@ -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
Expand Down Expand Up @@ -1235,7 +1235,7 @@ macro_rules! int_impl {
#[inline]
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
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
Expand All @@ -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
///
Expand All @@ -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
Expand Down Expand Up @@ -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
///
Expand Down Expand Up @@ -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
///
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/num/overflow_panic.rs
Original file line number Diff line number Diff line change
@@ -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() -> ! {
Expand Down
Loading

0 comments on commit 6d17169

Please sign in to comment.