From f1255d5f5d9c9d23d039ee3798af4e164563226f Mon Sep 17 00:00:00 2001 From: "R.Chavignat" Date: Sat, 22 Aug 2015 02:44:05 +0200 Subject: [PATCH 1/4] Casts : work in progress handling *size separately --- src/types.rs | 37 ++++++++++++++++++++++++++++--------- tests/compile-fail/cast.rs | 28 +++++++--------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/types.rs b/src/types.rs index 915ffb15fe56..e8da11ecbe0a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -150,14 +150,21 @@ declare_lint!(pub CAST_POSSIBLE_TRUNCATION, Allow, /// Will return 0 if the type is not an int or uint variant fn int_ty_to_nbits(typ: &ty::TyS) -> usize { let n = match &typ.sty { - &ty::TyInt(i) => 4 << (i as usize), - &ty::TyUint(u) => 4 << (u as usize), - _ => 0 + &ty::TyInt(i) => 4 << (i as usize), + &ty::TyUint(u) => 4 << (u as usize), + _ => 0 }; // n == 4 is the usize/isize case 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 + } +} + impl LintPass for CastPass { fn get_lints(&self) -> LintArray { lint_array!(CAST_PRECISION_LOSS, @@ -178,7 +185,14 @@ impl LintPass for CastPass { _ => 0 }; if from_nbits != 0 { - if from_nbits >= to_nbits { + // When casting to f32, precision loss would occur regardless of the arch + if is_isize_or_usize(cast_from) && to_nbits == 64 { + span_lint(cx, CAST_PRECISION_LOSS, expr.span, + &format!("converting from {0} to f64, which causes a loss of precision on 64-bit architectures \ + ({0} is 64 bits wide, but f64's mantissa is only 52 bits wide)", + cast_from)); + } + else 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)", @@ -186,7 +200,7 @@ impl LintPass for CastPass { } } }, - (false, true) => { + (false, true) => { // Nothing to add there span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &format!("casting {} to {} may cause truncation of the value", cast_from, cast_to)); if !cast_to.is_signed() { @@ -201,10 +215,15 @@ impl LintPass for CastPass { } 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)); + match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) { + (true, true) | (false, false) => + 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)); + }, + (true, false) => (), // TODO + (false, true) => () // TODO } } (false, false) => { diff --git a/tests/compile-fail/cast.rs b/tests/compile-fail/cast.rs index 0fa402b3bf73..8e854fb21f93 100644 --- a/tests/compile-fail/cast.rs +++ b/tests/compile-fail/cast.rs @@ -32,26 +32,12 @@ fn main() { i as u32; //~ERROR casting from i32 to u32 loses 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 - }*/ + let is : isize = -42; + let us : usize = 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 + is as f64; //ERROR converting from isize to f64, which causes a loss of precision on 64-bit architectures (isize is 64 bits wide, but f64's mantissa is only 52 bits wide) + us as f64; //ERROR converting from usize to f64, which causes a loss of precision on 64-bit architectures (usize is 64 bits wide, but f64's mantissa is only 52 bits wide) + */ } \ No newline at end of file From 807dab943bc35b8f579cc082f385bdd5a6a98c63 Mon Sep 17 00:00:00 2001 From: "R.Chavignat" Date: Sat, 22 Aug 2015 21:36:54 +0200 Subject: [PATCH 2/4] Updated test case for cast lints. Also improved readability and reworded the messages. --- tests/compile-fail/cast.rs | 78 ++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/tests/compile-fail/cast.rs b/tests/compile-fail/cast.rs index 8e854fb21f93..9be0d5011984 100644 --- a/tests/compile-fail/cast.rs +++ b/tests/compile-fail/cast.rs @@ -1,43 +1,57 @@ #![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; - let us : usize = 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 - is as f64; //ERROR converting from isize to f64, which causes a loss of precision on 64-bit architectures (isize is 64 bits wide, but f64's mantissa is only 52 bits wide) - us as f64; //ERROR converting from usize to f64, which causes a loss of precision on 64-bit architectures (usize is 64 bits wide, but f64's mantissa is only 52 bits wide) - */ + // 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 + // 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 } \ No newline at end of file From 79ef13592e617e58b84146ba568c43bad81e77bf Mon Sep 17 00:00:00 2001 From: "R.Chavignat" Date: Sat, 22 Aug 2015 23:49:03 +0200 Subject: [PATCH 3/4] Completed the implementation of *size handling. Added some more cases to the test, and implemented a new lint, cast_possible_wrap, triggered when casting from an unsigned type to a signed type of the same size. --- README.md | 1 + src/lib.rs | 1 + src/types.rs | 88 +++++++++++++++++++++++++++++--------- tests/compile-fail/cast.rs | 8 +++- 4 files changed, 76 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index fbb16fcb1704..7ac5388e0fe5 100644 --- a/README.md +++ b/README.md @@ -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>`, 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) diff --git a/src/lib.rs b/src/lib.rs index 9ea1efeed5f8..19a62dd214bc 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,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, diff --git a/src/types.rs b/src/types.rs index e8da11ecbe0a..579527cfff44 100644 --- a/src/types.rs +++ b/src/types.rs @@ -145,6 +145,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 @@ -169,7 +171,8 @@ 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) { @@ -186,50 +189,93 @@ impl LintPass for CastPass { }; if from_nbits != 0 { // When casting to f32, precision loss would occur regardless of the arch - if is_isize_or_usize(cast_from) && to_nbits == 64 { - span_lint(cx, CAST_PRECISION_LOSS, expr.span, - &format!("converting from {0} to f64, which causes a loss of precision on 64-bit architectures \ - ({0} is 64 bits wide, but f64's mantissa is only 52 bits wide)", - cast_from)); + if is_isize_or_usize(cast_from) { + if to_nbits == 64 { + span_lint(cx, CAST_PRECISION_LOSS, expr.span, + &format!("casting {0} to f64 causes a loss of precision on targets with 64-bit wide pointers \ + ({0} is 64 bits wide, but f64's mantissa is only 52 bits wide)", + cast_from)); + } + else { + span_lint(cx, CAST_PRECISION_LOSS, expr.span, + &format!("casting {0} to f32 causes a loss of precision \ + ({0} is 32 or 64 bits wide, but f32's mantissa is only 23 bits wide)", + cast_from)); + } } else 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 \ + &format!("casting {0} to {1} 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} )); } } }, - (false, true) => { // Nothing to add there + (false, true) => { + // Nothing to add there as long as UB in involved when the cast overflows 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 cast_from.is_signed() && !cast_to.is_signed() { + span_lint(cx, CAST_SIGN_LOSS, expr.span, + &format!("casting {} to {} may lose the sign of the value", cast_from, cast_to)); + } match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) { (true, true) | (false, false) => - 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)); + if to_nbits < from_nbits { + span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, + &format!("casting {} to {} may truncate the value", cast_from, cast_to)); + } + else if !cast_from.is_signed() && cast_to.is_signed() && to_nbits == from_nbits { + span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, + &format!("casting {} to {} may wrap around the value", cast_from, cast_to)); }, - (true, false) => (), // TODO - (false, true) => () // TODO + (true, false) => + if to_nbits == 32 { + span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, + &format!("casting {} to {} may truncate the value on targets with 64-bit wide pointers", + cast_from, cast_to)); + if !cast_from.is_signed() && cast_to.is_signed() { + span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, + &format!("casting {} to {} may wrap around the value on targets with 32-bit wide pointers", + cast_from, cast_to)); + } + } + else if to_nbits < 32 { + span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, + &format!("casting {} to {} may truncate the value", cast_from, cast_to)); + }, + (false, true) => + if from_nbits == 64 { + span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, + &format!("casting {} to {} may truncate the value on targets with 32-bit wide pointers", + cast_from, cast_to)); + if !cast_from.is_signed() && cast_to.is_signed() { + span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, + &format!("casting {} to {} may wrap around the value on targets with 64-bit wide pointers", + cast_from, cast_to)); + } + } + else { + if !cast_from.is_signed() && cast_to.is_signed() { + span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, + &format!("casting {} to {} may wrap around the value on targets with 32-bit wide pointers", + 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"); } } } diff --git a/tests/compile-fail/cast.rs b/tests/compile-fail/cast.rs index 9be0d5011984..b17f5de841b4 100644 --- a/tests/compile-fail/cast.rs +++ b/tests/compile-fail/cast.rs @@ -45,8 +45,10 @@ fn main() { 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 + //~^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 @@ -54,4 +56,8 @@ fn main() { 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 } \ No newline at end of file From 3af2e3ba857630214d9bc81756be1c07ae305fc4 Mon Sep 17 00:00:00 2001 From: "R.Chavignat" Date: Sun, 23 Aug 2015 01:06:31 +0200 Subject: [PATCH 4/4] Refactored CastPass. --- src/types.rs | 143 +++++++++++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 73 deletions(-) diff --git a/src/types.rs b/src/types.rs index 47ee51e98ef2..4e9dd133ac8e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -134,6 +134,72 @@ fn is_isize_or_usize(typ: &ty::TyS) -> bool { } } +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, @@ -149,33 +215,9 @@ 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 { - // When casting to f32, precision loss would occur regardless of the arch - if is_isize_or_usize(cast_from) { - if to_nbits == 64 { - span_lint(cx, CAST_PRECISION_LOSS, expr.span, - &format!("casting {0} to f64 causes a loss of precision on targets with 64-bit wide pointers \ - ({0} is 64 bits wide, but f64's mantissa is only 52 bits wide)", - cast_from)); - } - else { - span_lint(cx, CAST_PRECISION_LOSS, expr.span, - &format!("casting {0} to f32 causes a loss of precision \ - ({0} is 32 or 64 bits wide, but f32's mantissa is only 23 bits wide)", - cast_from)); - } - } - else if from_nbits >= to_nbits { - span_lint(cx, CAST_PRECISION_LOSS, expr.span, - &format!("casting {0} to {1} 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) => { @@ -187,56 +229,11 @@ impl LintPass for CastPass { } }, (true, true) => { - let from_nbits = int_ty_to_nbits(cast_from); - let to_nbits = int_ty_to_nbits(cast_to); if cast_from.is_signed() && !cast_to.is_signed() { span_lint(cx, CAST_SIGN_LOSS, expr.span, &format!("casting {} to {} may lose the sign of the value", cast_from, cast_to)); } - match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) { - (true, true) | (false, false) => - if to_nbits < from_nbits { - span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, - &format!("casting {} to {} may truncate the value", cast_from, cast_to)); - } - else if !cast_from.is_signed() && cast_to.is_signed() && to_nbits == from_nbits { - span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, - &format!("casting {} to {} may wrap around the value", cast_from, cast_to)); - }, - (true, false) => - if to_nbits == 32 { - span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, - &format!("casting {} to {} may truncate the value on targets with 64-bit wide pointers", - cast_from, cast_to)); - if !cast_from.is_signed() && cast_to.is_signed() { - span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, - &format!("casting {} to {} may wrap around the value on targets with 32-bit wide pointers", - cast_from, cast_to)); - } - } - else if to_nbits < 32 { - span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, - &format!("casting {} to {} may truncate the value", cast_from, cast_to)); - }, - (false, true) => - if from_nbits == 64 { - span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, - &format!("casting {} to {} may truncate the value on targets with 32-bit wide pointers", - cast_from, cast_to)); - if !cast_from.is_signed() && cast_to.is_signed() { - span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, - &format!("casting {} to {} may wrap around the value on targets with 64-bit wide pointers", - cast_from, cast_to)); - } - } - else { - if !cast_from.is_signed() && cast_to.is_signed() { - span_lint(cx, CAST_POSSIBLE_WRAP, expr.span, - &format!("casting {} to {} may wrap around the value on targets with 32-bit wide pointers", - cast_from, cast_to)); - } - } - } + check_truncation_and_wrapping(cx, expr, cast_from, cast_to); } (false, false) => { if let (&ty::TyFloat(ast::TyF64),