From 2ec4fba2352c07267d78a0f739226514b3019a1b Mon Sep 17 00:00:00 2001 From: koushiro Date: Fri, 17 Jan 2020 22:30:12 +0800 Subject: [PATCH] Fix clippy warnings Signed-off-by: koushiro --- benches/datetime_format.rs | 6 ++-- src/date.rs | 74 +++++++++++++++++++------------------- src/duration.rs | 57 ++++++++++++++--------------- src/lib.rs | 12 +++---- 4 files changed, 74 insertions(+), 75 deletions(-) diff --git a/benches/datetime_format.rs b/benches/datetime_format.rs index 8b92c7c..77d4766 100644 --- a/benches/datetime_format.rs +++ b/benches/datetime_format.rs @@ -8,7 +8,7 @@ use humantime::format_rfc3339; #[bench] fn rfc3339_humantime_seconds(b: &mut test::Bencher) { - let time = UNIX_EPOCH + Duration::new(1483228799, 0); + let time = UNIX_EPOCH + Duration::new(1_483_228_799, 0); let mut buf = Vec::with_capacity(100); b.iter(|| { buf.truncate(0); @@ -26,11 +26,11 @@ fn rfc3339_chrono(b: &mut test::Bencher) { use chrono::format::Pad::*; let time = DateTime::::from_utc( - NaiveDateTime::from_timestamp(1483228799, 0), Utc); + NaiveDateTime::from_timestamp(1_483_228_799, 0), Utc); let mut buf = Vec::with_capacity(100); // formatting code from env_logger - const ITEMS: &'static [Item<'static>] = { + const ITEMS: &[Item<'static>] = { &[ Numeric(Year, Zero), Literal("-"), diff --git a/src/date.rs b/src/date.rs index df79bf1..b294bcf 100644 --- a/src/date.rs +++ b/src/date.rs @@ -27,9 +27,9 @@ mod max { all(target_arch="wasm32", not(target_os="emscripten")), ))] mod max { - pub const SECONDS: u64 = 253402300800-1; // last second of year 9999 + pub const SECONDS: u64 = 253_402_300_800-1; // last second of year 9999 #[allow(unused)] - pub const TIMESTAMP: &'static str = "9999-12-31T23:59:59Z"; + pub const TIMESTAMP: &str = "9999-12-31T23:59:59Z"; } /// Error parsing datetime (timestamp) @@ -90,7 +90,7 @@ pub fn parse_rfc3339(s: &str) -> Result { if b[10] != b'T' || b[b.len()-1] != b'Z' { return Err(Error::InvalidFormat); } - return parse_rfc3339_weak(s); + parse_rfc3339_weak(s) } /// Parse RFC3339-like timestamp `2018-02-14 00:28:07` @@ -177,10 +177,8 @@ pub fn parse_rfc3339_weak(s: &str) -> Result { nanos += mult * (b[idx] - b'0') as u32; mult /= 10; } - } else { - if b.len() != 19 && (b.len() > 20 || b[19] != b'Z') { - return Err(Error::InvalidFormat); - } + } else if b.len() != 19 && (b.len() > 20 || b[19] != b'Z') { + return Err(Error::InvalidFormat); } let total_seconds = time + days * 86400; @@ -188,11 +186,11 @@ pub fn parse_rfc3339_weak(s: &str) -> Result { return Err(Error::OutOfRange); } - return Ok(UNIX_EPOCH + Duration::new(total_seconds, nanos)); + Ok(UNIX_EPOCH + Duration::new(total_seconds, nanos)) } fn is_leap_year(y: u64) -> bool { - y % 4 == 0 && (!(y % 100 == 0) || y % 400 == 0) + y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) } /// Format an RFC3339 timestamp `2018-02-14T00:28:07Z` @@ -203,7 +201,7 @@ fn is_leap_year(y: u64) -> bool { /// /// The value is always UTC and ignores system timezone. pub fn format_rfc3339(system_time: SystemTime) -> Rfc3339Timestamp { - return Rfc3339Timestamp(system_time, Precision::Smart); + Rfc3339Timestamp(system_time, Precision::Smart) } /// Format an RFC3339 timestamp `2018-02-14T00:28:07Z` @@ -212,7 +210,7 @@ pub fn format_rfc3339(system_time: SystemTime) -> Rfc3339Timestamp { /// /// The value is always UTC and ignores system timezone. pub fn format_rfc3339_seconds(system_time: SystemTime) -> Rfc3339Timestamp { - return Rfc3339Timestamp(system_time, Precision::Seconds); + Rfc3339Timestamp(system_time, Precision::Seconds) } /// Format an RFC3339 timestamp `2018-02-14T00:28:07.000Z` @@ -221,7 +219,7 @@ pub fn format_rfc3339_seconds(system_time: SystemTime) -> Rfc3339Timestamp { /// /// The value is always UTC and ignores system timezone. pub fn format_rfc3339_millis(system_time: SystemTime) -> Rfc3339Timestamp { - return Rfc3339Timestamp(system_time, Precision::Millis); + Rfc3339Timestamp(system_time, Precision::Millis) } /// Format an RFC3339 timestamp `2018-02-14T00:28:07.000000Z` @@ -230,7 +228,7 @@ pub fn format_rfc3339_millis(system_time: SystemTime) -> Rfc3339Timestamp { /// /// The value is always UTC and ignores system timezone. pub fn format_rfc3339_micros(system_time: SystemTime) -> Rfc3339Timestamp { - return Rfc3339Timestamp(system_time, Precision::Micros); + Rfc3339Timestamp(system_time, Precision::Micros) } /// Format an RFC3339 timestamp `2018-02-14T00:28:07.000000000Z` @@ -239,7 +237,7 @@ pub fn format_rfc3339_micros(system_time: SystemTime) -> Rfc3339Timestamp { /// /// The value is always UTC and ignores system timezone. pub fn format_rfc3339_nanos(system_time: SystemTime) -> Rfc3339Timestamp { - return Rfc3339Timestamp(system_time, Precision::Nanos); + Rfc3339Timestamp(system_time, Precision::Nanos) } impl fmt::Display for Rfc3339Timestamp { @@ -251,7 +249,7 @@ impl fmt::Display for Rfc3339Timestamp { let secs_since_epoch = dur.as_secs(); let nanos = dur.subsec_nanos(); - if secs_since_epoch >= 253402300800 { // year 9999 + if secs_since_epoch >= 253_402_300_800 { // year 9999 return Err(fmt::Error); } @@ -352,13 +350,13 @@ impl fmt::Display for Rfc3339Timestamp { buf[25] = b'0' + (nanos / 1_000 % 10) as u8; buf[26] = b'0' + (nanos / 100 % 10) as u8; buf[27] = b'0' + (nanos / 10 % 10) as u8; - buf[28] = b'0' + (nanos / 1 % 10) as u8; + buf[28] = b'0' + (nanos % 10) as u8; // 29th is 'Z' 29 }; // we know our chars are all ascii - f.write_str(unsafe { str::from_utf8_unchecked(&buf[..offset+1]) }) + f.write_str(unsafe { str::from_utf8_unchecked(&buf[..=offset]) }) } } @@ -378,7 +376,7 @@ mod test { let s = time::at_utc(time::Timespec { sec: sec as i64, nsec: 0 }) .rfc3339().to_string(); let time = UNIX_EPOCH + Duration::new(sec, 0); - return (s, time) + (s, time) } #[test] @@ -398,9 +396,9 @@ mod test { assert_eq!(parse_rfc3339("1970-01-01T00:00:01Z").unwrap(), UNIX_EPOCH + Duration::new(1, 0)); assert_eq!(parse_rfc3339("2018-02-13T23:08:32Z").unwrap(), - UNIX_EPOCH + Duration::new(1518563312, 0)); + UNIX_EPOCH + Duration::new(1_518_563_312, 0)); assert_eq!(parse_rfc3339("2012-01-01T00:00:00Z").unwrap(), - UNIX_EPOCH + Duration::new(1325376000, 0)); + UNIX_EPOCH + Duration::new(1_325_376_000, 0)); } #[test] @@ -412,10 +410,10 @@ mod test { format_rfc3339(UNIX_EPOCH + Duration::new(1, 0)).to_string(), "1970-01-01T00:00:01Z"); assert_eq!( - format_rfc3339(UNIX_EPOCH + Duration::new(1518563312, 0)).to_string(), + format_rfc3339(UNIX_EPOCH + Duration::new(1_518_563_312, 0)).to_string(), "2018-02-13T23:08:32Z"); assert_eq!( - format_rfc3339(UNIX_EPOCH + Duration::new(1325376000, 0)).to_string(), + format_rfc3339(UNIX_EPOCH + Duration::new(1_325_376_000, 0)).to_string(), "2012-01-01T00:00:00Z"); } @@ -427,7 +425,7 @@ mod test { "1970-01-01T00:00:00.000Z"); assert_eq!( format_rfc3339_millis(UNIX_EPOCH + - Duration::new(1518563312, 123_000_000)).to_string(), + Duration::new(1_518_563_312, 123_000_000)).to_string(), "2018-02-13T23:08:32.123Z"); } @@ -439,11 +437,11 @@ mod test { "1970-01-01T00:00:00.000000Z"); assert_eq!( format_rfc3339_micros(UNIX_EPOCH + - Duration::new(1518563312, 123_000_000)).to_string(), + Duration::new(1_518_563_312, 123_000_000)).to_string(), "2018-02-13T23:08:32.123000Z"); assert_eq!( format_rfc3339_micros(UNIX_EPOCH + - Duration::new(1518563312, 456_123_000)).to_string(), + Duration::new(1_518_563_312, 456_123_000)).to_string(), "2018-02-13T23:08:32.456123Z"); } @@ -455,11 +453,11 @@ mod test { "1970-01-01T00:00:00.000000000Z"); assert_eq!( format_rfc3339_nanos(UNIX_EPOCH + - Duration::new(1518563312, 123_000_000)).to_string(), + Duration::new(1_518_563_312, 123_000_000)).to_string(), "2018-02-13T23:08:32.123000000Z"); assert_eq!( format_rfc3339_nanos(UNIX_EPOCH + - Duration::new(1518563312, 789_456_123)).to_string(), + Duration::new(1_518_563_312, 789_456_123)).to_string(), "2018-02-13T23:08:32.789456123Z"); } @@ -473,13 +471,13 @@ mod test { #[test] fn leap_second() { assert_eq!(parse_rfc3339("2016-12-31T23:59:60Z").unwrap(), - UNIX_EPOCH + Duration::new(1483228799, 0)); + UNIX_EPOCH + Duration::new(1_483_228_799, 0)); } #[test] fn first_731_days() { let year_start = 0; // 1970 - for day in 0.. (365 * 2 + 1) { // scan leap year and non-leap year + for day in 0..= 365 * 2 { // scan leap year and non-leap year let (s, time) = from_sec(year_start + day * 86400); assert_eq!(parse_rfc3339(&s).unwrap(), time); assert_eq!(format_rfc3339(time).to_string(), s); @@ -488,8 +486,8 @@ mod test { #[test] fn the_731_consecutive_days() { - let year_start = 1325376000; // 2012 - for day in 0.. (365 * 2 + 1) { // scan leap year and non-leap year + let year_start = 1_325_376_000; // 2012 + for day in 0..= 365 * 2 { // scan leap year and non-leap year let (s, time) = from_sec(year_start + day * 86400); assert_eq!(parse_rfc3339(&s).unwrap(), time); assert_eq!(format_rfc3339(time).to_string(), s); @@ -498,7 +496,7 @@ mod test { #[test] fn all_86400_seconds() { - let day_start = 1325376000; + let day_start = 1_325_376_000; for second in 0..86400 { // scan leap year and non-leap year let (s, time) = from_sec(day_start + second); assert_eq!(parse_rfc3339(&s).unwrap(), time); @@ -520,7 +518,7 @@ mod test { #[test] fn random_wide_range() { - for _ in 0..100000 { + for _ in 0..100_000 { let sec = rand::thread_rng().gen_range(0, max::SECONDS); let (s, time) = from_sec(sec); assert_eq!(parse_rfc3339(&s).unwrap(), time); @@ -531,8 +529,8 @@ mod test { #[test] fn milliseconds() { assert_eq!(parse_rfc3339("1970-01-01T00:00:00.123Z").unwrap(), - UNIX_EPOCH + Duration::new(0, 123000000)); - assert_eq!(format_rfc3339(UNIX_EPOCH + Duration::new(0, 123000000)) + UNIX_EPOCH + Duration::new(0, 123_000_000)); + assert_eq!(format_rfc3339(UNIX_EPOCH + Duration::new(0, 123_000_000)) .to_string(), "1970-01-01T00:00:00.123000000Z"); } @@ -600,15 +598,15 @@ mod test { parse_rfc3339("1970-01-01 00:00:00").unwrap_err(); assert_eq!(parse_rfc3339_weak("1970-01-01 00:00:00.000123").unwrap(), - UNIX_EPOCH + Duration::new(0, 123000)); + UNIX_EPOCH + Duration::new(0, 123_000)); parse_rfc3339("1970-01-01 00:00:00.000123").unwrap_err(); assert_eq!(parse_rfc3339_weak("1970-01-01T00:00:00.000123").unwrap(), - UNIX_EPOCH + Duration::new(0, 123000)); + UNIX_EPOCH + Duration::new(0, 123_000)); parse_rfc3339("1970-01-01T00:00:00.000123").unwrap_err(); assert_eq!(parse_rfc3339_weak("1970-01-01 00:00:00.000123Z").unwrap(), - UNIX_EPOCH + Duration::new(0, 123000)); + UNIX_EPOCH + Duration::new(0, 123_000)); parse_rfc3339("1970-01-01 00:00:00.000123Z").unwrap_err(); assert_eq!(parse_rfc3339_weak("1970-01-01 00:00:00Z").unwrap(), diff --git a/src/duration.rs b/src/duration.rs index 2e95c71..55f4d8c 100644 --- a/src/duration.rs +++ b/src/duration.rs @@ -100,7 +100,7 @@ impl<'a> Parser<'a> { } } } - return Ok(None); + Ok(None) } fn parse_unit(&mut self, n: u64, start: usize, end: usize) -> Result<(), Error> @@ -108,21 +108,21 @@ impl<'a> Parser<'a> { let (mut sec, nsec) = match &self.src[start..end] { "nanos" | "nsec" | "ns" => (0u64, n), "usec" | "us" => (0u64, n.mul(1000)?), - "millis" | "msec" | "ms" => (0u64, n.mul(1000_000)?), + "millis" | "msec" | "ms" => (0u64, n.mul(1_000_000)?), "seconds" | "second" | "secs" | "sec" | "s" => (n, 0), "minutes" | "minute" | "min" | "mins" | "m" => (n.mul(60)?, 0), "hours" | "hour" | "hr" | "hrs" | "h" => (n.mul(3600)?, 0), "days" | "day" | "d" => (n.mul(86400)?, 0), "weeks" | "week" | "w" => (n.mul(86400*7)?, 0), - "months" | "month" | "M" => (n.mul(2630016)?, 0), // 30.44d - "years" | "year" | "y" => (n.mul(31557600)?, 0), // 365.25d + "months" | "month" | "M" => (n.mul(2_630_016)?, 0), // 30.44d + "years" | "year" | "y" => (n.mul(31_557_600)?, 0), // 365.25d _ => return Err(Error::UnknownUnit(start, end)), }; let mut nsec = self.current.1.add(nsec)?; - if nsec > 1000_000_000 { - sec = sec.add(nsec / 1000_000_000)?; - nsec %= 1000_000_000; + if nsec > 1_000_000_000 { + sec = sec.add(nsec / 1_000_000_000)?; + nsec %= 1_000_000_000; } sec = self.current.0.add(sec)?; self.current = (sec, nsec); @@ -271,10 +271,10 @@ impl fmt::Display for FormattedDuration { return Ok(()); } - let years = secs / 31557600; // 365.25d - let ydays = secs % 31557600; - let months = ydays / 2630016; // 30.44d - let mdays = ydays % 2630016; + let years = secs / 31_557_600; // 365.25d + let ydays = secs % 31_557_600; + let months = ydays / 2_630_016; // 30.44d + let mdays = ydays % 2_630_016; let days = mdays / 86400; let day_secs = mdays % 86400; let hours = day_secs / 3600; @@ -285,7 +285,7 @@ impl fmt::Display for FormattedDuration { let micros = nanos / 1000 % 1000; let nanosec = nanos % 1000; - let ref mut started = false; + let started = &mut false; item_plural(f, started, "year", years)?; item_plural(f, started, "month", months)?; item_plural(f, started, "day", days)?; @@ -309,15 +309,16 @@ mod test { use super::Error; #[test] + #[allow(clippy::cognitive_complexity)] fn test_units() { assert_eq!(parse_duration("17nsec"), Ok(Duration::new(0, 17))); assert_eq!(parse_duration("17nanos"), Ok(Duration::new(0, 17))); assert_eq!(parse_duration("33ns"), Ok(Duration::new(0, 33))); assert_eq!(parse_duration("3usec"), Ok(Duration::new(0, 3000))); assert_eq!(parse_duration("78us"), Ok(Duration::new(0, 78000))); - assert_eq!(parse_duration("31msec"), Ok(Duration::new(0, 31000000))); - assert_eq!(parse_duration("31millis"), Ok(Duration::new(0, 31000000))); - assert_eq!(parse_duration("6ms"), Ok(Duration::new(0, 6000000))); + assert_eq!(parse_duration("31msec"), Ok(Duration::new(0, 31_000_000))); + assert_eq!(parse_duration("31millis"), Ok(Duration::new(0, 31_000_000))); + assert_eq!(parse_duration("6ms"), Ok(Duration::new(0, 6_000_000))); assert_eq!(parse_duration("3000s"), Ok(Duration::new(3000, 0))); assert_eq!(parse_duration("300sec"), Ok(Duration::new(300, 0))); assert_eq!(parse_duration("300secs"), Ok(Duration::new(300, 0))); @@ -334,17 +335,17 @@ mod test { assert_eq!(parse_duration("1hour"), Ok(Duration::new(3600, 0))); assert_eq!(parse_duration("24hours"), Ok(Duration::new(86400, 0))); assert_eq!(parse_duration("1day"), Ok(Duration::new(86400, 0))); - assert_eq!(parse_duration("2days"), Ok(Duration::new(172800, 0))); - assert_eq!(parse_duration("365d"), Ok(Duration::new(31536000, 0))); - assert_eq!(parse_duration("1week"), Ok(Duration::new(604800, 0))); - assert_eq!(parse_duration("7weeks"), Ok(Duration::new(4233600, 0))); - assert_eq!(parse_duration("52w"), Ok(Duration::new(31449600, 0))); - assert_eq!(parse_duration("1month"), Ok(Duration::new(2630016, 0))); - assert_eq!(parse_duration("3months"), Ok(Duration::new(3*2630016, 0))); - assert_eq!(parse_duration("12M"), Ok(Duration::new(31560192, 0))); - assert_eq!(parse_duration("1year"), Ok(Duration::new(31557600, 0))); - assert_eq!(parse_duration("7years"), Ok(Duration::new(7*31557600, 0))); - assert_eq!(parse_duration("17y"), Ok(Duration::new(536479200, 0))); + assert_eq!(parse_duration("2days"), Ok(Duration::new(172_800, 0))); + assert_eq!(parse_duration("365d"), Ok(Duration::new(31_536_000, 0))); + assert_eq!(parse_duration("1week"), Ok(Duration::new(604_800, 0))); + assert_eq!(parse_duration("7weeks"), Ok(Duration::new(4_233_600, 0))); + assert_eq!(parse_duration("52w"), Ok(Duration::new(31_449_600, 0))); + assert_eq!(parse_duration("1month"), Ok(Duration::new(2_630_016, 0))); + assert_eq!(parse_duration("3months"), Ok(Duration::new(3*2_630_016, 0))); + assert_eq!(parse_duration("12M"), Ok(Duration::new(31_560_192, 0))); + assert_eq!(parse_duration("1year"), Ok(Duration::new(31_557_600, 0))); + assert_eq!(parse_duration("7years"), Ok(Duration::new(7*31_557_600, 0))); + assert_eq!(parse_duration("17y"), Ok(Duration::new(536_479_200, 0))); } #[test] @@ -365,7 +366,7 @@ mod test { #[test] fn random_second() { for _ in 0..10000 { - let sec = rand::thread_rng().gen_range(0, 253370764800); + let sec = rand::thread_rng().gen_range(0, 253_370_764_800); let d = Duration::new(sec, 0); assert_eq!(d, parse_duration(&format_duration(d).to_string()).unwrap()); @@ -375,7 +376,7 @@ mod test { #[test] fn random_any() { for _ in 0..10000 { - let sec = rand::thread_rng().gen_range(0, 253370764800); + let sec = rand::thread_rng().gen_range(0, 253_370_764_800); let nanos = rand::thread_rng().gen_range(0, 1_000_000_000); let d = Duration::new(sec, nanos); assert_eq!(d, diff --git a/src/lib.rs b/src/lib.rs index bce6ae9..f3a797c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,12 +22,12 @@ mod duration; mod wrapper; mod date; -pub use duration::{parse_duration, Error as DurationError}; -pub use duration::{format_duration, FormattedDuration}; -pub use wrapper::{Duration, Timestamp}; -pub use date::{parse_rfc3339, parse_rfc3339_weak, Error as TimestampError}; -pub use date::{ +pub use self::duration::{parse_duration, Error as DurationError}; +pub use self::duration::{format_duration, FormattedDuration}; +pub use self::wrapper::{Duration, Timestamp}; +pub use self::date::{parse_rfc3339, parse_rfc3339_weak, Error as TimestampError}; +pub use self::date::{ format_rfc3339, format_rfc3339_micros, format_rfc3339_millis, format_rfc3339_nanos, format_rfc3339_seconds, }; -pub use date::{Rfc3339Timestamp}; +pub use self::date::{Rfc3339Timestamp};