Replace as casts with type-conversion functions.

https://github.com/rust-lang-nursery/rust-clippy/wiki#cast_lossless
This commit is contained in:
Dan Gohman
2017-07-13 16:37:07 -07:00
parent af74cdf364
commit 0cacba15b9
12 changed files with 42 additions and 34 deletions

View File

@@ -171,8 +171,8 @@ U = EncRecipe(
UJ = EncRecipe( UJ = EncRecipe(
'UJ', Jump, size=4, ins=(), outs=(), branch_range=(0, 21), 'UJ', Jump, size=4, ins=(), outs=(), branch_range=(0, 21),
emit=''' emit='''
let dest = func.offsets[destination] as i64; let dest = i64::from(func.offsets[destination]);
let disp = dest - sink.offset() as i64; let disp = dest - i64::from(sink.offset());
put_uj(bits, disp, 0, sink); put_uj(bits, disp, 0, sink);
''') ''')
@@ -190,8 +190,8 @@ SB = EncRecipe(
ins=(GPR, GPR), outs=(), ins=(GPR, GPR), outs=(),
branch_range=(0, 13), branch_range=(0, 13),
emit=''' emit='''
let dest = func.offsets[destination] as i64; let dest = i64::from(func.offsets[destination]);
let disp = dest - sink.offset() as i64; let disp = dest - i64::from(sink.offset());
put_sb(bits, disp, in_reg0, in_reg1, sink); put_sb(bits, disp, in_reg0, in_reg1, sink);
''') ''')
@@ -201,8 +201,8 @@ SBzero = EncRecipe(
ins=(GPR), outs=(), ins=(GPR), outs=(),
branch_range=(0, 13), branch_range=(0, 13),
emit=''' emit='''
let dest = func.offsets[destination] as i64; let dest = i64::from(func.offsets[destination]);
let disp = dest - sink.offset() as i64; let disp = dest - i64::from(sink.offset());
put_sb(bits, disp, in_reg0, 0, sink); put_sb(bits, disp, in_reg0, 0, sink);
''') ''')

View File

@@ -87,7 +87,7 @@ fn parse_i64(s: &str) -> Result<i64, &'static str> {
return Err("Too many hexadecimal digits"); return Err("Too many hexadecimal digits");
} }
// This can't overflow given the digit limit. // This can't overflow given the digit limit.
value = (value << 4) | digit as u64; value = (value << 4) | u64::from(digit);
} }
None => { None => {
// Allow embedded underscores, but fail on anything else. // Allow embedded underscores, but fail on anything else.
@@ -107,7 +107,7 @@ fn parse_i64(s: &str) -> Result<i64, &'static str> {
None => return Err("Too large decimal number"), None => return Err("Too large decimal number"),
Some(v) => value = v, Some(v) => value = v,
} }
match value.checked_add(digit as u64) { match value.checked_add(u64::from(digit)) {
None => return Err("Too large decimal number"), None => return Err("Too large decimal number"),
Some(v) => value = v, Some(v) => value = v,
} }
@@ -221,7 +221,7 @@ impl Into<i32> for Offset32 {
impl Into<i64> for Offset32 { impl Into<i64> for Offset32 {
fn into(self) -> i64 { fn into(self) -> i64 {
self.0 as i64 i64::from(self.0)
} }
} }
@@ -241,7 +241,7 @@ impl Display for Offset32 {
// Always include a sign. // Always include a sign.
write!(f, "{}", if self.0 < 0 { '-' } else { '+' })?; write!(f, "{}", if self.0 < 0 { '-' } else { '+' })?;
let val = (self.0 as i64).abs(); let val = i64::from(self.0).abs();
if val < 10_000 { if val < 10_000 {
write!(f, "{}", val) write!(f, "{}", val)
} else { } else {
@@ -259,7 +259,9 @@ impl FromStr for Offset32 {
if !(s.starts_with('-') || s.starts_with('+')) { if !(s.starts_with('-') || s.starts_with('+')) {
return Err("Offset must begin with sign"); return Err("Offset must begin with sign");
} }
parse_i64(s).and_then(|x| if i32::MIN as i64 <= x && x <= i32::MAX as i64 { parse_i64(s).and_then(|x| if i64::from(i32::MIN) as i64 <= x &&
x <= i64::from(i32::MAX)
{
Ok(Offset32::new(x as i32)) Ok(Offset32::new(x as i32))
} else { } else {
Err("Offset out of range") Err("Offset out of range")
@@ -288,7 +290,7 @@ impl Into<u32> for Uoffset32 {
impl Into<i64> for Uoffset32 { impl Into<i64> for Uoffset32 {
fn into(self) -> i64 { fn into(self) -> i64 {
self.0 as i64 i64::from(self.0)
} }
} }
@@ -310,7 +312,7 @@ impl Display for Uoffset32 {
write!(f, "+{}", self.0) write!(f, "+{}", self.0)
} else { } else {
write!(f, "+")?; write!(f, "+")?;
write_hex(self.0 as i64, f) write_hex(i64::from(self.0), f)
} }
} }
@@ -324,7 +326,7 @@ impl FromStr for Uoffset32 {
if !s.starts_with('+') { if !s.starts_with('+') {
return Err("Unsigned offset must begin with '+' sign"); return Err("Unsigned offset must begin with '+' sign");
} }
parse_i64(s).and_then(|x| if 0 <= x && x <= u32::MAX as i64 { parse_i64(s).and_then(|x| if 0 <= x && x <= i64::from(u32::MAX) {
Ok(Uoffset32::new(x as u32)) Ok(Uoffset32::new(x as u32))
} else { } else {
Err("Offset out of range") Err("Offset out of range")
@@ -386,7 +388,13 @@ fn format_float(bits: u64, w: u8, t: u8, f: &mut Formatter) -> fmt::Result {
write!(f, "0.0") write!(f, "0.0")
} else { } else {
// Subnormal. // Subnormal.
write!(f, "0x0.{0:01$x}p{2}", left_t_bits, digits as usize, emin) write!(
f,
"0x0.{0:01$x}p{2}",
left_t_bits,
usize::from(digits),
emin
)
} }
} else if e_bits == max_e_bits { } else if e_bits == max_e_bits {
// Always print a `+` or `-` sign for these special values. // Always print a `+` or `-` sign for these special values.
@@ -414,7 +422,7 @@ fn format_float(bits: u64, w: u8, t: u8, f: &mut Formatter) -> fmt::Result {
} }
} else { } else {
// Normal number. // Normal number.
write!(f, "0x1.{0:01$x}p{2}", left_t_bits, digits as usize, e) write!(f, "0x1.{0:01$x}p{2}", left_t_bits, usize::from(digits), e)
} }
} }
@@ -511,7 +519,7 @@ fn parse_float(s: &str, w: u8, t: u8) -> Result<u64, &'static str> {
if digits > 16 { if digits > 16 {
return Err("Too many digits"); return Err("Too many digits");
} }
significand = (significand << 4) | digit as u64; significand = (significand << 4) | u64::from(digit);
} }
None => return Err("Invalid character"), None => return Err("Invalid character"),
} }
@@ -596,7 +604,7 @@ impl Ieee32 {
impl Display for Ieee32 { impl Display for Ieee32 {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let bits: u32 = self.0; let bits: u32 = self.0;
format_float(bits as u64, 8, 23, f) format_float(u64::from(bits), 8, 23, f)
} }
} }

View File

@@ -463,7 +463,7 @@ impl OpcodeConstraints {
/// Get the offset into `TYPE_SETS` for the controlling type variable. /// Get the offset into `TYPE_SETS` for the controlling type variable.
/// Returns `None` if the instruction is not polymorphic. /// Returns `None` if the instruction is not polymorphic.
fn typeset_offset(self) -> Option<usize> { fn typeset_offset(self) -> Option<usize> {
let offset = self.typeset_offset as usize; let offset = usize::from(self.typeset_offset);
if offset < TYPE_SETS.len() { if offset < TYPE_SETS.len() {
Some(offset) Some(offset)
} else { } else {

View File

@@ -194,12 +194,12 @@ impl Type {
/// Get the total number of bits used to represent this type. /// Get the total number of bits used to represent this type.
pub fn bits(self) -> u16 { pub fn bits(self) -> u16 {
self.lane_bits() as u16 * self.lane_count() u16::from(self.lane_bits()) * self.lane_count()
} }
/// Get the number of bytes used to store this type in memory. /// Get the number of bytes used to store this type in memory.
pub fn bytes(self) -> u32 { pub fn bytes(self) -> u32 {
(self.bits() as u32 + 7) / 8 (u32::from(self.bits()) + 7) / 8
} }
/// Get a SIMD vector type with `n` times more lanes than this one. /// Get a SIMD vector type with `n` times more lanes than this one.
@@ -213,7 +213,7 @@ impl Type {
return None; return None;
} }
let log2_lanes: u32 = n.trailing_zeros(); let log2_lanes: u32 = n.trailing_zeros();
let new_type = self.0 as u32 + (log2_lanes << 4); let new_type = u32::from(self.0) + (log2_lanes << 4);
if new_type < 0x90 { if new_type < 0x90 {
Some(Type(new_type as u8)) Some(Type(new_type as u8))
} else { } else {
@@ -234,7 +234,7 @@ impl Type {
/// Index of this type, for use with hash tables etc. /// Index of this type, for use with hash tables etc.
pub fn index(self) -> usize { pub fn index(self) -> usize {
self.0 as usize usize::from(self.0)
} }
/// True iff: /// True iff:

View File

@@ -105,7 +105,7 @@ impl BranchRange {
/// ///
/// This method does not detect if the range is larger than 2 GB. /// This method does not detect if the range is larger than 2 GB.
pub fn contains(self, branch: CodeOffset, dest: CodeOffset) -> bool { pub fn contains(self, branch: CodeOffset, dest: CodeOffset) -> bool {
let d = dest.wrapping_sub(branch + self.origin as CodeOffset) as i32; let d = dest.wrapping_sub(branch + CodeOffset::from(self.origin)) as i32;
let s = 32 - self.bits; let s = 32 - self.bits;
d == d << s >> s d == d << s >> s
} }

View File

@@ -124,7 +124,7 @@ impl EncInfo {
pub fn bytes(&self, enc: Encoding) -> CodeOffset { pub fn bytes(&self, enc: Encoding) -> CodeOffset {
self.sizing self.sizing
.get(enc.recipe()) .get(enc.recipe())
.map(|s| s.bytes as CodeOffset) .map(|s| CodeOffset::from(s.bytes))
.unwrap_or(0) .unwrap_or(0)
} }

View File

@@ -28,7 +28,7 @@ struct Args {
impl Args { impl Args {
fn new(bits: u16, gpr: &'static [RU], fpr_limit: usize) -> Args { fn new(bits: u16, gpr: &'static [RU], fpr_limit: usize) -> Args {
Args { Args {
pointer_bytes: bits as u32 / 8, pointer_bytes: u32::from(bits) / 8,
pointer_bits: bits, pointer_bits: bits,
pointer_type: ir::Type::int(bits).unwrap(), pointer_type: ir::Type::int(bits).unwrap(),
gpr, gpr,

View File

@@ -174,7 +174,7 @@ impl RegClassData {
/// Get a specific register unit in this class. /// Get a specific register unit in this class.
pub fn unit(&self, offset: usize) -> RegUnit { pub fn unit(&self, offset: usize) -> RegUnit {
let uoffset = offset * self.width as usize; let uoffset = offset * usize::from(self.width);
self.first + uoffset as RegUnit self.first + uoffset as RegUnit
} }
@@ -209,7 +209,7 @@ impl EntityRef for RegClassIndex {
} }
fn index(self) -> usize { fn index(self) -> usize {
self.0 as usize usize::from(self.0)
} }
} }

View File

@@ -26,7 +26,7 @@ impl Args {
fn new(bits: u16, enable_e: bool) -> Args { fn new(bits: u16, enable_e: bool) -> Args {
Args { Args {
pointer_bits: bits, pointer_bits: bits,
pointer_bytes: bits as u32 / 8, pointer_bytes: u32::from(bits) / 8,
pointer_type: Type::int(bits).unwrap(), pointer_type: Type::int(bits).unwrap(),
regs: 0, regs: 0,
reg_limit: if enable_e { 6 } else { 8 }, reg_limit: if enable_e { 6 } else { 8 },

View File

@@ -142,8 +142,8 @@ impl Pressure {
/// ///
/// This is the out-of-line slow path for `check_avail()`. /// This is the out-of-line slow path for `check_avail()`.
fn check_avail_aliased(&self, entry: &TopRC) -> RegClassMask { fn check_avail_aliased(&self, entry: &TopRC) -> RegClassMask {
let first = entry.first_toprc as usize; let first = usize::from(entry.first_toprc);
let num = entry.num_toprcs as usize; let num = usize::from(entry.num_toprcs);
let width = entry.width as u32; let width = entry.width as u32;
let ulimit = entry.limit * width; let ulimit = entry.limit * width;

View File

@@ -212,7 +212,7 @@ pub mod detail {
/// Get enumerators corresponding to a `Details::Enum`. /// Get enumerators corresponding to a `Details::Enum`.
pub fn enums(&self, last: u8, enumerators: u16) -> &[&'static str] { pub fn enums(&self, last: u8, enumerators: u16) -> &[&'static str] {
let from = enumerators as usize; let from = enumerators as usize;
let len = last as usize + 1; let len = usize::from(last) + 1;
&self.enumerators[from..from + len] &self.enumerators[from..from + len]
} }
@@ -230,7 +230,7 @@ pub mod detail {
Detail::Enum { last, enumerators } => { Detail::Enum { last, enumerators } => {
if byte <= last { if byte <= last {
let tags = self.enums(last, enumerators); let tags = self.enums(last, enumerators);
write!(f, "\"{}\"", tags[byte as usize]) write!(f, "\"{}\"", tags[usize::from(byte)])
} else { } else {
write!(f, "{}", byte) write!(f, "{}", byte)
} }

View File

@@ -1115,7 +1115,7 @@ impl<'a> Parser<'a> {
if bytes < 0 { if bytes < 0 {
return err!(self.loc, "negative stack slot size"); return err!(self.loc, "negative stack slot size");
} }
if bytes > u32::MAX as i64 { if bytes > i64::from(u32::MAX) {
return err!(self.loc, "stack slot too large"); return err!(self.loc, "stack slot too large");
} }
let mut data = StackSlotData::new(kind, bytes as u32); let mut data = StackSlotData::new(kind, bytes as u32);