Skip to content

Commit

Permalink
Reduce lint allowing
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Mar 22, 2022
1 parent 0d8abfe commit d156554
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 96 deletions.
84 changes: 28 additions & 56 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::{SystemTime, UNIX_EPOCH};

use super::DateTime;
use crate::naive::{NaiveDate, NaiveTime};
#[cfg(feature = "clock")]
Expand All @@ -6,68 +8,45 @@ use crate::offset::{FixedOffset, TimeZone, Utc};
use crate::oldtime::Duration;
#[cfg(feature = "clock")]
use crate::Datelike;
use std::time::{SystemTime, UNIX_EPOCH};

#[test]
#[allow(non_snake_case)]
fn test_datetime_offset() {
let Est = FixedOffset::west(5 * 60 * 60);
let Edt = FixedOffset::west(4 * 60 * 60);
let Kst = FixedOffset::east(9 * 60 * 60);
let est = FixedOffset::west(5 * 60 * 60);
let edt = FixedOffset::west(4 * 60 * 60);
let kst = FixedOffset::east(9 * 60 * 60);

assert_eq!(format!("{}", Utc.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06 07:08:09 UTC");
assert_eq!(
format!("{}", Edt.ymd(2014, 5, 6).and_hms(7, 8, 9)),
"2014-05-06 07:08:09 -04:00"
);
assert_eq!(
format!("{}", Kst.ymd(2014, 5, 6).and_hms(7, 8, 9)),
"2014-05-06 07:08:09 +09:00"
);
assert_eq!(format!("{}", edt.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06 07:08:09 -04:00");
assert_eq!(format!("{}", kst.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06 07:08:09 +09:00");
assert_eq!(format!("{:?}", Utc.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06T07:08:09Z");
assert_eq!(
format!("{:?}", Edt.ymd(2014, 5, 6).and_hms(7, 8, 9)),
"2014-05-06T07:08:09-04:00"
);
assert_eq!(
format!("{:?}", Kst.ymd(2014, 5, 6).and_hms(7, 8, 9)),
"2014-05-06T07:08:09+09:00"
);
assert_eq!(format!("{:?}", edt.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06T07:08:09-04:00");
assert_eq!(format!("{:?}", kst.ymd(2014, 5, 6).and_hms(7, 8, 9)), "2014-05-06T07:08:09+09:00");

// edge cases
assert_eq!(format!("{:?}", Utc.ymd(2014, 5, 6).and_hms(0, 0, 0)), "2014-05-06T00:00:00Z");
assert_eq!(format!("{:?}", edt.ymd(2014, 5, 6).and_hms(0, 0, 0)), "2014-05-06T00:00:00-04:00");
assert_eq!(format!("{:?}", kst.ymd(2014, 5, 6).and_hms(0, 0, 0)), "2014-05-06T00:00:00+09:00");
assert_eq!(format!("{:?}", Utc.ymd(2014, 5, 6).and_hms(23, 59, 59)), "2014-05-06T23:59:59Z");
assert_eq!(
format!("{:?}", Edt.ymd(2014, 5, 6).and_hms(0, 0, 0)),
"2014-05-06T00:00:00-04:00"
);
assert_eq!(
format!("{:?}", Kst.ymd(2014, 5, 6).and_hms(0, 0, 0)),
"2014-05-06T00:00:00+09:00"
);
assert_eq!(
format!("{:?}", Utc.ymd(2014, 5, 6).and_hms(23, 59, 59)),
"2014-05-06T23:59:59Z"
);
assert_eq!(
format!("{:?}", Edt.ymd(2014, 5, 6).and_hms(23, 59, 59)),
format!("{:?}", edt.ymd(2014, 5, 6).and_hms(23, 59, 59)),
"2014-05-06T23:59:59-04:00"
);
assert_eq!(
format!("{:?}", Kst.ymd(2014, 5, 6).and_hms(23, 59, 59)),
format!("{:?}", kst.ymd(2014, 5, 6).and_hms(23, 59, 59)),
"2014-05-06T23:59:59+09:00"
);

let dt = Utc.ymd(2014, 5, 6).and_hms(7, 8, 9);
assert_eq!(dt, Edt.ymd(2014, 5, 6).and_hms(3, 8, 9));
assert_eq!(dt, edt.ymd(2014, 5, 6).and_hms(3, 8, 9));
assert_eq!(dt + Duration::seconds(3600 + 60 + 1), Utc.ymd(2014, 5, 6).and_hms(8, 9, 10));
assert_eq!(
dt.signed_duration_since(Edt.ymd(2014, 5, 6).and_hms(10, 11, 12)),
dt.signed_duration_since(edt.ymd(2014, 5, 6).and_hms(10, 11, 12)),
Duration::seconds(-7 * 3600 - 3 * 60 - 3)
);

assert_eq!(*Utc.ymd(2014, 5, 6).and_hms(7, 8, 9).offset(), Utc);
assert_eq!(*Edt.ymd(2014, 5, 6).and_hms(7, 8, 9).offset(), Edt);
assert!(*Edt.ymd(2014, 5, 6).and_hms(7, 8, 9).offset() != Est);
assert_eq!(*edt.ymd(2014, 5, 6).and_hms(7, 8, 9).offset(), edt);
assert!(*edt.ymd(2014, 5, 6).and_hms(7, 8, 9).offset() != est);
}

#[test]
Expand Down Expand Up @@ -107,31 +86,27 @@ fn test_datetime_with_timezone() {
}

#[test]
#[allow(non_snake_case)]
fn test_datetime_rfc2822_and_rfc3339() {
let EDT = FixedOffset::east(5 * 60 * 60);
let edt = FixedOffset::east(5 * 60 * 60);
assert_eq!(
Utc.ymd(2015, 2, 18).and_hms(23, 16, 9).to_rfc2822(),
"Wed, 18 Feb 2015 23:16:09 +0000"
);
assert_eq!(Utc.ymd(2015, 2, 18).and_hms(23, 16, 9).to_rfc3339(), "2015-02-18T23:16:09+00:00");
assert_eq!(
Utc.ymd(2015, 2, 18).and_hms(23, 16, 9).to_rfc3339(),
"2015-02-18T23:16:09+00:00"
);
assert_eq!(
EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc2822(),
edt.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc2822(),
"Wed, 18 Feb 2015 23:16:09 +0500"
);
assert_eq!(
EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc3339(),
edt.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc3339(),
"2015-02-18T23:16:09.150+05:00"
);
assert_eq!(
EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc2822(),
edt.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc2822(),
"Wed, 18 Feb 2015 23:59:60 +0500"
);
assert_eq!(
EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc3339(),
edt.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc3339(),
"2015-02-18T23:59:60.234567+05:00"
);

Expand All @@ -149,11 +124,11 @@ fn test_datetime_rfc2822_and_rfc3339() {
);
assert_eq!(
DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:60 +0500"),
Ok(EDT.ymd(2015, 2, 18).and_hms_milli(23, 59, 59, 1_000))
Ok(edt.ymd(2015, 2, 18).and_hms_milli(23, 59, 59, 1_000))
);
assert_eq!(
DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00"),
Ok(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567))
Ok(edt.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567))
);
}

Expand Down Expand Up @@ -237,11 +212,8 @@ fn test_datetime_parse_from_str() {
Ok(ymdhms(2014, 5, 7, 12, 34, 56, 570 * 60))
); // ignore offset
assert!(DateTime::parse_from_str("20140507000000", "%Y%m%d%H%M%S").is_err()); // no offset
assert!(DateTime::parse_from_str(
"Fri, 09 Aug 2013 23:54:35 GMT",
"%a, %d %b %Y %H:%M:%S GMT"
)
.is_err());
assert!(DateTime::parse_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT")
.is_err());
assert_eq!(
Utc.datetime_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT"),
Ok(Utc.ymd(2013, 8, 9).and_hms(23, 54, 35))
Expand Down
30 changes: 24 additions & 6 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
//! # }
//! ```

#![allow(ellipsis_inclusive_range_patterns)]

#[cfg(feature = "alloc")]
extern crate alloc;

Expand Down Expand Up @@ -749,15 +747,22 @@ pub struct DelayedFormat<I> {
/// Locale used for text.
// TODO: Only used with the locale feature. We should make this property
// only present when the feature is enabled.
#[allow(dead_code)]
#[cfg(feature = "unstable-locales")]
locale: Option<Locale>,
}

#[cfg(any(feature = "alloc", feature = "std", test))]
impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
/// Makes a new `DelayedFormat` value out of local date and time.
pub fn new(date: Option<NaiveDate>, time: Option<NaiveTime>, items: I) -> DelayedFormat<I> {
DelayedFormat { date, time, off: None, items, locale: None }
DelayedFormat {
date,
time,
off: None,
items,
#[cfg(feature = "unstable-locales")]
locale: None,
}
}

/// Makes a new `DelayedFormat` value out of local date and time and UTC offset.
Expand All @@ -771,7 +776,14 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
Off: Offset + fmt::Display,
{
let name_and_diff = (offset.to_string(), offset.fix());
DelayedFormat { date, time, off: Some(name_and_diff), items, locale: None }
DelayedFormat {
date,
time,
off: Some(name_and_diff),
items,
#[cfg(feature = "unstable-locales")]
locale: None,
}
}

/// Makes a new `DelayedFormat` value out of local date and time and locale.
Expand All @@ -782,7 +794,13 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
items: I,
locale: Locale,
) -> DelayedFormat<I> {
DelayedFormat { date, time, off: None, items, locale: Some(locale) }
DelayedFormat {
date,
time,
off: None,
items,
locale: Some(locale),
}
}

/// Makes a new `DelayedFormat` value out of local date and time, UTC offset and locale.
Expand Down
4 changes: 2 additions & 2 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
let mut year = try_consume!(scan::number(s, 2, usize::MAX));
let yearlen = prevlen - s.len();
match (yearlen, year) {
(2, 0...49) => {
(2, 0..=49) => {
year += 2000;
} // 47 -> 2047, 05 -> 2005
(2, 50...99) => {
(2, 50..=99) => {
year += 1900;
} // 79 -> 1979
(3, _) => {
Expand Down
19 changes: 9 additions & 10 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::{Datelike, Timelike};
///
/// - `to_*` methods try to make a concrete date and time value out of set fields.
/// It fully checks any remaining out-of-range conditions and inconsistent/impossible fields.
#[allow(missing_copy_implementations)]
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Parsed {
/// Year.
Expand Down Expand Up @@ -305,7 +304,7 @@ impl Parsed {
// check if present quotient and/or modulo is consistent to the full year.
// since the presence of those fields means a positive full year,
// we should filter a negative full year first.
(Some(y), q, r @ Some(0...99)) | (Some(y), q, r @ None) => {
(Some(y), q, r @ Some(0..=99)) | (Some(y), q, r @ None) => {
if y < 0 {
return Err(OUT_OF_RANGE);
}
Expand All @@ -319,7 +318,7 @@ impl Parsed {

// the full year is missing but we have quotient and modulo.
// reconstruct the full year. make sure that the result is always positive.
(None, Some(q), Some(r @ 0...99)) => {
(None, Some(q), Some(r @ 0..=99)) => {
if q < 0 {
return Err(OUT_OF_RANGE);
}
Expand All @@ -329,7 +328,7 @@ impl Parsed {

// we only have modulo. try to interpret a modulo as a conventional two-digit year.
// note: we are affected by Rust issue #18060. avoid multiple range patterns.
(None, None, Some(r @ 0...99)) => Ok(Some(r + if r < 70 { 2000 } else { 1900 })),
(None, None, Some(r @ 0..=99)) => Ok(Some(r + if r < 70 { 2000 } else { 1900 })),

// otherwise it is an out-of-bound or insufficient condition.
(None, Some(_), None) => Err(NOT_ENOUGH),
Expand Down Expand Up @@ -500,32 +499,32 @@ impl Parsed {
/// It is able to handle leap seconds when given second is 60.
pub fn to_naive_time(&self) -> ParseResult<NaiveTime> {
let hour_div_12 = match self.hour_div_12 {
Some(v @ 0...1) => v,
Some(v @ 0..=1) => v,
Some(_) => return Err(OUT_OF_RANGE),
None => return Err(NOT_ENOUGH),
};
let hour_mod_12 = match self.hour_mod_12 {
Some(v @ 0...11) => v,
Some(v @ 0..=11) => v,
Some(_) => return Err(OUT_OF_RANGE),
None => return Err(NOT_ENOUGH),
};
let hour = hour_div_12 * 12 + hour_mod_12;

let minute = match self.minute {
Some(v @ 0...59) => v,
Some(v @ 0..=59) => v,
Some(_) => return Err(OUT_OF_RANGE),
None => return Err(NOT_ENOUGH),
};

// we allow omitting seconds or nanoseconds, but they should be in the range.
let (second, mut nano) = match self.second.unwrap_or(0) {
v @ 0...59 => (v, 0),
v @ 0..=59 => (v, 0),
60 => (59, 1_000_000_000),
_ => return Err(OUT_OF_RANGE),
};
nano += match self.nanosecond {
Some(v @ 0...999_999_999) if self.second.is_some() => v,
Some(0...999_999_999) => return Err(NOT_ENOUGH), // second is missing
Some(v @ 0..=999_999_999) if self.second.is_some() => v,
Some(0..=999_999_999) => return Err(NOT_ENOUGH), // second is missing
Some(_) => return Err(OUT_OF_RANGE),
None => 0,
};
Expand Down
10 changes: 5 additions & 5 deletions src/format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::Weekday;
/// Assumes that the `pattern` is already converted to lower case.
fn equals(s: &str, pattern: &str) -> bool {
let mut xs = s.as_bytes().iter().map(|&c| match c {
b'A'...b'Z' => c + 32,
b'A'..=b'Z' => c + 32,
_ => c,
});
let mut ys = pattern.as_bytes().iter().cloned();
Expand Down Expand Up @@ -240,7 +240,7 @@ where

// hours (00--99)
let hours = match digits(s)? {
(h1 @ b'0'...b'9', h2 @ b'0'...b'9') => i32::from((h1 - b'0') * 10 + (h2 - b'0')),
(h1 @ b'0'..=b'9', h2 @ b'0'..=b'9') => i32::from((h1 - b'0') * 10 + (h2 - b'0')),
_ => return Err(INVALID),
};
s = &s[2..];
Expand All @@ -252,8 +252,8 @@ where
// if the next two items are digits then we have to add minutes
let minutes = if let Ok(ds) = digits(s) {
match ds {
(m1 @ b'0'...b'5', m2 @ b'0'...b'9') => i32::from((m1 - b'0') * 10 + (m2 - b'0')),
(b'6'...b'9', b'0'...b'9') => return Err(OUT_OF_RANGE),
(m1 @ b'0'..=b'5', m2 @ b'0'..=b'9') => i32::from((m1 - b'0') * 10 + (m2 - b'0')),
(b'6'..=b'9', b'0'..=b'9') => return Err(OUT_OF_RANGE),
_ => return Err(INVALID),
}
} else if allow_missing_minutes {
Expand Down Expand Up @@ -314,7 +314,7 @@ pub(super) fn timezone_offset_2822(s: &str) -> ParseResult<(&str, Option<i32>)>
.as_bytes()
.iter()
.position(|&c| match c {
b'a'...b'z' | b'A'...b'Z' => false,
b'a'..=b'z' | b'A'..=b'Z' => false,
_ => true,
})
.unwrap_or(s.len());
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@
#![deny(missing_debug_implementations)]
#![warn(unreachable_pub)]
#![deny(dead_code)]
// lints are added all the time, we test on 1.13
#![allow(unknown_lints)]
#![cfg_attr(not(any(feature = "std", test)), no_std)]

#[cfg(feature = "oldtime")]
Expand Down

0 comments on commit d156554

Please sign in to comment.