]> git.lizzy.rs Git - rust.git/blob - tests/ui/cast.rs
rustup and compile-fail -> ui test move
[rust.git] / tests / ui / cast.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #[deny(cast_precision_loss, cast_possible_truncation, cast_sign_loss, cast_possible_wrap)]
5 #[allow(no_effect, unnecessary_operation)]
6 fn main() {
7     // Test cast_precision_loss
8     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)
9     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)
10     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)
11     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)
12     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)
13     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)
14     1i32 as f64; // Should not trigger the lint
15     1u32 as f64; // Should not trigger the lint
16
17     // Test cast_possible_truncation
18     1f32 as i32;   //~ERROR casting f32 to i32 may truncate the value
19     1f32 as u32;   //~ERROR casting f32 to u32 may truncate the value
20                   //~^ERROR casting f32 to u32 may lose the sign of the value
21     1f64 as f32;   //~ERROR casting f64 to f32 may truncate the value
22     1i32 as i8;    //~ERROR casting i32 to i8 may truncate the value
23     1i32 as u8;    //~ERROR casting i32 to u8 may truncate the value
24                   //~^ERROR casting i32 to u8 may lose the sign of the value
25     1f64 as isize; //~ERROR casting f64 to isize may truncate the value
26     1f64 as usize; //~ERROR casting f64 to usize may truncate the value
27                   //~^ERROR casting f64 to usize may lose the sign of the value
28
29     // Test cast_possible_wrap
30     1u8 as i8;       //~ERROR casting u8 to i8 may wrap around the value
31     1u16 as i16;     //~ERROR casting u16 to i16 may wrap around the value
32     1u32 as i32;     //~ERROR casting u32 to i32 may wrap around the value
33     1u64 as i64;     //~ERROR casting u64 to i64 may wrap around the value
34     1usize as isize; //~ERROR casting usize to isize may wrap around the value
35
36     // Test cast_sign_loss
37     1i32 as u32;     //~ERROR casting i32 to u32 may lose the sign of the value
38     1isize as usize; //~ERROR casting isize to usize may lose the sign of the value
39
40     // Extra checks for *size
41     // Casting from *size
42     1isize as i8;  //~ERROR casting isize to i8 may truncate the value
43     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)
44     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)
45     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)
46     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)
47     1isize as i32; //~ERROR casting isize to i32 may truncate the value on targets with 64-bit wide pointers
48     1isize as u32; //~ERROR casting isize to u32 may lose the sign of the value
49                   //~^ERROR casting isize to u32 may truncate the value on targets with 64-bit wide pointers
50     1usize as u32; //~ERROR casting usize to u32 may truncate the value on targets with 64-bit wide pointers
51     1usize as i32; //~ERROR casting usize to i32 may truncate the value on targets with 64-bit wide pointers
52                   //~^ERROR casting usize to i32 may wrap around the value on targets with 32-bit wide pointers
53     // Casting to *size
54     1i64 as isize; //~ERROR casting i64 to isize may truncate the value on targets with 32-bit wide pointers
55     1i64 as usize; //~ERROR casting i64 to usize may truncate the value on targets with 32-bit wide pointers
56                   //~^ERROR casting i64 to usize may lose the sign of the value
57     1u64 as isize; //~ERROR casting u64 to isize may truncate the value on targets with 32-bit wide pointers
58                   //~^ERROR casting u64 to isize may wrap around the value on targets with 64-bit wide pointers
59     1u64 as usize; //~ERROR casting u64 to usize may truncate the value on targets with 32-bit wide pointers
60     1u32 as isize; //~ERROR casting u32 to isize may wrap around the value on targets with 32-bit wide pointers
61     1u32 as usize; // Should not trigger any lint
62     1i32 as isize; // Neither should this
63     1i32 as usize; //~ERROR casting i32 to usize may lose the sign of the value
64 }