Skip to content

Commit

Permalink
Add #[must_use] to some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas committed Mar 31, 2023
1 parent 2fcdd9e commit b3f129b
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/naive/date.rs
Expand Up @@ -469,6 +469,7 @@ impl NaiveDate {
///
/// Returns `None` if `n` out-of-range; ie. if `n` is larger than the number of `weekday` in
/// `month` (eg. the 6th Friday of March 2017), or if `n == 0`.
#[must_use]
pub fn from_weekday_of_month_opt(
year: i32,
month: u32,
Expand Down Expand Up @@ -549,6 +550,7 @@ impl NaiveDate {
/// Some(NaiveDate::from_ymd_opt(2022, 9, 30).unwrap())
/// );
/// ```
#[must_use]
pub fn checked_add_months(self, months: Months) -> Option<Self> {
if months.0 == 0 {
return Some(self);
Expand Down Expand Up @@ -579,6 +581,7 @@ impl NaiveDate {
/// None
/// );
/// ```
#[must_use]
pub fn checked_sub_months(self, months: Months) -> Option<Self> {
if months.0 == 0 {
return Some(self);
Expand All @@ -591,6 +594,7 @@ impl NaiveDate {
}
}

#[must_use]
fn diff_months(self, months: i32) -> Option<Self> {
let (years, left) = ((months / 12), (months % 12));

Expand Down Expand Up @@ -652,6 +656,7 @@ impl NaiveDate {
/// None
/// );
/// ```
#[must_use]
pub fn checked_add_days(self, days: Days) -> Option<Self> {
if days.0 == 0 {
return Some(self);
Expand All @@ -675,6 +680,7 @@ impl NaiveDate {
/// None
/// );
/// ```
#[must_use]
pub fn checked_sub_days(self, days: Days) -> Option<Self> {
if days.0 == 0 {
return Some(self);
Expand All @@ -683,6 +689,7 @@ impl NaiveDate {
i64::try_from(days.0).ok().and_then(|d| self.diff_days(-d))
}

#[must_use]
fn diff_days(self, days: i64) -> Option<Self> {
let secs = days.checked_mul(86400)?; // 86400 seconds in one day
if secs >= core::i64::MAX / 1000 || secs <= core::i64::MIN / 1000 {
Expand All @@ -706,6 +713,7 @@ impl NaiveDate {
/// assert_eq!(dt.time(), t);
/// ```
#[inline]
#[must_use]
pub const fn and_time(&self, time: NaiveTime) -> NaiveDateTime {
NaiveDateTime::new(*self, time)
}
Expand Down

0 comments on commit b3f129b

Please sign in to comment.