]> git.proxmox.com Git - rustc.git/blame_incremental - src/tools/clippy/tests/ui/unnecessary_cast.fixed
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / tools / clippy / tests / ui / unnecessary_cast.fixed
... / ...
CommitLineData
1//@aux-build:extern_fake_libc.rs
2#![warn(clippy::unnecessary_cast)]
3#![allow(
4 clippy::borrow_as_ptr,
5 clippy::multiple_bound_locations,
6 clippy::no_effect,
7 clippy::nonstandard_macro_braces,
8 clippy::unnecessary_operation,
9 nonstandard_style,
10 unused
11)]
12
13extern crate extern_fake_libc;
14
15type PtrConstU8 = *const u8;
16type PtrMutU8 = *mut u8;
17
18fn owo<T>(ptr: *const T) -> *const T {
19 ptr
20}
21
22fn uwu<T, U>(ptr: *const T) -> *const U {
23 ptr as *const U
24}
25
26mod fake_libc {
27 type pid_t = i32;
28 pub unsafe fn getpid() -> pid_t {
29 pid_t::from(0)
30 }
31 // Make sure a where clause does not break it
32 pub fn getpid_SAFE_TRUTH<T: Clone>(t: &T) -> pid_t
33 where
34 T: Clone,
35 {
36 t;
37 unsafe { getpid() }
38 }
39}
40
41fn aaa() -> ::std::primitive::u32 {
42 0
43}
44
45use std::primitive::u32 as UnsignedThirtyTwoBitInteger;
46
47fn bbb() -> UnsignedThirtyTwoBitInteger {
48 0
49}
50
51#[rustfmt::skip]
52fn main() {
53 // Test cast_unnecessary
54 1_i32;
55 1_f32;
56 false;
57 &1i32 as &i32;
58
59 -1_i32;
60 - 1_i32;
61 -1_f32;
62 1_i32;
63 1_f32;
64
65 let _: *mut u8 = [1u8, 2].as_ptr() as *mut u8;
66
67 [1u8, 2].as_ptr();
68 [1u8, 2].as_ptr() as *mut u8;
69 [1u8, 2].as_mut_ptr();
70 [1u8, 2].as_mut_ptr() as *const u8;
71 [1u8, 2].as_ptr() as PtrConstU8;
72 [1u8, 2].as_ptr() as PtrMutU8;
73 [1u8, 2].as_mut_ptr() as PtrMutU8;
74 [1u8, 2].as_mut_ptr() as PtrConstU8;
75 let _: *const u8 = [1u8, 2].as_ptr() as _;
76 let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
77 let _: *const u8 = [1u8, 2].as_ptr() as *const _;
78 let _: *mut u8 = [1u8, 2].as_mut_ptr() as *mut _;
79
80 owo::<u32>([1u32].as_ptr());
81 uwu::<u32, u8>([1u32].as_ptr());
82 // this will not lint in the function body even though they have the same type, instead here
83 uwu::<u32, u32>([1u32].as_ptr());
84
85 // macro version
86 macro_rules! foo {
87 ($a:ident, $b:ident) => {
88 #[allow(unused)]
89 pub fn $a() -> $b {
90 1 as $b
91 }
92 };
93 }
94 foo!(a, i32);
95 foo!(b, f32);
96 foo!(c, f64);
97
98 // do not lint cast from cfg-dependant type
99 let x = 0 as std::ffi::c_ulong;
100 let y = x as u64;
101 let x: std::ffi::c_ulong = 0;
102 let y = x as u64;
103
104 // do not lint cast to cfg-dependant type
105 let x = 1 as std::os::raw::c_char;
106 let y = x as u64;
107
108 // do not lint cast to alias type
109 1 as I32Alias;
110 &1 as &I32Alias;
111 // or from
112 let x: I32Alias = 1;
113 let y = x as u64;
114 fake_libc::getpid_SAFE_TRUTH(&0u32) as i32;
115 extern_fake_libc::getpid_SAFE_TRUTH() as i32;
116 let pid = unsafe { fake_libc::getpid() };
117 pid as i32;
118 aaa();
119 let x = aaa();
120 aaa();
121 // Will not lint currently.
122 bbb() as u32;
123 let x = bbb();
124 bbb() as u32;
125
126 let i8_ptr: *const i8 = &1;
127 let u8_ptr: *const u8 = &1;
128
129 // cfg dependant pointees
130 i8_ptr as *const std::os::raw::c_char;
131 u8_ptr as *const std::os::raw::c_char;
132
133 // type aliased pointees
134 i8_ptr as *const std::ffi::c_char;
135 u8_ptr as *const std::ffi::c_char;
136
137 // issue #9960
138 macro_rules! bind_var {
139 ($id:ident, $e:expr) => {{
140 let $id = 0usize;
141 let _ = $e != 0usize;
142 let $id = 0isize;
143 let _ = $e != 0usize;
144 }}
145 }
146 bind_var!(x, (x as usize) + 1);
147}
148
149type I32Alias = i32;
150
151mod fixable {
152 #![allow(dead_code)]
153
154 fn main() {
155 // casting integer literal to float is unnecessary
156 100_f32;
157 100_f64;
158 100_f64;
159 let _ = -100_f32;
160 let _ = -100_f64;
161 let _ = -100_f64;
162 100_f32;
163 100_f64;
164 // Should not trigger
165 #[rustfmt::skip]
166 let v = vec!(1);
167 &v as &[i32];
168 0x10 as f32;
169 0o10 as f32;
170 0b10 as f32;
171 0x11 as f64;
172 0o11 as f64;
173 0b11 as f64;
174
175 1_u32;
176 0x10_i32;
177 0b10_usize;
178 0o73_u16;
179 1_000_000_000_u32;
180
181 1.0_f64;
182 0.5_f32;
183
184 1.0 as u16;
185
186 let _ = -1_i32;
187 let _ = -1.0_f32;
188
189 let _ = 1 as I32Alias;
190 let _ = &1 as &I32Alias;
191
192 let x = 1i32;
193 let _ = &{ x };
194 }
195
196 type I32Alias = i32;
197
198 fn issue_9380() {
199 let _: i32 = -1_i32;
200 let _: f32 = -(1) as f32;
201 let _: i64 = -1_i64;
202 let _: i64 = -(1.0) as i64;
203
204 let _ = -(1 + 1) as i64;
205 }
206
207 fn issue_9563() {
208 let _: f64 = (-8.0_f64).exp();
209 #[allow(clippy::precedence)]
210 let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
211 }
212
213 fn issue_9562_non_literal() {
214 fn foo() -> f32 {
215 0.
216 }
217
218 let _num = foo();
219 }
220
221 fn issue_9603() {
222 let _: f32 = -0x400 as f32;
223 }
224
225 // Issue #11968: The suggestion for this lint removes the parentheses and leave the code as
226 // `*x.pow(2)` which tries to dereference the return value rather than `x`.
227 fn issue_11968(x: &usize) -> usize {
228 { *x }.pow(2)
229 }
230}