1 //@aux-build:extern_fake_libc.rs
2 #![warn(clippy::unnecessary_cast)]
6 clippy::nonstandard_macro_braces,
7 clippy::unnecessary_operation,
12 extern crate extern_fake_libc;
14 type PtrConstU8 = *const u8;
15 type PtrMutU8 = *mut u8;
17 fn owo<T>(ptr: *const T) -> *const T {
21 fn uwu<T, U>(ptr: *const T) -> *const U {
27 pub unsafe fn getpid() -> pid_t {
30 // Make sure a where clause does not break it
31 pub fn getpid_SAFE_TRUTH<T: Clone>(t: &T) -> pid_t
40 fn aaa() -> ::std::primitive::u32 {
44 use std::primitive::u32 as UnsignedThirtyTwoBitInteger;
46 fn bbb() -> UnsignedThirtyTwoBitInteger {
52 // Test cast_unnecessary
64 let _: *mut u8 = [1u8, 2].as_ptr() as *mut u8;
67 [1u8, 2].as_ptr() as *mut u8;
68 [1u8, 2].as_mut_ptr();
69 [1u8, 2].as_mut_ptr() as *const u8;
70 [1u8, 2].as_ptr() as PtrConstU8;
71 [1u8, 2].as_ptr() as PtrMutU8;
72 [1u8, 2].as_mut_ptr() as PtrMutU8;
73 [1u8, 2].as_mut_ptr() as PtrConstU8;
74 let _: *const u8 = [1u8, 2].as_ptr() as _;
75 let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
76 let _: *const u8 = [1u8, 2].as_ptr() as *const _;
77 let _: *mut u8 = [1u8, 2].as_mut_ptr() as *mut _;
79 owo::<u32>([1u32].as_ptr());
80 uwu::<u32, u8>([1u32].as_ptr());
81 // this will not lint in the function body even though they have the same type, instead here
82 uwu::<u32, u32>([1u32].as_ptr());
86 ($a:ident, $b:ident) => {
97 // do not lint cast from cfg-dependant type
98 let x = 0 as std::ffi::c_ulong;
100 let x: std::ffi::c_ulong = 0;
103 // do not lint cast to cfg-dependant type
104 let x = 1 as std::os::raw::c_char;
107 // do not lint cast to alias type
113 fake_libc::getpid_SAFE_TRUTH(&0u32) as i32;
114 extern_fake_libc::getpid_SAFE_TRUTH() as i32;
115 let pid = unsafe { fake_libc::getpid() };
120 // Will not lint currently.
125 let i8_ptr: *const i8 = &1;
126 let u8_ptr: *const u8 = &1;
128 // cfg dependant pointees
129 i8_ptr as *const std::os::raw::c_char;
130 u8_ptr as *const std::os::raw::c_char;
132 // type aliased pointees
133 i8_ptr as *const std::ffi::c_char;
134 u8_ptr as *const std::ffi::c_char;
137 macro_rules! bind_var {
138 ($id:ident, $e:expr) => {{
140 let _ = $e != 0usize;
142 let _ = $e != 0usize;
145 bind_var!(x, (x as usize) + 1);
154 // casting integer literal to float is unnecessary
163 // Should not trigger
188 let _ = 1 as I32Alias;
189 let _ = &1 as &I32Alias;
199 let _: f32 = -(1) as f32;
201 let _: i64 = -(1.0) as i64;
203 let _ = -(1 + 1) as i64;
207 let _: f64 = (-8.0_f64).exp();
208 #[allow(clippy::precedence)]
209 let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
212 fn issue_9562_non_literal() {
221 let _: f32 = -0x400 as f32;