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 6c17832
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
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 @@ -526,6 +527,7 @@ impl NaiveDate {
/// # let parse_from_str = NaiveDate::parse_from_str;
/// assert!(parse_from_str("Sat, 09 Aug 2013", "%a, %d %b %Y").is_err());
/// ```
#[must_use]
pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveDate> {
let mut parsed = Parsed::new();
parse(&mut parsed, s, StrftimeItems::new(fmt))?;
Expand All @@ -549,6 +551,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 +582,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 +595,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 +657,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 +681,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 +690,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 +714,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 6c17832

Please sign in to comment.