fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1//@aux-build:extern_fake_libc.rs 2#![warn(clippy::unnecessary_cast)] 3#![allow( 4 clippy::borrow_as_ptr, 5 clippy::no_effect, 6 clippy::nonstandard_macro_braces, 7 clippy::unnecessary_operation, 8 nonstandard_style, 9 unused 10)] 11 12extern crate extern_fake_libc; 13 14type PtrConstU8 = *const u8; 15type PtrMutU8 = *mut u8; 16 17fn owo<T>(ptr: *const T) -> *const T { 18 ptr 19} 20 21fn uwu<T, U>(ptr: *const T) -> *const U { 22 ptr as *const U 23} 24 25mod fake_libc { 26 type pid_t = i32; 27 pub unsafe fn getpid() -> pid_t { 28 pid_t::from(0) 29 } 30 // Make sure a where clause does not break it 31 pub fn getpid_SAFE_TRUTH<T: Clone>(t: &T) -> pid_t 32 where 33 T: Clone, 34 { 35 t; 36 unsafe { getpid() } 37 } 38} 39 40fn aaa() -> ::std::primitive::u32 { 41 0 42} 43 44use std::primitive::u32 as UnsignedThirtyTwoBitInteger; 45 46fn bbb() -> UnsignedThirtyTwoBitInteger { 47 0 48} 49 50#[rustfmt::skip] 51fn main() { 52 // Test cast_unnecessary 53 1_i32; 54 1_f32; 55 false; 56 &1i32 as &i32; 57 58 -1_i32; 59 - 1_i32; 60 -1_f32; 61 1_i32; 62 1_f32; 63 64 let _: *mut u8 = [1u8, 2].as_ptr() as *mut u8; 65 66 [1u8, 2].as_ptr(); 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 _; 78 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()); 83 84 // macro version 85 macro_rules! foo { 86 ($a:ident, $b:ident) => { 87 #[allow(unused)] 88 pub fn $a() -> $b { 89 1 as $b 90 } 91 }; 92 } 93 foo!(a, i32); 94 foo!(b, f32); 95 foo!(c, f64); 96 97 // do not lint cast from cfg-dependant type 98 let x = 0 as std::ffi::c_ulong; 99 let y = x as u64; 100 let x: std::ffi::c_ulong = 0; 101 let y = x as u64; 102 103 // do not lint cast to cfg-dependant type 104 let x = 1 as std::os::raw::c_char; 105 let y = x as u64; 106 107 // do not lint cast to alias type 108 1 as I32Alias; 109 &1 as &I32Alias; 110 // or from 111 let x: I32Alias = 1; 112 let y = x as u64; 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() }; 116 pid as i32; 117 aaa(); 118 let x = aaa(); 119 aaa(); 120 // Will not lint currently. 121 bbb() as u32; 122 let x = bbb(); 123 bbb() as u32; 124 125 let i8_ptr: *const i8 = &1; 126 let u8_ptr: *const u8 = &1; 127 128 // cfg dependant pointees 129 i8_ptr as *const std::os::raw::c_char; 130 u8_ptr as *const std::os::raw::c_char; 131 132 // type aliased pointees 133 i8_ptr as *const std::ffi::c_char; 134 u8_ptr as *const std::ffi::c_char; 135 136 // issue #9960 137 macro_rules! bind_var { 138 ($id:ident, $e:expr) => {{ 139 let $id = 0usize; 140 let _ = $e != 0usize; 141 let $id = 0isize; 142 let _ = $e != 0usize; 143 }} 144 } 145 bind_var!(x, (x as usize) + 1); 146} 147 148type I32Alias = i32; 149 150mod fixable { 151 #![allow(dead_code)] 152 153 fn main() { 154 // casting integer literal to float is unnecessary 155 100_f32; 156 100_f64; 157 100_f64; 158 let _ = -100_f32; 159 let _ = -100_f64; 160 let _ = -100_f64; 161 100_f32; 162 100_f64; 163 // Should not trigger 164 #[rustfmt::skip] 165 let v = vec!(1); 166 &v as &[i32]; 167 0x10 as f32; 168 0o10 as f32; 169 0b10 as f32; 170 0x11 as f64; 171 0o11 as f64; 172 0b11 as f64; 173 174 1_u32; 175 0x10_i32; 176 0b10_usize; 177 0o73_u16; 178 1_000_000_000_u32; 179 180 1.0_f64; 181 0.5_f32; 182 183 1.0 as u16; 184 185 let _ = -1_i32; 186 let _ = -1.0_f32; 187 188 let _ = 1 as I32Alias; 189 let _ = &1 as &I32Alias; 190 191 let x = 1i32; 192 let _ = &{ x }; 193 } 194 195 type I32Alias = i32; 196 197 fn issue_9380() { 198 let _: i32 = -1_i32; 199 let _: f32 = -(1) as f32; 200 let _: i64 = -1_i64; 201 let _: i64 = -(1.0) as i64; 202 203 let _ = -(1 + 1) as i64; 204 } 205 206 fn issue_9563() { 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 210 } 211 212 fn issue_9562_non_literal() { 213 fn foo() -> f32 { 214 0. 215 } 216 217 let _num = foo(); 218 } 219 220 fn issue_9603() { 221 let _: f32 = -0x400 as f32; 222 } 223}