]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/unnecessary_cast.fixed
Remove `LitKind::synthesize_token_lit`.
[rust.git] / src / tools / clippy / tests / ui / unnecessary_cast.fixed
1 // run-rustfix
2 #![warn(clippy::unnecessary_cast)]
3 #![allow(
4     unused_must_use,
5     clippy::borrow_as_ptr,
6     clippy::no_effect,
7     clippy::nonstandard_macro_braces,
8     clippy::unnecessary_operation
9 )]
10
11 #[rustfmt::skip]
12 fn main() {
13     // Test cast_unnecessary
14     1_i32;
15     1_f32;
16     false;
17     &1i32 as &i32;
18
19     -1_i32;
20     - 1_i32;
21     -1_f32;
22     1_i32;
23     1_f32;
24
25     // macro version
26     macro_rules! foo {
27         ($a:ident, $b:ident) => {
28             #[allow(unused)]
29             pub fn $a() -> $b {
30                 1 as $b
31             }
32         };
33     }
34     foo!(a, i32);
35     foo!(b, f32);
36     foo!(c, f64);
37
38     // do not lint cast to cfg-dependant type
39     1 as std::os::raw::c_char;
40
41     // do not lint cast to alias type
42     1 as I32Alias;
43     &1 as &I32Alias;
44 }
45
46 type I32Alias = i32;
47
48 mod fixable {
49     #![allow(dead_code)]
50
51     fn main() {
52         // casting integer literal to float is unnecessary
53         100_f32;
54         100_f64;
55         100_f64;
56         let _ = -100_f32;
57         let _ = -100_f64;
58         let _ = -100_f64;
59         100_f32;
60         100_f64;
61         // Should not trigger
62         #[rustfmt::skip]
63         let v = vec!(1);
64         &v as &[i32];
65         0x10 as f32;
66         0o10 as f32;
67         0b10 as f32;
68         0x11 as f64;
69         0o11 as f64;
70         0b11 as f64;
71
72         1_u32;
73         0x10_i32;
74         0b10_usize;
75         0o73_u16;
76         1_000_000_000_u32;
77
78         1.0_f64;
79         0.5_f32;
80
81         1.0 as u16;
82
83         let _ = -1_i32;
84         let _ = -1.0_f32;
85
86         let _ = 1 as I32Alias;
87         let _ = &1 as &I32Alias;
88     }
89
90     type I32Alias = i32;
91
92     fn issue_9380() {
93         let _: i32 = -1_i32;
94         let _: f32 = -(1) as f32;
95         let _: i64 = -1_i64;
96         let _: i64 = -(1.0) as i64;
97
98         let _ = -(1 + 1) as i64;
99     }
100
101     fn issue_9563() {
102         let _: f64 = (-8.0_f64).exp();
103         #[allow(clippy::precedence)]
104         let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
105     }
106
107     fn issue_9562_non_literal() {
108         fn foo() -> f32 {
109             0.
110         }
111
112         let _num = foo();
113     }
114
115     fn issue_9603() {
116         let _: f32 = -0x400 as f32;
117     }
118 }