Skip to content
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

Document panics in Add/Sub impls and use expect #1316

Merged
merged 7 commits into from
Sep 26, 2023
Next Next commit
Use expect instead of unwrap in Add and Sub impls
  • Loading branch information
pitdicker committed Sep 25, 2023
commit de2c31928fd56e05c06ac2733b8bbd5452639d44
14 changes: 8 additions & 6 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,8 @@ impl<Tz: TimeZone> Add<FixedOffset> for DateTime<Tz> {

#[inline]
fn add(mut self, rhs: FixedOffset) -> DateTime<Tz> {
self.datetime = self.naive_utc().checked_add_offset(rhs).unwrap();
self.datetime =
self.naive_utc().checked_add_offset(rhs).expect("`DateTime + FixedOffset` overflowed");
self
}
}
Expand All @@ -1261,7 +1262,7 @@ impl<Tz: TimeZone> Add<Months> for DateTime<Tz> {
type Output = DateTime<Tz>;

fn add(self, rhs: Months) -> Self::Output {
self.checked_add_months(rhs).unwrap()
self.checked_add_months(rhs).expect("`DateTime + Months` out of range")
}
}

Expand Down Expand Up @@ -1309,7 +1310,8 @@ impl<Tz: TimeZone> Sub<FixedOffset> for DateTime<Tz> {

#[inline]
fn sub(mut self, rhs: FixedOffset) -> DateTime<Tz> {
self.datetime = self.naive_utc().checked_sub_offset(rhs).unwrap();
self.datetime =
self.naive_utc().checked_sub_offset(rhs).expect("`DateTime - FixedOffset` overflowed");
self
}
}
Expand All @@ -1318,7 +1320,7 @@ impl<Tz: TimeZone> Sub<Months> for DateTime<Tz> {
type Output = DateTime<Tz>;

fn sub(self, rhs: Months) -> Self::Output {
self.checked_sub_months(rhs).unwrap()
self.checked_sub_months(rhs).expect("`DateTime - Months` out of range")
}
}

Expand All @@ -1344,15 +1346,15 @@ impl<Tz: TimeZone> Add<Days> for DateTime<Tz> {
type Output = DateTime<Tz>;

fn add(self, days: Days) -> Self::Output {
self.checked_add_days(days).unwrap()
self.checked_add_days(days).expect("`DateTime + Days` out of range")
}
}

impl<Tz: TimeZone> Sub<Days> for DateTime<Tz> {
type Output = DateTime<Tz>;

fn sub(self, days: Days) -> Self::Output {
self.checked_sub_days(days).unwrap()
self.checked_sub_days(days).expect("`DateTime - Days` out of range")
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,7 @@ impl Add<Months> for NaiveDate {
/// assert_eq!(from_ymd(2020, 1, 31) + Months::new(1), from_ymd(2020, 2, 29));
/// ```
fn add(self, months: Months) -> Self::Output {
self.checked_add_months(months).unwrap()
self.checked_add_months(months).expect("`NaiveDate + Months` out of range")
}
}

Expand All @@ -1914,23 +1914,23 @@ impl Sub<Months> for NaiveDate {
/// assert_eq!(from_ymd(2014, 1, 1) - Months::new(13), from_ymd(2012, 12, 1));
/// ```
fn sub(self, months: Months) -> Self::Output {
self.checked_sub_months(months).unwrap()
self.checked_sub_months(months).expect("`NaiveDate - Months` out of range")
}
}

impl Add<Days> for NaiveDate {
type Output = NaiveDate;

fn add(self, days: Days) -> Self::Output {
self.checked_add_days(days).unwrap()
self.checked_add_days(days).expect("`NaiveDate + Days` out of range")
}
}

impl Sub<Days> for NaiveDate {
type Output = NaiveDate;

fn sub(self, days: Days) -> Self::Output {
self.checked_sub_days(days).unwrap()
self.checked_sub_days(days).expect("`NaiveDate - Days` out of range")
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ impl Add<FixedOffset> for NaiveDateTime {

#[inline]
fn add(self, rhs: FixedOffset) -> NaiveDateTime {
self.checked_add_offset(rhs).unwrap()
self.checked_add_offset(rhs).expect("`NaiveDateTime + FixedOffset` out of range")
}
}

Expand Down Expand Up @@ -1668,7 +1668,7 @@ impl Add<Months> for NaiveDateTime {
/// );
/// ```
fn add(self, rhs: Months) -> Self::Output {
Self { date: self.date.checked_add_months(rhs).unwrap(), time: self.time }
self.checked_add_months(rhs).expect("`NaiveDateTime + Months` out of range")
}
}

Expand Down Expand Up @@ -1760,7 +1760,7 @@ impl Sub<FixedOffset> for NaiveDateTime {

#[inline]
fn sub(self, rhs: FixedOffset) -> NaiveDateTime {
self.checked_sub_offset(rhs).unwrap()
self.checked_sub_offset(rhs).expect("`NaiveDateTime - FixedOffset` out of range")
}
}

Expand Down Expand Up @@ -1792,7 +1792,7 @@ impl Sub<Months> for NaiveDateTime {
type Output = NaiveDateTime;

fn sub(self, rhs: Months) -> Self::Output {
Self { date: self.date.checked_sub_months(rhs).unwrap(), time: self.time }
self.checked_sub_months(rhs).expect("`NaiveDateTime - Months` out of range")
}
}

Expand Down Expand Up @@ -1848,15 +1848,15 @@ impl Add<Days> for NaiveDateTime {
type Output = NaiveDateTime;

fn add(self, days: Days) -> Self::Output {
self.checked_add_days(days).unwrap()
self.checked_add_days(days).expect("`NaiveDateTime + Days` out of range")
}
}

impl Sub<Days> for NaiveDateTime {
type Output = NaiveDateTime;

fn sub(self, days: Days) -> Self::Output {
self.checked_sub_days(days).unwrap()
self.checked_sub_days(days).expect("`NaiveDateTime - Days` out of range")
}
}

Expand Down