From 853154b239e389f34dd813451ca69b1a1272a897 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:28:35 -0500 Subject: [PATCH 01/16] Only create naive TsSeconds if rustc-serialize is enabled --- src/datetime.rs | 57 ++++++++++++++++++++----------------------- src/lib.rs | 7 ++++-- src/naive/datetime.rs | 45 +++++++++++++++++----------------- 3 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 50d0830..7a40d4d 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -6,8 +6,6 @@ use std::{str, fmt, hash}; use std::cmp::Ordering; use std::ops::{Add, Sub}; -#[cfg(feature = "rustc-serialize")] -use std::ops::Deref; use std::time::{SystemTime, UNIX_EPOCH}; use oldtime::Duration as OldDuration; @@ -29,30 +27,6 @@ pub struct DateTime { offset: Tz::Offset, } -/// A DateTime that can be deserialized from a timestamp -/// -/// A timestamp here is seconds since the epoch -#[cfg(feature = "rustc-serialize")] -pub struct TsSeconds(DateTime); - -#[cfg(feature = "rustc-serialize")] -impl From> for DateTime { - /// Pull the inner DateTime out - fn from(obj: TsSeconds) -> DateTime { - obj.0 - } -} - -#[cfg(feature = "rustc-serialize")] -impl Deref for TsSeconds { - type Target = DateTime; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - - impl DateTime { /// Makes a new `DateTime` with given *UTC* datetime and offset. /// The local datetime should be constructed via the `TimeZone` trait. @@ -517,9 +491,9 @@ fn test_decodable_json(utc_from_str: FUtc, fn test_decodable_json_timestamps(utc_from_str: FUtc, fixed_from_str: FFixed, local_from_str: FLocal) - where FUtc: Fn(&str) -> Result, E>, - FFixed: Fn(&str) -> Result, E>, - FLocal: Fn(&str) -> Result, E>, + where FUtc: Fn(&str) -> Result, E>, + FFixed: Fn(&str) -> Result, E>, + FLocal: Fn(&str) -> Result, E>, E: ::std::fmt::Debug { fn norm(dt: &Option>) -> Option<(&DateTime, &Tz::Offset)> { @@ -543,9 +517,10 @@ fn test_decodable_json_timestamps(utc_from_str: FUtc, } #[cfg(feature = "rustc-serialize")] -mod rustc_serialize { +pub mod rustc_serialize { use std::fmt; - use super::{DateTime, TsSeconds}; + use std::ops::Deref; + use super::DateTime; use offset::{TimeZone, LocalResult, Utc, Local, FixedOffset}; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; @@ -592,6 +567,26 @@ mod rustc_serialize { } } + /// A DateTime that can be deserialized from a timestamp + /// + /// A timestamp here is seconds since the epoch + pub struct TsSeconds(DateTime); + + impl From> for DateTime { + /// Pull the inner DateTime out + fn from(obj: TsSeconds) -> DateTime { + obj.0 + } + } + + impl Deref for TsSeconds { + type Target = DateTime; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl Decodable for TsSeconds { fn decode(d: &mut D) -> Result, D::Error> { from(Utc.timestamp_opt(d.read_i64()?, 0), d) diff --git a/src/lib.rs b/src/lib.rs index c4a70f8..bc393f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -374,7 +374,7 @@ pub use oldtime::Duration; #[doc(no_inline)] pub use naive::{NaiveDate, IsoWeek, NaiveTime, NaiveDateTime}; pub use date::{Date, MIN_DATE, MAX_DATE}; pub use datetime::DateTime; -#[cfg(feature = "rustc-serialize")] pub use datetime::TsSeconds; +#[cfg(feature = "rustc-serialize")] pub use datetime::rustc_serialize::TsSeconds; pub use format::{ParseError, ParseResult}; /// A convenience module appropriate for glob imports (`use chrono::prelude::*;`). @@ -410,7 +410,10 @@ pub mod naive { pub use self::date::{NaiveDate, MIN_DATE, MAX_DATE}; pub use self::isoweek::IsoWeek; pub use self::time::NaiveTime; - pub use self::datetime::{NaiveDateTime, TsSeconds}; + pub use self::datetime::NaiveDateTime; + #[cfg(feature = "rustc-serialize")] + pub use self::datetime::rustc_serialize::TsSeconds; + /// Serialization/Deserialization of naive types in alternate formats /// diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 4758be4..6e628de 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -4,7 +4,7 @@ //! ISO 8601 date and time without timezone. use std::{str, fmt, hash}; -use std::ops::{Add, Sub, Deref, AddAssign, SubAssign}; +use std::ops::{Add, Sub, AddAssign, SubAssign}; use num::traits::ToPrimitive; use oldtime::Duration as OldDuration; @@ -53,24 +53,6 @@ pub struct NaiveDateTime { time: NaiveTime, } -/// A DateTime that can be deserialized from a seconds-based timestamp -pub struct TsSeconds(NaiveDateTime); - -impl From for NaiveDateTime { - /// Pull the internal NaiveDateTime out - fn from(obj: TsSeconds) -> NaiveDateTime { - obj.0 - } -} - -impl Deref for TsSeconds { - type Target = NaiveDateTime; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - impl NaiveDateTime { /// Makes a new `NaiveDateTime` from date and time components. /// Equivalent to [`date.and_time(time)`](./struct.NaiveDate.html#method.and_time) @@ -1459,7 +1441,7 @@ fn test_decodable_json(from_str: F) #[cfg(all(test, feature = "rustc-serialize"))] fn test_decodable_json_timestamp(from_str: F) - where F: Fn(&str) -> Result, E: ::std::fmt::Debug + where F: Fn(&str) -> Result, E: ::std::fmt::Debug { assert_eq!( *from_str("0").unwrap(), @@ -1474,8 +1456,9 @@ fn test_decodable_json_timestamp(from_str: F) } #[cfg(feature = "rustc-serialize")] -mod rustc_serialize { - use super::{NaiveDateTime, TsSeconds}; +pub mod rustc_serialize { + use std::ops::Deref; + use super::NaiveDateTime; use rustc_serialize::{Encodable, Encoder, Decodable, Decoder}; impl Encodable for NaiveDateTime { @@ -1490,6 +1473,24 @@ mod rustc_serialize { } } + /// A `DateTime` that can be deserialized from a seconds-based timestamp + pub struct TsSeconds(NaiveDateTime); + + impl From for NaiveDateTime { + /// Pull the internal NaiveDateTime out + fn from(obj: TsSeconds) -> NaiveDateTime { + obj.0 + } + } + + impl Deref for TsSeconds { + type Target = NaiveDateTime; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl Decodable for TsSeconds { fn decode(d: &mut D) -> Result { Ok(TsSeconds( From fac6ff44c9c0c77e74423ffada91caf0d6e7b369 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:08:52 -0500 Subject: [PATCH 02/16] Fix Clippy lints: needless_return --- src/format/strftime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/format/strftime.rs b/src/format/strftime.rs index 2002d31..da85579 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -180,7 +180,7 @@ impl<'a> Iterator for StrftimeItems<'a> { match self.remainder.chars().next() { // we are done - None => return None, + None => None, // the next item is a specifier Some('%') => { From 4ca861c905b7fda5decac4cff112cc2f613e0ae3 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:41:16 -0500 Subject: [PATCH 03/16] Fix Clippy lints: should_assert_eq --- src/naive/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/naive/time.rs b/src/naive/time.rs index 2ded525..558bf9d 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -545,13 +545,13 @@ impl NaiveTime { let rhssecs = rhs.num_seconds(); let rhsfrac = (rhs - OldDuration::seconds(rhssecs)).num_nanoseconds().unwrap(); - debug_assert!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac) == rhs); + debug_assert_eq!(OldDuration::seconds(rhssecs) + OldDuration::nanoseconds(rhsfrac), rhs); let rhssecsinday = rhssecs % 86400; let mut morerhssecs = rhssecs - rhssecsinday; let rhssecs = rhssecsinday as i32; let rhsfrac = rhsfrac as i32; debug_assert!(-86400 < rhssecs && rhssecs < 86400); - debug_assert!(morerhssecs % 86400 == 0); + debug_assert_eq!(morerhssecs % 86400, 0); debug_assert!(-1_000_000_000 < rhsfrac && rhsfrac < 1_000_000_000); let mut secs = secs as i32 + rhssecs; From ed8f406a61fde421ca8e98f328b4d25b05103d8a Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:47:59 -0500 Subject: [PATCH 04/16] Fix Clippy lints: redundant_closure --- src/datetime.rs | 4 ++-- src/naive/datetime.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 7a40d4d..1fc7c18 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -554,7 +554,7 @@ pub mod rustc_serialize { impl Decodable for TsSeconds { fn decode(d: &mut D) -> Result, D::Error> { from(FixedOffset::east(0).timestamp_opt(d.read_i64()?, 0), d) - .map(|dt| TsSeconds(dt)) + .map(TsSeconds) } } @@ -590,7 +590,7 @@ pub mod rustc_serialize { impl Decodable for TsSeconds { fn decode(d: &mut D) -> Result, D::Error> { from(Utc.timestamp_opt(d.read_i64()?, 0), d) - .map(|dt| TsSeconds(dt)) + .map(TsSeconds) } } diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 6e628de..50ebd87 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -135,7 +135,7 @@ impl NaiveDateTime { pub fn from_timestamp_opt(secs: i64, nsecs: u32) -> Option { let (days, secs) = div_mod_floor(secs, 86400); let date = days.to_i32().and_then(|days| days.checked_add(719163)) - .and_then(|days_ce| NaiveDate::from_num_days_from_ce_opt(days_ce)); + .and_then(NaiveDate::from_num_days_from_ce_opt); let time = NaiveTime::from_num_seconds_from_midnight_opt(secs as u32, nsecs); match (date, time) { (Some(date), Some(time)) => Some(NaiveDateTime { date: date, time: time }), From 3e6e0e51c64cc9f4c772445da118f54c1b6658df Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:53:19 -0500 Subject: [PATCH 05/16] Fix Clippy lints: doc_markdown --- src/datetime.rs | 4 ++-- src/naive/datetime.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 1fc7c18..1704449 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -567,7 +567,7 @@ pub mod rustc_serialize { } } - /// A DateTime that can be deserialized from a timestamp + /// A `DateTime` that can be deserialized from a timestamp /// /// A timestamp here is seconds since the epoch pub struct TsSeconds(DateTime); @@ -680,7 +680,7 @@ pub mod serde { use {DateTime, Utc, FixedOffset}; use offset::{LocalResult, TimeZone}; - /// Deserialize a DateTime from a seconds timestamp + /// Deserialize a `DateTime` from a seconds timestamp /// /// Intended for use with `serde`s `deserialize_with` attribute. /// diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 50ebd87..247ee8a 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1519,14 +1519,14 @@ pub mod rustc_serialize { } -/// Tools to help serializing/deserializing NaiveDateTimes +/// Tools to help serializing/deserializing `NaiveDateTime`s #[cfg(feature = "serde")] pub mod serde { use std::fmt; use super::{NaiveDateTime}; use serdelib::{ser, de}; - /// Serialize a NaiveDateTime as an RFC 3339 string + /// Serialize a `NaiveDateTime` as an RFC 3339 string /// /// See [the `serde` module](./serde/index.html) for alternate /// serialization formats. @@ -1614,7 +1614,7 @@ pub mod serde { use NaiveDateTime; - /// Deserialize a DateTime from a seconds timestamp + /// Deserialize a `DateTime` from a seconds timestamp /// /// Intended for use with `serde`s `deserialize_with` attribute. /// From c63a0122539b5c4611eecc1fbb54ebaa75f2fab3 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:55:17 -0500 Subject: [PATCH 06/16] Fix Clippy lints: needless_lifetimes --- src/date.rs | 2 +- src/datetime.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/date.rs b/src/date.rs index 7a049d6..7acdde7 100644 --- a/src/date.rs +++ b/src/date.rs @@ -184,7 +184,7 @@ impl Date { /// Retrieves an associated offset from UTC. #[inline] - pub fn offset<'a>(&'a self) -> &'a Tz::Offset { + pub fn offset(&self) -> &Tz::Offset { &self.offset } diff --git a/src/datetime.rs b/src/datetime.rs index 1704449..566849f 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -98,7 +98,7 @@ impl DateTime { /// Retrieves an associated offset from UTC. #[inline] - pub fn offset<'a>(&'a self) -> &'a Tz::Offset { + pub fn offset(&self) -> &Tz::Offset { &self.offset } From de7f0f64f7eb25fa8dac2d6de3500e94c8f910c5 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:58:07 -0500 Subject: [PATCH 07/16] Fix Clippy lints: needless_pass_by_value --- src/datetime.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 566849f..5934b21 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -766,19 +766,19 @@ pub mod serde { fn visit_i64(self, value: i64) -> Result, E> where E: de::Error { - from(FixedOffset::east(0).timestamp_opt(value, 0), value) + from(FixedOffset::east(0).timestamp_opt(value, 0), &value) } /// Deserialize a timestamp in seconds since the epoch fn visit_u64(self, value: u64) -> Result, E> where E: de::Error { - from(FixedOffset::east(0).timestamp_opt(value as i64, 0), value) + from(FixedOffset::east(0).timestamp_opt(value as i64, 0), &value) } } // try!-like function to convert a LocalResult into a serde-ish Result - fn from(me: LocalResult, ts: V) -> Result + fn from(me: LocalResult, ts: &V) -> Result where E: de::Error, V: fmt::Display, T: fmt::Display, From fd6036a8b9c9eb71adfef395e4ed5ecf2c87fc90 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 14:59:43 -0500 Subject: [PATCH 08/16] Fix Clippy lints: assign_op_pattern --- src/format/parsed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/format/parsed.rs b/src/format/parsed.rs index 3ba371d..b28d735 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -538,7 +538,7 @@ impl Parsed { // it's okay, just do not try to overwrite the existing field. 59 => {} // `datetime` is known to be off by one second. - 0 => { datetime = datetime - OldDuration::seconds(1); } + 0 => { datetime -= OldDuration::seconds(1); } // otherwise it is impossible. _ => return Err(IMPOSSIBLE) } From 4a70e160fbfde500baeea1dd7cccf6d80ca0d828 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 15:02:58 -0500 Subject: [PATCH 09/16] Fix Clippy lints: or_fun_call --- src/format/scan.rs | 6 ++++-- src/format/strftime.rs | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/format/scan.rs b/src/format/scan.rs index 345b842..a492202 100644 --- a/src/format/scan.rs +++ b/src/format/scan.rs @@ -36,7 +36,8 @@ pub fn number(s: &str, min: usize, max: usize) -> ParseResult<(&str, i64)> { if window.len() > max { window = &window[..max]; } // scan digits - let upto = window.iter().position(|&c| c < b'0' || b'9' < c).unwrap_or(window.len()); + let upto = window.iter().position(|&c| c < b'0' || b'9' < c) + .unwrap_or_else(|| window.len()); if upto < min { return Err(if window.is_empty() {TOO_SHORT} else {INVALID}); } @@ -224,7 +225,8 @@ pub fn timezone_offset_zulu(s: &str, colon: F) -> ParseResult<(&str, i32)> pub fn timezone_offset_2822(s: &str) -> ParseResult<(&str, Option)> { // tries to parse legacy time zone names let upto = s.as_bytes().iter().position(|&c| match c { b'a'...b'z' | b'A'...b'Z' => false, - _ => true }).unwrap_or(s.len()); + _ => true }) + .unwrap_or_else(|| s.len()); if upto > 0 { let name = &s[..upto]; let s = &s[upto..]; diff --git a/src/format/strftime.rs b/src/format/strftime.rs index da85579..fdf490c 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -305,7 +305,7 @@ impl<'a> Iterator for StrftimeItems<'a> { Some(c) if c.is_whitespace() => { // `%` is not a whitespace, so `c != '%'` is redundant let nextspec = self.remainder.find(|c: char| !c.is_whitespace()) - .unwrap_or(self.remainder.len()); + .unwrap_or_else(|| self.remainder.len()); assert!(nextspec > 0); let item = sp!(&self.remainder[..nextspec]); self.remainder = &self.remainder[nextspec..]; @@ -315,7 +315,7 @@ impl<'a> Iterator for StrftimeItems<'a> { // the next item is literal _ => { let nextspec = self.remainder.find(|c: char| c.is_whitespace() || c == '%') - .unwrap_or(self.remainder.len()); + .unwrap_or_else(|| self.remainder.len()); assert!(nextspec > 0); let item = lit!(&self.remainder[..nextspec]); self.remainder = &self.remainder[nextspec..]; From 96b31283d0525adf4ba8e52dc773dd51b1a84004 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 15:14:41 -0500 Subject: [PATCH 10/16] Fix Clippy lints: if_same_then_else --- src/format/scan.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/format/scan.rs b/src/format/scan.rs index a492202..be83df7 100644 --- a/src/format/scan.rs +++ b/src/format/scan.rs @@ -230,24 +230,19 @@ pub fn timezone_offset_2822(s: &str) -> ParseResult<(&str, Option)> { if upto > 0 { let name = &s[..upto]; let s = &s[upto..]; + let offset_hours = |o| Ok((s, Some(o * 3600))); if equals(name, "gmt") || equals(name, "ut") { - Ok((s, Some(0))) - } else if equals(name, "est") { - Ok((s, Some(-5 * 3600))) + offset_hours(0) } else if equals(name, "edt") { - Ok((s, Some(-4 * 3600))) - } else if equals(name, "cst") { - Ok((s, Some(-6 * 3600))) - } else if equals(name, "cdt") { - Ok((s, Some(-5 * 3600))) - } else if equals(name, "mst") { - Ok((s, Some(-7 * 3600))) - } else if equals(name, "mdt") { - Ok((s, Some(-6 * 3600))) + offset_hours(-4) + } else if equals(name, "est") || equals(name, "cdt") { + offset_hours(-5) + } else if equals(name, "cst") || equals(name, "mdt") { + offset_hours(-6) + } else if equals(name, "mst") || equals(name, "pdt") { + offset_hours(-7) } else if equals(name, "pst") { - Ok((s, Some(-8 * 3600))) - } else if equals(name, "pdt") { - Ok((s, Some(-7 * 3600))) + offset_hours(-8) } else { Ok((s, None)) // recommended by RFC 2822: consume but treat it as -0000 } From 0dc4e3a416c9646711968da1f8b97809f4f3ae88 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 16:01:01 -0500 Subject: [PATCH 11/16] Fix Clippy lints: type_complexity --- src/format/parse.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/format/parse.rs b/src/format/parse.rs index 23c7596..9888380 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -230,9 +230,9 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<( Item::Numeric(spec, _pad) => { use super::Numeric::*; + type Setter = fn(&mut Parsed, i64) -> ParseResult<()>; - let (width, signed, set): (usize, bool, - fn(&mut Parsed, i64) -> ParseResult<()>) = match spec { + let (width, signed, set): (usize, bool, Setter) = match spec { Year => (4, true, Parsed::set_year), YearDiv100 => (2, false, Parsed::set_year_div_100), YearMod100 => (2, false, Parsed::set_year_mod_100), From 330504792b796a536ec8465dc814acd8cf7db196 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 23 Jun 2017 16:02:07 -0500 Subject: [PATCH 12/16] Fix Clippy lints: match_same_arms --- src/format/strftime.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/format/strftime.rs b/src/format/strftime.rs index fdf490c..4e015d3 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -237,7 +237,7 @@ impl<'a> Iterator for StrftimeItems<'a> { 'Y' => num0!(Year), 'Z' => fix!(TimezoneName), 'a' => fix!(ShortWeekdayName), - 'b' => fix!(ShortMonthName), + 'b' | 'h' => fix!(ShortMonthName), 'c' => recons![fix!(ShortWeekdayName), sp!(" "), fix!(ShortMonthName), sp!(" "), nums!(Day), sp!(" "), num0!(Hour), lit!(":"), num0!(Minute), lit!(":"), num0!(Second), sp!(" "), num0!(Year)], @@ -245,7 +245,6 @@ impl<'a> Iterator for StrftimeItems<'a> { 'e' => nums!(Day), 'f' => num0!(Nanosecond), 'g' => num0!(IsoYearMod100), - 'h' => fix!(ShortMonthName), 'j' => num0!(Ordinal), 'k' => nums!(Hour), 'l' => nums!(Hour12), From 4c18d701cacc4b7e4b8ed7dd8101ff82e7e36ba0 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Sat, 24 Jun 2017 12:42:59 -0500 Subject: [PATCH 13/16] Allow Clippy lint: cyclomatic complexity Honestly this function is pretty clear, breaking it up makes it seem worse. --- src/naive/time.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/naive/time.rs b/src/naive/time.rs index 558bf9d..3222c7a 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -518,6 +518,7 @@ impl NaiveTime { /// (from_hms(20, 4, 5), -86400)); /// # } /// ~~~~ + #[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))] pub fn overflowing_add_signed(&self, mut rhs: OldDuration) -> (NaiveTime, i64) { let mut secs = self.secs; let mut frac = self.frac; From 5643b845a54d3baaa48528d3009d95b600bfd3e3 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Sun, 9 Jul 2017 15:21:51 -0500 Subject: [PATCH 14/16] Allow Clippy lint: derive_hash_xor_eq The implementation is identical to how #[derive] would do it, and we use the implementation to add some documentation warning people not to use items with nanosecond-level precision in hash maps unless they're sure that's what they want. --- src/naive/date.rs | 12 ++---------- src/naive/datetime.rs | 1 + src/naive/time.rs | 1 + 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/naive/date.rs b/src/naive/date.rs index e178f5e..6fe21e1 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -3,7 +3,7 @@ //! ISO 8601 calendar date without timezone. -use std::{str, fmt, hash}; +use std::{str, fmt}; use std::ops::{Add, Sub, AddAssign, SubAssign}; use num::traits::ToPrimitive; use oldtime::Duration as OldDuration; @@ -93,7 +93,7 @@ const MAX_BITS: usize = 44; /// The year number is same to that of the [calendar date](#calendar-date). /// /// This is currently the internal format of Chrono's date types. -#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] +#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Copy, Clone)] pub struct NaiveDate { ymdf: DateImpl, // (year << 13) | of } @@ -1302,14 +1302,6 @@ impl Datelike for NaiveDate { } } -/// `NaiveDate` can be used as a key to the hash maps. -impl hash::Hash for NaiveDate { - fn hash(&self, state: &mut H) { - // don't need to strip flags, as we can safely assume that it is correct - self.ymdf.hash(state); - } -} - /// An addition of `Duration` to `NaiveDate` discards the fractional days, /// rounding to the closest integral number of days towards `Duration::zero()`. /// diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 247ee8a..0a022d4 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1103,6 +1103,7 @@ impl Timelike for NaiveDateTime { /// /// Practically this also takes account of fractional seconds, so it is not recommended. /// (For the obvious reason this also distinguishes leap seconds from non-leap seconds.) +#[cfg_attr(feature = "cargo-clippy", allow(derive_hash_xor_eq))] impl hash::Hash for NaiveDateTime { fn hash(&self, state: &mut H) { self.date.hash(state); diff --git a/src/naive/time.rs b/src/naive/time.rs index 3222c7a..1a337ab 100644 --- a/src/naive/time.rs +++ b/src/naive/time.rs @@ -981,6 +981,7 @@ impl Timelike for NaiveTime { /// /// Practically this also takes account of fractional seconds, so it is not recommended. /// (For the obvious reason this also distinguishes leap seconds from non-leap seconds.) +#[cfg_attr(feature = "cargo-clippy", allow(derive_hash_xor_eq))] impl hash::Hash for NaiveTime { fn hash(&self, state: &mut H) { self.secs.hash(state); From 1f625fe69d2ba7a56eac036e43c70be91f366d66 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Sat, 24 Jun 2017 12:27:37 -0500 Subject: [PATCH 15/16] Enable Clippy in travis This will run clippy on all builds, and as long as clippy successfully compiles it will deny builds that fail clippy lints. --- .travis.sh | 16 +++++++++++++++- .travis.yml | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.travis.sh b/.travis.sh index cba5909..120cbbe 100755 --- a/.travis.sh +++ b/.travis.sh @@ -46,11 +46,25 @@ build_only() { channel build -v --features 'serde bincode' } +run_clippy() { + # cached installation will not work on a later nightly + if [ -n "${TRAVIS}" ] && ! cargo install clippy --debug --force; then + echo "COULD NOT COMPILE CLIPPY, IGNORING CLIPPY TESTS" + exit + fi + + cargo clippy --features 'serde bincode rustc-serialize' -- -Dclippy +} + rustc --version cargo --version CHANNEL=nightly -build_and_test +if [ "x${CLIPPY}" = xy ] ; then + run_clippy +else + build_and_test +fi CHANNEL=beta build_and_test diff --git a/.travis.yml b/.travis.yml index 74a29cb..3b726d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,15 @@ os: matrix: allow_failures: - rust: nightly + env: CLIPPY=n + include: + - rust: nightly + env: CLIPPY=y + env: global: - LD_LIBRARY_PATH: /usr/local/lib + - CLIPPY: n script: ./.travis.sh notifications: email: false From ce6ea4d8d17c557afdb2b9d7728d1ee19de315d2 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Mon, 26 Jun 2017 11:48:13 -0500 Subject: [PATCH 16/16] deny(missing_debug_implementations) --- src/datetime.rs | 1 + src/format/strftime.rs | 2 +- src/lib.rs | 1 + src/naive/datetime.rs | 1 + src/offset/local.rs | 2 +- 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 5934b21..9c440e8 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -570,6 +570,7 @@ pub mod rustc_serialize { /// A `DateTime` that can be deserialized from a timestamp /// /// A timestamp here is seconds since the epoch + #[derive(Debug)] pub struct TsSeconds(DateTime); impl From> for DateTime { diff --git a/src/format/strftime.rs b/src/format/strftime.rs index 4e015d3..ce1932d 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -149,7 +149,7 @@ Notes: use super::{Item, Numeric, Fixed, Pad}; /// Parsing iterator for `strftime`-like format strings. -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct StrftimeItems<'a> { /// Remaining portion of the string. remainder: &'a str, diff --git a/src/lib.rs b/src/lib.rs index bc393f5..d519b2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -359,6 +359,7 @@ #![cfg_attr(bench, feature(test))] // lib stability features as per RFC #507 #![deny(missing_docs)] +#![deny(missing_debug_implementations)] extern crate time as oldtime; extern crate num; diff --git a/src/naive/datetime.rs b/src/naive/datetime.rs index 0a022d4..cf976e6 100644 --- a/src/naive/datetime.rs +++ b/src/naive/datetime.rs @@ -1475,6 +1475,7 @@ pub mod rustc_serialize { } /// A `DateTime` that can be deserialized from a seconds-based timestamp + #[derive(Debug)] pub struct TsSeconds(NaiveDateTime); impl From for NaiveDateTime { diff --git a/src/offset/local.rs b/src/offset/local.rs index 1f65f1a..6aa4ab7 100644 --- a/src/offset/local.rs +++ b/src/offset/local.rs @@ -77,7 +77,7 @@ fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> oldtime::Timespec { /// let dt: DateTime = Local::now(); /// let dt: DateTime = Local.timestamp(0, 0); /// ~~~~ -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub struct Local; impl Local {