Fix Clippy lints: cast_lossless

Now (assuming clippy is right) all (~100) uses of ` as ` in the code are
actually doing casts that could potentially silently lose data. Woooo?

At least this means that new `as`s can be extra-scrutinized, and we should
probably be adding debug_assert!s for the casts in real code.
This commit is contained in:
Brandon W Maister 2017-10-08 18:08:40 -04:00
parent 268be10d79
commit c3fa1b5f17
10 changed files with 68 additions and 64 deletions

View File

@ -362,26 +362,30 @@ pub fn format<'a, I>(w: &mut fmt::Formatter, date: Option<&NaiveDate>, time: Opt
(d.ordinal() as i32 - d.weekday().num_days_from_monday() as i32 + 7) / 7; (d.ordinal() as i32 - d.weekday().num_days_from_monday() as i32 + 7) / 7;
let (width, v) = match spec { let (width, v) = match spec {
Year => (4, date.map(|d| d.year() as i64)), Year => (4, date.map(|d| i64::from(d.year()))),
YearDiv100 => (2, date.map(|d| div_floor(d.year() as i64, 100))), YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| mod_floor(d.year() as i64, 100))), YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
IsoYear => (4, date.map(|d| d.iso_week().year() as i64)), IsoYear => (4, date.map(|d| i64::from(d.iso_week().year()))),
IsoYearDiv100 => (2, date.map(|d| div_floor(d.iso_week().year() as i64, 100))), IsoYearDiv100 => (2, date.map(|d| div_floor(
IsoYearMod100 => (2, date.map(|d| mod_floor(d.iso_week().year() as i64, 100))), i64::from(d.iso_week().year()), 100))),
Month => (2, date.map(|d| d.month() as i64)), IsoYearMod100 => (2, date.map(|d| mod_floor(
Day => (2, date.map(|d| d.day() as i64)), i64::from(d.iso_week().year()), 100))),
WeekFromSun => (2, date.map(|d| week_from_sun(d) as i64)), Month => (2, date.map(|d| i64::from(d.month()))),
WeekFromMon => (2, date.map(|d| week_from_mon(d) as i64)), Day => (2, date.map(|d| i64::from(d.day()))),
IsoWeek => (2, date.map(|d| d.iso_week().week() as i64)), WeekFromSun => (2, date.map(|d| i64::from(week_from_sun(d)))),
NumDaysFromSun => (1, date.map(|d| d.weekday().num_days_from_sunday() as i64)), WeekFromMon => (2, date.map(|d| i64::from(week_from_mon(d)))),
WeekdayFromMon => (1, date.map(|d| d.weekday().number_from_monday() as i64)), IsoWeek => (2, date.map(|d| i64::from(d.iso_week().week()))),
Ordinal => (3, date.map(|d| d.ordinal() as i64)), NumDaysFromSun => (1, date.map(|d| i64::from(d.weekday()
Hour => (2, time.map(|t| t.hour() as i64)), .num_days_from_sunday()))),
Hour12 => (2, time.map(|t| t.hour12().1 as i64)), WeekdayFromMon => (1, date.map(|d| i64::from(d.weekday()
Minute => (2, time.map(|t| t.minute() as i64)), .number_from_monday()))),
Second => (2, time.map(|t| (t.second() + Ordinal => (3, date.map(|d| i64::from(d.ordinal()))),
t.nanosecond() / 1_000_000_000) as i64)), Hour => (2, time.map(|t| i64::from(t.hour()))),
Nanosecond => (9, time.map(|t| (t.nanosecond() % 1_000_000_000) as i64)), Hour12 => (2, time.map(|t| i64::from(t.hour12().1))),
Minute => (2, time.map(|t| i64::from(t.minute()))),
Second => (2, time.map(|t| i64::from(t.second() +
t.nanosecond() / 1_000_000_000))),
Nanosecond => (9, time.map(|t| i64::from(t.nanosecond() % 1_000_000_000))),
Timestamp => (1, match (date, time, off) { Timestamp => (1, match (date, time, off) {
(Some(d), Some(t), None) => (Some(d), Some(t), None) =>
Some(d.and_time(*t).timestamp()), Some(d.and_time(*t).timestamp()),

View File

@ -93,7 +93,7 @@ fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
s = s.trim_left(); s = s.trim_left();
try!(parsed.set_day(try_consume!(scan::number(s, 1, 2)))); try!(parsed.set_day(try_consume!(scan::number(s, 1, 2))));
s = try!(scan::space(s)); // mandatory s = try!(scan::space(s)); // mandatory
try!(parsed.set_month(1 + try_consume!(scan::short_month0(s)) as i64)); try!(parsed.set_month(1 + i64::from(try_consume!(scan::short_month0(s)))));
s = try!(scan::space(s)); // mandatory s = try!(scan::space(s)); // mandatory
// distinguish two- and three-digit years from four-digit years // distinguish two- and three-digit years from four-digit years
@ -119,7 +119,7 @@ fn parse_rfc2822<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
s = try!(scan::space(s)); // mandatory s = try!(scan::space(s)); // mandatory
if let Some(offset) = try_consume!(scan::timezone_offset_2822(s)) { if let Some(offset) = try_consume!(scan::timezone_offset_2822(s)) {
// only set the offset when it is definitely known (i.e. not `-0000`) // only set the offset when it is definitely known (i.e. not `-0000`)
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
} }
Ok((s, ())) Ok((s, ()))
@ -181,7 +181,7 @@ fn parse_rfc3339<'a>(parsed: &mut Parsed, mut s: &'a str) -> ParseResult<(&'a st
let offset = try_consume!(scan::timezone_offset_zulu(s, |s| scan::char(s, b':'))); let offset = try_consume!(scan::timezone_offset_zulu(s, |s| scan::char(s, b':')));
if offset <= -86_400 || offset >= 86_400 { return Err(OUT_OF_RANGE); } if offset <= -86_400 || offset >= 86_400 { return Err(OUT_OF_RANGE); }
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
Ok((s, ())) Ok((s, ()))
} }
@ -279,12 +279,12 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
match spec { match spec {
ShortMonthName => { ShortMonthName => {
let month0 = try_consume!(scan::short_month0(s)); let month0 = try_consume!(scan::short_month0(s));
try!(parsed.set_month(month0 as i64 + 1)); try!(parsed.set_month(i64::from(month0) + 1));
} }
LongMonthName => { LongMonthName => {
let month0 = try_consume!(scan::short_or_long_month0(s)); let month0 = try_consume!(scan::short_or_long_month0(s));
try!(parsed.set_month(month0 as i64 + 1)); try!(parsed.set_month(i64::from(month0) + 1));
} }
ShortWeekdayName => { ShortWeekdayName => {
@ -320,13 +320,13 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
TimezoneOffsetColon | TimezoneOffset => { TimezoneOffsetColon | TimezoneOffset => {
let offset = try_consume!(scan::timezone_offset(s.trim_left(), let offset = try_consume!(scan::timezone_offset(s.trim_left(),
scan::colon_or_space)); scan::colon_or_space));
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
} }
TimezoneOffsetColonZ | TimezoneOffsetZ => { TimezoneOffsetColonZ | TimezoneOffsetZ => {
let offset = try_consume!(scan::timezone_offset_zulu(s.trim_left(), let offset = try_consume!(scan::timezone_offset_zulu(s.trim_left(),
scan::colon_or_space)); scan::colon_or_space));
try!(parsed.set_offset(offset as i64)); try!(parsed.set_offset(i64::from(offset)));
} }
RFC2822 => try_consume!(parse_rfc2822(parsed, s)), RFC2822 => try_consume!(parse_rfc2822(parsed, s)),

View File

@ -394,7 +394,7 @@ impl Parsed {
if week_from_sun > 53 { return Err(OUT_OF_RANGE); } // can it overflow? if week_from_sun > 53 { return Err(OUT_OF_RANGE); } // can it overflow?
let ndays = firstweek + (week_from_sun as i32 - 1) * 7 + let ndays = firstweek + (week_from_sun as i32 - 1) * 7 +
weekday.num_days_from_sunday() as i32; weekday.num_days_from_sunday() as i32;
let date = try!(newyear.checked_add_signed(OldDuration::days(ndays as i64)) let date = try!(newyear.checked_add_signed(OldDuration::days(i64::from(ndays)))
.ok_or(OUT_OF_RANGE)); .ok_or(OUT_OF_RANGE));
if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error
@ -419,7 +419,7 @@ impl Parsed {
if week_from_mon > 53 { return Err(OUT_OF_RANGE); } // can it overflow? if week_from_mon > 53 { return Err(OUT_OF_RANGE); } // can it overflow?
let ndays = firstweek + (week_from_mon as i32 - 1) * 7 + let ndays = firstweek + (week_from_mon as i32 - 1) * 7 +
weekday.num_days_from_monday() as i32; weekday.num_days_from_monday() as i32;
let date = try!(newyear.checked_add_signed(OldDuration::days(ndays as i64)) let date = try!(newyear.checked_add_signed(OldDuration::days(i64::from(ndays)))
.ok_or(OUT_OF_RANGE)); .ok_or(OUT_OF_RANGE));
if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error if date.year() != year { return Err(OUT_OF_RANGE); } // early exit for correct error
@ -502,7 +502,7 @@ impl Parsed {
// verify the timestamp field if any // verify the timestamp field if any
// the following is safe, `timestamp` is very limited in range // the following is safe, `timestamp` is very limited in range
let timestamp = datetime.timestamp() - offset as i64; let timestamp = datetime.timestamp() - i64::from(offset);
if let Some(given_timestamp) = self.timestamp { if let Some(given_timestamp) = self.timestamp {
// if `datetime` represents a leap second, it might be off by one second. // if `datetime` represents a leap second, it might be off by one second.
if given_timestamp != timestamp && if given_timestamp != timestamp &&
@ -525,7 +525,7 @@ impl Parsed {
} }
// reconstruct date and time fields from timestamp // reconstruct date and time fields from timestamp
let ts = try!(timestamp.checked_add(offset as i64).ok_or(OUT_OF_RANGE)); let ts = try!(timestamp.checked_add(i64::from(offset)).ok_or(OUT_OF_RANGE));
let datetime = NaiveDateTime::from_timestamp_opt(ts, 0); let datetime = NaiveDateTime::from_timestamp_opt(ts, 0);
let mut datetime = try!(datetime.ok_or(OUT_OF_RANGE)); let mut datetime = try!(datetime.ok_or(OUT_OF_RANGE));
@ -544,12 +544,12 @@ impl Parsed {
} }
// ...and we have the correct candidates for other fields. // ...and we have the correct candidates for other fields.
} else { } else {
try!(parsed.set_second(datetime.second() as i64)); try!(parsed.set_second(i64::from(datetime.second())));
} }
try!(parsed.set_year (datetime.year() as i64)); try!(parsed.set_year (i64::from(datetime.year())));
try!(parsed.set_ordinal(datetime.ordinal() as i64)); // more efficient than ymd try!(parsed.set_ordinal(i64::from(datetime.ordinal()))); // more efficient than ymd
try!(parsed.set_hour (datetime.hour() as i64)); try!(parsed.set_hour (i64::from(datetime.hour())));
try!(parsed.set_minute (datetime.minute() as i64)); try!(parsed.set_minute (i64::from(datetime.minute())));
// validate other fields (e.g. week) and return // validate other fields (e.g. week) and return
let date = try!(parsed.to_naive_date()); let date = try!(parsed.to_naive_date());

View File

@ -191,7 +191,7 @@ pub fn timezone_offset<F>(mut s: &str, mut colon: F) -> ParseResult<(&str, i32)>
// hours (00--99) // hours (00--99)
let hours = match try!(digits(s)) { let hours = match try!(digits(s)) {
(h1 @ b'0'...b'9', h2 @ b'0'...b'9') => ((h1 - b'0') * 10 + (h2 - b'0')) as i32, (h1 @ b'0'...b'9', h2 @ b'0'...b'9') => i32::from((h1 - b'0') * 10 + (h2 - b'0')),
_ => return Err(INVALID), _ => return Err(INVALID),
}; };
s = &s[2..]; s = &s[2..];
@ -201,7 +201,7 @@ pub fn timezone_offset<F>(mut s: &str, mut colon: F) -> ParseResult<(&str, i32)>
// minutes (00--59) // minutes (00--59)
let minutes = match try!(digits(s)) { let minutes = match try!(digits(s)) {
(m1 @ b'0'...b'5', m2 @ b'0'...b'9') => ((m1 - b'0') * 10 + (m2 - b'0')) as i32, (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), (b'6'...b'9', b'0'...b'9') => return Err(OUT_OF_RANGE),
_ => return Err(INVALID), _ => return Err(INVALID),
}; };

View File

@ -883,9 +883,9 @@ impl NaiveDate {
let year2 = rhs.year(); let year2 = rhs.year();
let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400); let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400);
let (year2_div_400, year2_mod_400) = div_mod_floor(year2, 400); let (year2_div_400, year2_mod_400) = div_mod_floor(year2, 400);
let cycle1 = internals::yo_to_cycle(year1_mod_400 as u32, self.of().ordinal()) as i64; let cycle1 = i64::from(internals::yo_to_cycle(year1_mod_400 as u32, self.of().ordinal()));
let cycle2 = internals::yo_to_cycle(year2_mod_400 as u32, rhs.of().ordinal()) as i64; let cycle2 = i64::from(internals::yo_to_cycle(year2_mod_400 as u32, rhs.of().ordinal()));
OldDuration::days((year1_div_400 as i64 - year2_div_400 as i64) * 146_097 + OldDuration::days((i64::from(year1_div_400) - i64::from(year2_div_400)) * 146_097 +
(cycle1 - cycle2)) (cycle1 - cycle2))
} }

View File

@ -258,8 +258,8 @@ impl NaiveDateTime {
/// ~~~~ /// ~~~~
#[inline] #[inline]
pub fn timestamp(&self) -> i64 { pub fn timestamp(&self) -> i64 {
let ndays = self.date.num_days_from_ce() as i64; let ndays = i64::from(self.date.num_days_from_ce());
let nseconds = self.time.num_seconds_from_midnight() as i64; let nseconds = i64::from(self.time.num_seconds_from_midnight());
(ndays - 719_163) * 86_400 + nseconds (ndays - 719_163) * 86_400 + nseconds
} }
@ -287,7 +287,7 @@ impl NaiveDateTime {
#[inline] #[inline]
pub fn timestamp_millis(&self) -> i64 { pub fn timestamp_millis(&self) -> i64 {
let as_ms = self.timestamp() * 1000; let as_ms = self.timestamp() * 1000;
as_ms + self.timestamp_subsec_millis() as i64 as_ms + i64::from(self.timestamp_subsec_millis())
} }
/// Returns the number of milliseconds since the last whole non-leap second. /// Returns the number of milliseconds since the last whole non-leap second.

View File

@ -93,10 +93,10 @@ static YEAR_DELTAS: [u8; 401] = [
pub fn cycle_to_yo(cycle: u32) -> (u32, u32) { pub fn cycle_to_yo(cycle: u32) -> (u32, u32) {
let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365); let (mut year_mod_400, mut ordinal0) = div_rem(cycle, 365);
let delta = YEAR_DELTAS[year_mod_400 as usize] as u32; let delta = u32::from(YEAR_DELTAS[year_mod_400 as usize]);
if ordinal0 < delta { if ordinal0 < delta {
year_mod_400 -= 1; year_mod_400 -= 1;
ordinal0 += 365 - YEAR_DELTAS[year_mod_400 as usize] as u32; ordinal0 += 365 - u32::from(YEAR_DELTAS[year_mod_400 as usize]);
} else { } else {
ordinal0 -= delta; ordinal0 -= delta;
} }
@ -104,7 +104,7 @@ pub fn cycle_to_yo(cycle: u32) -> (u32, u32) {
} }
pub fn yo_to_cycle(year_mod_400: u32, ordinal: u32) -> u32 { pub fn yo_to_cycle(year_mod_400: u32, ordinal: u32) -> u32 {
year_mod_400 * 365 + YEAR_DELTAS[year_mod_400 as usize] as u32 + ordinal - 1 year_mod_400 * 365 + u32::from(YEAR_DELTAS[year_mod_400 as usize]) + ordinal - 1
} }
impl YearFlags { impl YearFlags {
@ -122,13 +122,13 @@ impl YearFlags {
#[inline] #[inline]
pub fn ndays(&self) -> u32 { pub fn ndays(&self) -> u32 {
let YearFlags(flags) = *self; let YearFlags(flags) = *self;
366 - (flags >> 3) as u32 366 - u32::from(flags >> 3)
} }
#[inline] #[inline]
pub fn isoweek_delta(&self) -> u32 { pub fn isoweek_delta(&self) -> u32 {
let YearFlags(flags) = *self; let YearFlags(flags) = *self;
let mut delta = flags as u32 & 0b111; let mut delta = u32::from(flags) & 0b0111;
if delta < 3 { delta += 7; } if delta < 3 { delta += 7; }
delta delta
} }
@ -286,14 +286,14 @@ impl Of {
#[inline] #[inline]
pub fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Of { pub fn new(ordinal: u32, YearFlags(flags): YearFlags) -> Of {
let ordinal = Of::clamp_ordinal(ordinal); let ordinal = Of::clamp_ordinal(ordinal);
Of((ordinal << 4) | (flags as u32)) Of((ordinal << 4) | u32::from(flags))
} }
#[inline] #[inline]
pub fn from_mdf(Mdf(mdf): Mdf) -> Of { pub fn from_mdf(Mdf(mdf): Mdf) -> Of {
let mdl = mdf >> 3; let mdl = mdf >> 3;
match MDL_TO_OL.get(mdl as usize) { match MDL_TO_OL.get(mdl as usize) {
Some(&v) => Of(mdf.wrapping_sub((v as i32 as u32 & 0x3ff) << 3)), Some(&v) => Of(mdf.wrapping_sub((i32::from(v) as u32 & 0x3ff) << 3)),
None => Of(0) None => Of(0)
} }
} }
@ -327,7 +327,7 @@ impl Of {
#[inline] #[inline]
pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Of { pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Of {
let Of(of) = *self; let Of(of) = *self;
Of((of & !0b1111) | (flags as u32)) Of((of & !0b1111) | u32::from(flags))
} }
#[inline] #[inline]
@ -393,14 +393,14 @@ impl Mdf {
pub fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Mdf { pub fn new(month: u32, day: u32, YearFlags(flags): YearFlags) -> Mdf {
let month = Mdf::clamp_month(month); let month = Mdf::clamp_month(month);
let day = Mdf::clamp_day(day); let day = Mdf::clamp_day(day);
Mdf((month << 9) | (day << 4) | (flags as u32)) Mdf((month << 9) | (day << 4) | u32::from(flags))
} }
#[inline] #[inline]
pub fn from_of(Of(of): Of) -> Mdf { pub fn from_of(Of(of): Of) -> Mdf {
let ol = of >> 3; let ol = of >> 3;
match OL_TO_MDL.get(ol as usize) { match OL_TO_MDL.get(ol as usize) {
Some(&v) => Mdf(of + ((v as u32) << 3)), Some(&v) => Mdf(of + (u32::from(v) << 3)),
None => Mdf(0) None => Mdf(0)
} }
} }
@ -450,7 +450,7 @@ impl Mdf {
#[inline] #[inline]
pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf { pub fn with_flags(&self, YearFlags(flags): YearFlags) -> Mdf {
let Mdf(mdf) = *self; let Mdf(mdf) = *self;
Mdf((mdf & !0b1111) | (flags as u32)) Mdf((mdf & !0b1111) | u32::from(flags))
} }
#[inline] #[inline]

View File

@ -40,7 +40,7 @@ pub fn iso_week_from_yof(year: i32, of: Of) -> IsoWeek {
(year, rawweek) (year, rawweek)
} }
}; };
IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | of.flags().0 as DateImpl } IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | DateImpl::from(of.flags().0) }
} }
impl IsoWeek { impl IsoWeek {

View File

@ -528,15 +528,15 @@ impl NaiveTime {
// otherwise the addition immediately finishes. // otherwise the addition immediately finishes.
if frac >= 1_000_000_000 { if frac >= 1_000_000_000 {
let rfrac = 2_000_000_000 - frac; let rfrac = 2_000_000_000 - frac;
if rhs >= OldDuration::nanoseconds(rfrac as i64) { if rhs >= OldDuration::nanoseconds(i64::from(rfrac)) {
rhs = rhs - OldDuration::nanoseconds(rfrac as i64); rhs = rhs - OldDuration::nanoseconds(i64::from(rfrac));
secs += 1; secs += 1;
frac = 0; frac = 0;
} else if rhs < OldDuration::nanoseconds(-(frac as i64)) { } else if rhs < OldDuration::nanoseconds(-i64::from(frac)) {
rhs = rhs + OldDuration::nanoseconds(frac as i64); rhs = rhs + OldDuration::nanoseconds(i64::from(frac));
frac = 0; frac = 0;
} else { } else {
frac = (frac as i64 + rhs.num_nanoseconds().unwrap()) as u32; frac = (i64::from(frac) + rhs.num_nanoseconds().unwrap()) as u32;
debug_assert!(frac < 2_000_000_000); debug_assert!(frac < 2_000_000_000);
return (NaiveTime { secs: secs, frac: frac }, 0); return (NaiveTime { secs: secs, frac: frac }, 0);
} }
@ -683,8 +683,8 @@ impl NaiveTime {
use std::cmp::Ordering; use std::cmp::Ordering;
let secs = self.secs as i64 - rhs.secs as i64; let secs = i64::from(self.secs) - i64::from(rhs.secs);
let frac = self.frac as i64 - rhs.frac as i64; let frac = i64::from(self.frac) - i64::from(rhs.frac);
// `secs` may contain a leap second yet to be counted // `secs` may contain a leap second yet to be counted
let adjust = match self.secs.cmp(&rhs.secs) { let adjust = match self.secs.cmp(&rhs.secs) {

View File

@ -145,7 +145,7 @@ fn add_with_leapsecond<T>(lhs: &T, rhs: i32) -> T
// extract and temporarily remove the fractional part and later recover it // extract and temporarily remove the fractional part and later recover it
let nanos = lhs.nanosecond(); let nanos = lhs.nanosecond();
let lhs = lhs.with_nanosecond(0).unwrap(); let lhs = lhs.with_nanosecond(0).unwrap();
(lhs + OldDuration::seconds(rhs as i64)).with_nanosecond(nanos).unwrap() (lhs + OldDuration::seconds(i64::from(rhs))).with_nanosecond(nanos).unwrap()
} }
impl Add<FixedOffset> for NaiveTime { impl Add<FixedOffset> for NaiveTime {