]> git.lizzy.rs Git - rust.git/blob - tests/ui/cast.rs
casting integer literal to float is unnecessary
[rust.git] / tests / ui / cast.rs
1 #[warn(
2     clippy::cast_precision_loss,
3     clippy::cast_possible_truncation,
4     clippy::cast_sign_loss,
5     clippy::cast_possible_wrap,
6     clippy::cast_lossless
7 )]
8 #[allow(clippy::no_effect, clippy::unnecessary_operation)]
9 fn main() {
10     // Test clippy::cast_precision_loss
11     let x0 = 1i32;
12     x0 as f32;
13     let x1 = 1i64;
14     x1 as f32;
15     x1 as f64;
16     let x2 = 1u32;
17     x2 as f32;
18     let x3 = 1u64;
19     x3 as f32;
20     x3 as f64;
21     // Test clippy::cast_possible_truncation
22     1f32 as i32;
23     1f32 as u32;
24     1f64 as f32;
25     1i32 as i8;
26     1i32 as u8;
27     1f64 as isize;
28     1f64 as usize;
29     // Test clippy::cast_possible_wrap
30     1u8 as i8;
31     1u16 as i16;
32     1u32 as i32;
33     1u64 as i64;
34     1usize as isize;
35     // Test clippy::cast_lossless with casts from floating-point types
36     1.0f32 as f64;
37     // Test clippy::cast_lossless with an expression wrapped in parens
38     (1u8 + 1u8) as u16;
39     // Test clippy::cast_sign_loss
40     1i32 as u32;
41     -1i32 as u32;
42     1isize as usize;
43     -1isize as usize;
44     0i8 as u8;
45     i8::max_value() as u8;
46     i16::max_value() as u16;
47     i32::max_value() as u32;
48     i64::max_value() as u64;
49     i128::max_value() as u128;
50     // Extra checks for *size
51     // Test cast_unnecessary
52     1i32 as i32;
53     1f32 as f32;
54     false as bool;
55     &1i32 as &i32;
56     // casting integer literal to float is unnecessary
57     100 as f32;
58     100 as f64;
59     100_i32 as f64;
60     // Should not trigger
61     #[rustfmt::skip]
62     let v = vec!(1);
63     &v as &[i32];
64     1.0 as f64;
65     1 as u64;
66 }