Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea832c5

Browse files
svix-jplattedjc
authored andcommitted
Add track_caller to non-deprecated functions
… that can panic because of invalid user input.
1 parent cfae889 commit ea832c5

6 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/date.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,15 @@ impl<Tz: TimeZone> Add<TimeDelta> for Date<Tz> {
497497
type Output = Date<Tz>;
498498

499499
#[inline]
500+
#[track_caller]
500501
fn add(self, rhs: TimeDelta) -> Date<Tz> {
501502
self.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed")
502503
}
503504
}
504505

505506
impl<Tz: TimeZone> AddAssign<TimeDelta> for Date<Tz> {
506507
#[inline]
508+
#[track_caller]
507509
fn add_assign(&mut self, rhs: TimeDelta) {
508510
self.date = self.date.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed");
509511
}
@@ -513,13 +515,15 @@ impl<Tz: TimeZone> Sub<TimeDelta> for Date<Tz> {
513515
type Output = Date<Tz>;
514516

515517
#[inline]
518+
#[track_caller]
516519
fn sub(self, rhs: TimeDelta) -> Date<Tz> {
517520
self.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed")
518521
}
519522
}
520523

521524
impl<Tz: TimeZone> SubAssign<TimeDelta> for Date<Tz> {
522525
#[inline]
526+
#[track_caller]
523527
fn sub_assign(&mut self, rhs: TimeDelta) {
524528
self.date = self.date.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed");
525529
}

src/datetime/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
620620
/// can not have more than 4 digits.
621621
#[cfg(feature = "alloc")]
622622
#[must_use]
623+
#[track_caller]
623624
pub fn to_rfc2822(&self) -> String {
624625
let mut result = String::with_capacity(32);
625626
write_rfc2822(&mut result, self.overflowing_naive_local(), self.offset.fix())
@@ -1529,6 +1530,7 @@ impl<Tz: TimeZone> Add<TimeDelta> for DateTime<Tz> {
15291530
type Output = DateTime<Tz>;
15301531

15311532
#[inline]
1533+
#[track_caller]
15321534
fn add(self, rhs: TimeDelta) -> DateTime<Tz> {
15331535
self.checked_add_signed(rhs).expect("`DateTime + TimeDelta` overflowed")
15341536
}
@@ -1548,6 +1550,7 @@ impl<Tz: TimeZone> Add<Duration> for DateTime<Tz> {
15481550
type Output = DateTime<Tz>;
15491551

15501552
#[inline]
1553+
#[track_caller]
15511554
fn add(self, rhs: Duration) -> DateTime<Tz> {
15521555
let rhs = TimeDelta::from_std(rhs)
15531556
.expect("overflow converting from core::time::Duration to TimeDelta");
@@ -1567,6 +1570,7 @@ impl<Tz: TimeZone> Add<Duration> for DateTime<Tz> {
15671570
/// Consider using [`DateTime<Tz>::checked_add_signed`] to get an `Option` instead.
15681571
impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz> {
15691572
#[inline]
1573+
#[track_caller]
15701574
fn add_assign(&mut self, rhs: TimeDelta) {
15711575
let datetime =
15721576
self.datetime.checked_add_signed(rhs).expect("`DateTime + TimeDelta` overflowed");
@@ -1587,6 +1591,7 @@ impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz> {
15871591
/// Consider using [`DateTime<Tz>::checked_add_signed`] to get an `Option` instead.
15881592
impl<Tz: TimeZone> AddAssign<Duration> for DateTime<Tz> {
15891593
#[inline]
1594+
#[track_caller]
15901595
fn add_assign(&mut self, rhs: Duration) {
15911596
let rhs = TimeDelta::from_std(rhs)
15921597
.expect("overflow converting from core::time::Duration to TimeDelta");
@@ -1603,6 +1608,7 @@ impl<Tz: TimeZone> Add<FixedOffset> for DateTime<Tz> {
16031608
type Output = DateTime<Tz>;
16041609

16051610
#[inline]
1611+
#[track_caller]
16061612
fn add(mut self, rhs: FixedOffset) -> DateTime<Tz> {
16071613
self.datetime =
16081614
self.naive_utc().checked_add_offset(rhs).expect("`DateTime + FixedOffset` overflowed");
@@ -1626,6 +1632,7 @@ impl<Tz: TimeZone> Add<FixedOffset> for DateTime<Tz> {
16261632
impl<Tz: TimeZone> Add<Months> for DateTime<Tz> {
16271633
type Output = DateTime<Tz>;
16281634

1635+
#[track_caller]
16291636
fn add(self, rhs: Months) -> Self::Output {
16301637
self.checked_add_months(rhs).expect("`DateTime + Months` out of range")
16311638
}
@@ -1647,6 +1654,7 @@ impl<Tz: TimeZone> Sub<TimeDelta> for DateTime<Tz> {
16471654
type Output = DateTime<Tz>;
16481655

16491656
#[inline]
1657+
#[track_caller]
16501658
fn sub(self, rhs: TimeDelta) -> DateTime<Tz> {
16511659
self.checked_sub_signed(rhs).expect("`DateTime - TimeDelta` overflowed")
16521660
}
@@ -1666,6 +1674,7 @@ impl<Tz: TimeZone> Sub<Duration> for DateTime<Tz> {
16661674
type Output = DateTime<Tz>;
16671675

16681676
#[inline]
1677+
#[track_caller]
16691678
fn sub(self, rhs: Duration) -> DateTime<Tz> {
16701679
let rhs = TimeDelta::from_std(rhs)
16711680
.expect("overflow converting from core::time::Duration to TimeDelta");
@@ -1687,6 +1696,7 @@ impl<Tz: TimeZone> Sub<Duration> for DateTime<Tz> {
16871696
/// Consider using [`DateTime<Tz>::checked_sub_signed`] to get an `Option` instead.
16881697
impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz> {
16891698
#[inline]
1699+
#[track_caller]
16901700
fn sub_assign(&mut self, rhs: TimeDelta) {
16911701
let datetime =
16921702
self.datetime.checked_sub_signed(rhs).expect("`DateTime - TimeDelta` overflowed");
@@ -1707,6 +1717,7 @@ impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz> {
17071717
/// Consider using [`DateTime<Tz>::checked_sub_signed`] to get an `Option` instead.
17081718
impl<Tz: TimeZone> SubAssign<Duration> for DateTime<Tz> {
17091719
#[inline]
1720+
#[track_caller]
17101721
fn sub_assign(&mut self, rhs: Duration) {
17111722
let rhs = TimeDelta::from_std(rhs)
17121723
.expect("overflow converting from core::time::Duration to TimeDelta");
@@ -1723,6 +1734,7 @@ impl<Tz: TimeZone> Sub<FixedOffset> for DateTime<Tz> {
17231734
type Output = DateTime<Tz>;
17241735

17251736
#[inline]
1737+
#[track_caller]
17261738
fn sub(mut self, rhs: FixedOffset) -> DateTime<Tz> {
17271739
self.datetime =
17281740
self.naive_utc().checked_sub_offset(rhs).expect("`DateTime - FixedOffset` overflowed");
@@ -1746,6 +1758,7 @@ impl<Tz: TimeZone> Sub<FixedOffset> for DateTime<Tz> {
17461758
impl<Tz: TimeZone> Sub<Months> for DateTime<Tz> {
17471759
type Output = DateTime<Tz>;
17481760

1761+
#[track_caller]
17491762
fn sub(self, rhs: Months) -> Self::Output {
17501763
self.checked_sub_months(rhs).expect("`DateTime - Months` out of range")
17511764
}
@@ -1782,6 +1795,7 @@ impl<Tz: TimeZone> Sub<&DateTime<Tz>> for DateTime<Tz> {
17821795
impl<Tz: TimeZone> Add<Days> for DateTime<Tz> {
17831796
type Output = DateTime<Tz>;
17841797

1798+
#[track_caller]
17851799
fn add(self, days: Days) -> Self::Output {
17861800
self.checked_add_days(days).expect("`DateTime + Days` out of range")
17871801
}
@@ -1800,6 +1814,7 @@ impl<Tz: TimeZone> Add<Days> for DateTime<Tz> {
18001814
impl<Tz: TimeZone> Sub<Days> for DateTime<Tz> {
18011815
type Output = DateTime<Tz>;
18021816

1817+
#[track_caller]
18031818
fn sub(self, days: Days) -> Self::Output {
18041819
self.checked_sub_days(days).expect("`DateTime - Days` out of range")
18051820
}

src/naive/date/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,7 @@ impl Add<TimeDelta> for NaiveDate {
19821982
type Output = NaiveDate;
19831983

19841984
#[inline]
1985+
#[track_caller]
19851986
fn add(self, rhs: TimeDelta) -> NaiveDate {
19861987
self.checked_add_signed(rhs).expect("`NaiveDate + TimeDelta` overflowed")
19871988
}
@@ -1998,6 +1999,7 @@ impl Add<TimeDelta> for NaiveDate {
19981999
/// Consider using [`NaiveDate::checked_add_signed`] to get an `Option` instead.
19992000
impl AddAssign<TimeDelta> for NaiveDate {
20002001
#[inline]
2002+
#[track_caller]
20012003
fn add_assign(&mut self, rhs: TimeDelta) {
20022004
*self = self.add(rhs);
20032005
}
@@ -2030,6 +2032,7 @@ impl AddAssign<TimeDelta> for NaiveDate {
20302032
impl Add<Months> for NaiveDate {
20312033
type Output = NaiveDate;
20322034

2035+
#[track_caller]
20332036
fn add(self, months: Months) -> Self::Output {
20342037
self.checked_add_months(months).expect("`NaiveDate + Months` out of range")
20352038
}
@@ -2059,6 +2062,7 @@ impl Add<Months> for NaiveDate {
20592062
impl Sub<Months> for NaiveDate {
20602063
type Output = NaiveDate;
20612064

2065+
#[track_caller]
20622066
fn sub(self, months: Months) -> Self::Output {
20632067
self.checked_sub_months(months).expect("`NaiveDate - Months` out of range")
20642068
}
@@ -2073,6 +2077,7 @@ impl Sub<Months> for NaiveDate {
20732077
impl Add<Days> for NaiveDate {
20742078
type Output = NaiveDate;
20752079

2080+
#[track_caller]
20762081
fn add(self, days: Days) -> Self::Output {
20772082
self.checked_add_days(days).expect("`NaiveDate + Days` out of range")
20782083
}
@@ -2087,6 +2092,7 @@ impl Add<Days> for NaiveDate {
20872092
impl Sub<Days> for NaiveDate {
20882093
type Output = NaiveDate;
20892094

2095+
#[track_caller]
20902096
fn sub(self, days: Days) -> Self::Output {
20912097
self.checked_sub_days(days).expect("`NaiveDate - Days` out of range")
20922098
}
@@ -2134,6 +2140,7 @@ impl Sub<TimeDelta> for NaiveDate {
21342140
type Output = NaiveDate;
21352141

21362142
#[inline]
2143+
#[track_caller]
21372144
fn sub(self, rhs: TimeDelta) -> NaiveDate {
21382145
self.checked_sub_signed(rhs).expect("`NaiveDate - TimeDelta` overflowed")
21392146
}
@@ -2151,6 +2158,7 @@ impl Sub<TimeDelta> for NaiveDate {
21512158
/// Consider using [`NaiveDate::checked_sub_signed`] to get an `Option` instead.
21522159
impl SubAssign<TimeDelta> for NaiveDate {
21532160
#[inline]
2161+
#[track_caller]
21542162
fn sub_assign(&mut self, rhs: TimeDelta) {
21552163
*self = self.sub(rhs);
21562164
}

src/naive/datetime/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,7 @@ impl Add<TimeDelta> for NaiveDateTime {
16311631
type Output = NaiveDateTime;
16321632

16331633
#[inline]
1634+
#[track_caller]
16341635
fn add(self, rhs: TimeDelta) -> NaiveDateTime {
16351636
self.checked_add_signed(rhs).expect("`NaiveDateTime + TimeDelta` overflowed")
16361637
}
@@ -1650,6 +1651,7 @@ impl Add<Duration> for NaiveDateTime {
16501651
type Output = NaiveDateTime;
16511652

16521653
#[inline]
1654+
#[track_caller]
16531655
fn add(self, rhs: Duration) -> NaiveDateTime {
16541656
let rhs = TimeDelta::from_std(rhs)
16551657
.expect("overflow converting from core::time::Duration to TimeDelta");
@@ -1669,6 +1671,7 @@ impl Add<Duration> for NaiveDateTime {
16691671
/// Consider using [`NaiveDateTime::checked_add_signed`] to get an `Option` instead.
16701672
impl AddAssign<TimeDelta> for NaiveDateTime {
16711673
#[inline]
1674+
#[track_caller]
16721675
fn add_assign(&mut self, rhs: TimeDelta) {
16731676
*self = self.add(rhs);
16741677
}
@@ -1686,6 +1689,7 @@ impl AddAssign<TimeDelta> for NaiveDateTime {
16861689
/// Consider using [`NaiveDateTime::checked_add_signed`] to get an `Option` instead.
16871690
impl AddAssign<Duration> for NaiveDateTime {
16881691
#[inline]
1692+
#[track_caller]
16891693
fn add_assign(&mut self, rhs: Duration) {
16901694
*self = self.add(rhs);
16911695
}
@@ -1701,6 +1705,7 @@ impl Add<FixedOffset> for NaiveDateTime {
17011705
type Output = NaiveDateTime;
17021706

17031707
#[inline]
1708+
#[track_caller]
17041709
fn add(self, rhs: FixedOffset) -> NaiveDateTime {
17051710
self.checked_add_offset(rhs).expect("`NaiveDateTime + FixedOffset` out of range")
17061711
}
@@ -1754,6 +1759,7 @@ impl Add<FixedOffset> for NaiveDateTime {
17541759
impl Add<Months> for NaiveDateTime {
17551760
type Output = NaiveDateTime;
17561761

1762+
#[track_caller]
17571763
fn add(self, rhs: Months) -> Self::Output {
17581764
self.checked_add_months(rhs).expect("`NaiveDateTime + Months` out of range")
17591765
}
@@ -1819,6 +1825,7 @@ impl Sub<TimeDelta> for NaiveDateTime {
18191825
type Output = NaiveDateTime;
18201826

18211827
#[inline]
1828+
#[track_caller]
18221829
fn sub(self, rhs: TimeDelta) -> NaiveDateTime {
18231830
self.checked_sub_signed(rhs).expect("`NaiveDateTime - TimeDelta` overflowed")
18241831
}
@@ -1838,6 +1845,7 @@ impl Sub<Duration> for NaiveDateTime {
18381845
type Output = NaiveDateTime;
18391846

18401847
#[inline]
1848+
#[track_caller]
18411849
fn sub(self, rhs: Duration) -> NaiveDateTime {
18421850
let rhs = TimeDelta::from_std(rhs)
18431851
.expect("overflow converting from core::time::Duration to TimeDelta");
@@ -1859,6 +1867,7 @@ impl Sub<Duration> for NaiveDateTime {
18591867
/// Consider using [`NaiveDateTime::checked_sub_signed`] to get an `Option` instead.
18601868
impl SubAssign<TimeDelta> for NaiveDateTime {
18611869
#[inline]
1870+
#[track_caller]
18621871
fn sub_assign(&mut self, rhs: TimeDelta) {
18631872
*self = self.sub(rhs);
18641873
}
@@ -1876,6 +1885,7 @@ impl SubAssign<TimeDelta> for NaiveDateTime {
18761885
/// Consider using [`NaiveDateTime::checked_sub_signed`] to get an `Option` instead.
18771886
impl SubAssign<Duration> for NaiveDateTime {
18781887
#[inline]
1888+
#[track_caller]
18791889
fn sub_assign(&mut self, rhs: Duration) {
18801890
*self = self.sub(rhs);
18811891
}
@@ -1891,6 +1901,7 @@ impl Sub<FixedOffset> for NaiveDateTime {
18911901
type Output = NaiveDateTime;
18921902

18931903
#[inline]
1904+
#[track_caller]
18941905
fn sub(self, rhs: FixedOffset) -> NaiveDateTime {
18951906
self.checked_sub_offset(rhs).expect("`NaiveDateTime - FixedOffset` out of range")
18961907
}
@@ -1930,6 +1941,7 @@ impl Sub<FixedOffset> for NaiveDateTime {
19301941
impl Sub<Months> for NaiveDateTime {
19311942
type Output = NaiveDateTime;
19321943

1944+
#[track_caller]
19331945
fn sub(self, rhs: Months) -> Self::Output {
19341946
self.checked_sub_months(rhs).expect("`NaiveDateTime - Months` out of range")
19351947
}
@@ -2002,6 +2014,7 @@ impl Sub<NaiveDateTime> for NaiveDateTime {
20022014
impl Add<Days> for NaiveDateTime {
20032015
type Output = NaiveDateTime;
20042016

2017+
#[track_caller]
20052018
fn add(self, days: Days) -> Self::Output {
20062019
self.checked_add_days(days).expect("`NaiveDateTime + Days` out of range")
20072020
}
@@ -2016,6 +2029,7 @@ impl Add<Days> for NaiveDateTime {
20162029
impl Sub<Days> for NaiveDateTime {
20172030
type Output = NaiveDateTime;
20182031

2032+
#[track_caller]
20192033
fn sub(self, days: Days) -> Self::Output {
20202034
self.checked_sub_days(days).expect("`NaiveDateTime - Days` out of range")
20212035
}

src/naive/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl NaiveWeek {
6161
/// ```
6262
#[inline]
6363
#[must_use]
64+
#[track_caller]
6465
pub const fn first_day(&self) -> NaiveDate {
6566
expect(self.checked_first_day(), "first weekday out of range for `NaiveDate`")
6667
}
@@ -113,6 +114,7 @@ impl NaiveWeek {
113114
/// ```
114115
#[inline]
115116
#[must_use]
117+
#[track_caller]
116118
pub const fn last_day(&self) -> NaiveDate {
117119
expect(self.checked_last_day(), "last weekday out of range for `NaiveDate`")
118120
}
@@ -167,6 +169,7 @@ impl NaiveWeek {
167169
/// ```
168170
#[inline]
169171
#[must_use]
172+
#[track_caller]
170173
pub const fn days(&self) -> RangeInclusive<NaiveDate> {
171174
// `expect` doesn't work because `RangeInclusive` is not `Copy`
172175
match self.checked_days() {

0 commit comments

Comments
 (0)