Merge pull request #220 from Robzz/cast_iusize_improvements

Casts : architecture independent handling of *size types
This commit is contained in:
llogiq 2015-08-24 10:49:01 +02:00
commit 45533bb03e
4 changed files with 139 additions and 70 deletions

View File

@ -12,6 +12,7 @@ approx_constant | warn | the approximate of a known float constant (
bad_bit_mask | deny | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
box_vec | warn | usage of `Box<Vec<T>>`, vector elements are already on the heap
cast_possible_truncation | allow | casts that may cause truncation of the value, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`
cast_possible_wrap | allow | casts that may cause wrapping around the value, e.g `x as i32` where `x: u32` and `x > i32::MAX`
cast_precision_loss | allow | casts that cause loss of precision, e.g `x as f32` where `x: u64`
cast_sign_loss | allow | casts from signed types to unsigned types, e.g `x as u32` where `x: i32`
cmp_nan | deny | comparisons to NAN (which will always return false, which is probably not intended)

View File

@ -110,6 +110,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
strings::STRING_ADD_ASSIGN,
types::BOX_VEC,
types::CAST_POSSIBLE_TRUNCATION,
types::CAST_POSSIBLE_WRAP,
types::CAST_PRECISION_LOSS,
types::CAST_SIGN_LOSS,
types::LET_UNIT_VALUE,

View File

@ -112,6 +112,8 @@ declare_lint!(pub CAST_SIGN_LOSS, Allow,
"casts from signed types to unsigned types, e.g `x as u32` where `x: i32`");
declare_lint!(pub CAST_POSSIBLE_TRUNCATION, Allow,
"casts that may cause truncation of the value, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`");
declare_lint!(pub CAST_POSSIBLE_WRAP, Allow,
"casts that may cause wrapping around the value, e.g `x as i32` where `x: u32` and `x > i32::MAX`");
/// Returns the size in bits of an integral type.
/// Will return 0 if the type is not an int or uint variant
@ -125,11 +127,85 @@ fn int_ty_to_nbits(typ: &ty::TyS) -> usize {
if n == 4 { ::std::usize::BITS } else { n }
}
fn is_isize_or_usize(typ: &ty::TyS) -> bool {
match typ.sty {
ty::TyInt(ast::TyIs) | ty::TyUint(ast::TyUs) => true,
_ => false
}
}
fn span_precision_loss_lint(cx: &Context, expr: &Expr, cast_from: &ty::TyS, cast_to_f64: bool) {
let mantissa_nbits = if cast_to_f64 {52} else {23};
let arch_dependent = is_isize_or_usize(cast_from) && cast_to_f64;
let arch_dependent_str = "on targets with 64-bit wide pointers ";
let from_nbits_str = if arch_dependent {"64".to_owned()}
else if is_isize_or_usize(cast_from) {"32 or 64".to_owned()}
else {int_ty_to_nbits(cast_from).to_string()};
span_lint(cx, CAST_PRECISION_LOSS, expr.span,
&format!("casting {0} to {1} causes a loss of precision {2}\
({0} is {3} bits wide, but {1}'s mantissa is only {4} bits wide)",
cast_from, if cast_to_f64 {"f64"} else {"f32"},
if arch_dependent {arch_dependent_str} else {""},
from_nbits_str,
mantissa_nbits));
}
enum ArchSuffix {
_32, _64, None
}
fn check_truncation_and_wrapping(cx: &Context, expr: &Expr, cast_from: &ty::TyS, cast_to: &ty::TyS) {
let arch_64_suffix = " on targets with 64-bit wide pointers";
let arch_32_suffix = " on targets with 32-bit wide pointers";
let cast_unsigned_to_signed = !cast_from.is_signed() && cast_to.is_signed();
let (from_nbits, to_nbits) = (int_ty_to_nbits(cast_from), int_ty_to_nbits(cast_to));
let (span_truncation, suffix_truncation, span_wrap, suffix_wrap) =
match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) {
(true, true) | (false, false) => (
to_nbits < from_nbits,
ArchSuffix::None,
to_nbits == from_nbits && cast_unsigned_to_signed,
ArchSuffix::None
),
(true, false) => (
to_nbits <= 32,
if to_nbits == 32 {ArchSuffix::_64} else {ArchSuffix::None},
to_nbits <= 32 && cast_unsigned_to_signed,
ArchSuffix::_32
),
(false, true) => (
from_nbits == 64,
ArchSuffix::_32,
cast_unsigned_to_signed,
if from_nbits == 64 {ArchSuffix::_64} else {ArchSuffix::_32}
),
};
if span_truncation {
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span,
&format!("casting {} to {} may truncate the value{}",
cast_from, cast_to,
match suffix_truncation {
ArchSuffix::_32 => arch_32_suffix,
ArchSuffix::_64 => arch_64_suffix,
ArchSuffix::None => "" }));
}
if span_wrap {
span_lint(cx, CAST_POSSIBLE_WRAP, expr.span,
&format!("casting {} to {} may wrap around the value{}",
cast_from, cast_to,
match suffix_wrap {
ArchSuffix::_32 => arch_32_suffix,
ArchSuffix::_64 => arch_64_suffix,
ArchSuffix::None => "" }));
}
}
impl LintPass for CastPass {
fn get_lints(&self) -> LintArray {
lint_array!(CAST_PRECISION_LOSS,
CAST_SIGN_LOSS,
CAST_POSSIBLE_TRUNCATION)
CAST_POSSIBLE_TRUNCATION,
CAST_POSSIBLE_WRAP)
}
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
@ -139,45 +215,30 @@ impl LintPass for CastPass {
match (cast_from.is_integral(), cast_to.is_integral()) {
(true, false) => {
let from_nbits = int_ty_to_nbits(cast_from);
let to_nbits : usize = match cast_to.sty {
ty::TyFloat(ast::TyF32) => 32,
ty::TyFloat(ast::TyF64) => 64,
_ => 0
};
if from_nbits != 0 {
if from_nbits >= to_nbits {
span_lint(cx, CAST_PRECISION_LOSS, expr.span,
&format!("converting from {0} to {1}, which causes a loss of precision \
({0} is {2} bits wide, but {1}'s mantissa is only {3} bits wide)",
cast_from, cast_to, from_nbits, if to_nbits == 64 {52} else {23} ));
}
let to_nbits = if let ty::TyFloat(ast::TyF32) = cast_to.sty {32} else {64};
if is_isize_or_usize(cast_from) || from_nbits >= to_nbits {
span_precision_loss_lint(cx, expr, cast_from, to_nbits == 64);
}
},
(false, true) => {
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span,
&format!("casting {} to {} may cause truncation of the value", cast_from, cast_to));
&format!("casting {} to {} may truncate the value", cast_from, cast_to));
if !cast_to.is_signed() {
span_lint(cx, CAST_SIGN_LOSS, expr.span,
&format!("casting from {} to {} loses the sign of the value", cast_from, cast_to));
&format!("casting {} to {} may lose the sign of the value", cast_from, cast_to));
}
},
(true, true) => {
if cast_from.is_signed() && !cast_to.is_signed() {
span_lint(cx, CAST_SIGN_LOSS, expr.span,
&format!("casting from {} to {} loses the sign of the value", cast_from, cast_to));
}
let from_nbits = int_ty_to_nbits(cast_from);
let to_nbits = int_ty_to_nbits(cast_to);
if to_nbits < from_nbits ||
(!cast_from.is_signed() && cast_to.is_signed() && to_nbits <= from_nbits) {
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span,
&format!("casting {} to {} may cause truncation of the value", cast_from, cast_to));
&format!("casting {} to {} may lose the sign of the value", cast_from, cast_to));
}
check_truncation_and_wrapping(cx, expr, cast_from, cast_to);
}
(false, false) => {
if let (&ty::TyFloat(ast::TyF64),
&ty::TyFloat(ast::TyF32)) = (&cast_from.sty, &cast_to.sty) {
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, "casting f64 to f32 may cause truncation of the value");
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, "casting f64 to f32 may truncate the value");
}
}
}

View File

@ -1,57 +1,63 @@
#![feature(plugin)]
#![plugin(clippy)]
#[deny(cast_precision_loss, cast_possible_truncation, cast_sign_loss)]
#[allow(dead_code)]
#[deny(cast_precision_loss, cast_possible_truncation, cast_sign_loss, cast_possible_wrap)]
fn main() {
let i : i32 = 42;
let u : u32 = 42;
let f : f32 = 42.0;
// Test cast_precision_loss
i as f32; //~ERROR converting from i32 to f32, which causes a loss of precision (i32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
(i as i64) as f32; //~ERROR converting from i64 to f32, which causes a loss of precision (i64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
(i as i64) as f64; //~ERROR converting from i64 to f64, which causes a loss of precision (i64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
u as f32; //~ERROR converting from u32 to f32, which causes a loss of precision (u32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
(u as u64) as f32; //~ERROR converting from u64 to f32, which causes a loss of precision (u64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
(u as u64) as f64; //~ERROR converting from u64 to f64, which causes a loss of precision (u64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
i as f64; // Should not trigger the lint
u as f64; // Should not trigger the lint
1i32 as f32; //~ERROR casting i32 to f32 causes a loss of precision (i32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
1i64 as f32; //~ERROR casting i64 to f32 causes a loss of precision (i64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
1i64 as f64; //~ERROR casting i64 to f64 causes a loss of precision (i64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
1u32 as f32; //~ERROR casting u32 to f32 causes a loss of precision (u32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
1u64 as f32; //~ERROR casting u64 to f32 causes a loss of precision (u64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
1u64 as f64; //~ERROR casting u64 to f64 causes a loss of precision (u64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
1i32 as f64; // Should not trigger the lint
1u32 as f64; // Should not trigger the lint
// Test cast_possible_truncation
f as i32; //~ERROR casting f32 to i32 may cause truncation of the value
f as u32; //~ERROR casting f32 to u32 may cause truncation of the value
//~^ERROR casting from f32 to u32 loses the sign of the value
i as u8; //~ERROR casting i32 to u8 may cause truncation of the value
//~^ERROR casting from i32 to u8 loses the sign of the value
(f as f64) as f32; //~ERROR casting f64 to f32 may cause truncation of the value
i as i8; //~ERROR casting i32 to i8 may cause truncation of the value
u as i32; //~ERROR casting u32 to i32 may cause truncation of the value
1f32 as i32; //~ERROR casting f32 to i32 may truncate the value
1f32 as u32; //~ERROR casting f32 to u32 may truncate the value
//~^ERROR casting f32 to u32 may lose the sign of the value
1f64 as f32; //~ERROR casting f64 to f32 may truncate the value
1i32 as i8; //~ERROR casting i32 to i8 may truncate the value
1i32 as u8; //~ERROR casting i32 to u8 may truncate the value
//~^ERROR casting i32 to u8 may lose the sign of the value
1f64 as isize; //~ERROR casting f64 to isize may truncate the value
1f64 as usize; //~ERROR casting f64 to usize may truncate the value
//~^ERROR casting f64 to usize may lose the sign of the value
// Test cast_possible_wrap
1u8 as i8; //~ERROR casting u8 to i8 may wrap around the value
1u16 as i16; //~ERROR casting u16 to i16 may wrap around the value
1u32 as i32; //~ERROR casting u32 to i32 may wrap around the value
1u64 as i64; //~ERROR casting u64 to i64 may wrap around the value
1usize as isize; //~ERROR casting usize to isize may wrap around the value
// Test cast_sign_loss
i as u32; //~ERROR casting from i32 to u32 loses the sign of the value
1i32 as u32; //~ERROR casting i32 to u32 may lose the sign of the value
1isize as usize; //~ERROR casting isize to usize may lose the sign of the value
// Extra checks for usize/isize
let is : isize = -42;
is as usize; //~ERROR casting from isize to usize loses the sign of the value
is as i8; //~ERROR casting isize to i8 may cause truncation of the value
// FIXME : enable these checks when we figure out a way to make compiletest deal with conditional compilation
/*
#[cfg(target_pointer_width = "64")]
fn check_64() {
let is : isize = -42;
let us : usize = 42;
is as f32; //ERROR converting from isize to f32, which causes a loss of precision (isize is 64 bits wide, but f32's mantissa is only 23 bits wide)
us as u32; //ERROR casting usize to u32 may cause truncation of the value
us as u64; // Should not trigger any lint
}
#[cfg(target_pointer_width = "32")]
fn check_32() {
let is : isize = -42;
let us : usize = 42;
is as f32; //ERROR converting from isize to f32, which causes a loss of precision (isize is 32 bits wide, but f32's mantissa is only 23 bits wide)
us as u32; // Should not trigger any lint
us as u64; // Should not trigger any lint
}*/
// Extra checks for *size
// Casting from *size
1isize as i8; //~ERROR casting isize to i8 may truncate the value
1isize as f64; //~ERROR casting isize to f64 causes a loss of precision on targets with 64-bit wide pointers (isize is 64 bits wide, but f64's mantissa is only 52 bits wide)
1usize as f64; //~ERROR casting usize to f64 causes a loss of precision on targets with 64-bit wide pointers (usize is 64 bits wide, but f64's mantissa is only 52 bits wide)
1isize as f32; //~ERROR casting isize to f32 causes a loss of precision (isize is 32 or 64 bits wide, but f32's mantissa is only 23 bits wide)
1usize as f32; //~ERROR casting usize to f32 causes a loss of precision (usize is 32 or 64 bits wide, but f32's mantissa is only 23 bits wide)
1isize as i32; //~ERROR casting isize to i32 may truncate the value on targets with 64-bit wide pointers
1isize as u32; //~ERROR casting isize to u32 may lose the sign of the value
//~^ERROR casting isize to u32 may truncate the value on targets with 64-bit wide pointers
1usize as u32; //~ERROR casting usize to u32 may truncate the value on targets with 64-bit wide pointers
1usize as i32; //~ERROR casting usize to i32 may truncate the value on targets with 64-bit wide pointers
//~^ERROR casting usize to i32 may wrap around the value on targets with 32-bit wide pointers
// Casting to *size
1i64 as isize; //~ERROR casting i64 to isize may truncate the value on targets with 32-bit wide pointers
1i64 as usize; //~ERROR casting i64 to usize may truncate the value on targets with 32-bit wide pointers
//~^ERROR casting i64 to usize may lose the sign of the value
1u64 as isize; //~ERROR casting u64 to isize may truncate the value on targets with 32-bit wide pointers
//~^ERROR casting u64 to isize may wrap around the value on targets with 64-bit wide pointers
1u64 as usize; //~ERROR casting u64 to usize may truncate the value on targets with 32-bit wide pointers
1u32 as isize; //~ERROR casting u32 to isize may wrap around the value on targets with 32-bit wide pointers
1u32 as usize; // Should not trigger any lint
1i32 as isize; // Neither should this
1i32 as usize; //~ERROR casting i32 to usize may lose the sign of the value
}