]>
Commit | Line | Data |
---|---|---|
487cf647 | 1 | #![allow(dead_code, clippy::borrow_as_ptr, clippy::needless_lifetimes)] |
781aab86 | 2 | //@no-rustfix |
f20569fa XL |
3 | extern crate core; |
4 | ||
5 | use std::mem::transmute as my_transmute; | |
6 | use std::vec::Vec as MyVec; | |
7 | ||
8 | fn my_int() -> Usize { | |
9 | Usize(42) | |
10 | } | |
11 | ||
12 | fn my_vec() -> MyVec<i32> { | |
13 | vec![] | |
14 | } | |
15 | ||
16 | #[allow(clippy::needless_lifetimes, clippy::transmute_ptr_to_ptr)] | |
17 | #[warn(clippy::useless_transmute)] | |
18 | unsafe fn _generic<'a, T, U: 'a>(t: &'a T) { | |
923072b8 FG |
19 | // FIXME: should lint |
20 | // let _: &'a T = core::intrinsics::transmute(t); | |
f20569fa XL |
21 | |
22 | let _: &'a U = core::intrinsics::transmute(t); | |
23 | ||
24 | let _: *const T = core::intrinsics::transmute(t); | |
781aab86 FG |
25 | //~^ ERROR: transmute from a reference to a pointer |
26 | //~| NOTE: `-D clippy::useless-transmute` implied by `-D warnings` | |
f20569fa XL |
27 | |
28 | let _: *mut T = core::intrinsics::transmute(t); | |
781aab86 | 29 | //~^ ERROR: transmute from a reference to a pointer |
f20569fa XL |
30 | |
31 | let _: *const U = core::intrinsics::transmute(t); | |
781aab86 | 32 | //~^ ERROR: transmute from a reference to a pointer |
f20569fa XL |
33 | } |
34 | ||
35 | #[warn(clippy::useless_transmute)] | |
36 | fn useless() { | |
37 | unsafe { | |
38 | let _: Vec<i32> = core::intrinsics::transmute(my_vec()); | |
781aab86 | 39 | //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself |
f20569fa XL |
40 | |
41 | let _: Vec<i32> = core::mem::transmute(my_vec()); | |
781aab86 | 42 | //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself |
f20569fa XL |
43 | |
44 | let _: Vec<i32> = std::intrinsics::transmute(my_vec()); | |
781aab86 | 45 | //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself |
f20569fa XL |
46 | |
47 | let _: Vec<i32> = std::mem::transmute(my_vec()); | |
781aab86 | 48 | //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself |
f20569fa XL |
49 | |
50 | let _: Vec<i32> = my_transmute(my_vec()); | |
781aab86 | 51 | //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself |
f20569fa XL |
52 | |
53 | let _: *const usize = std::mem::transmute(5_isize); | |
781aab86 | 54 | //~^ ERROR: transmute from an integer to a pointer |
f20569fa XL |
55 | |
56 | let _ = 5_isize as *const usize; | |
57 | ||
58 | let _: *const usize = std::mem::transmute(1 + 1usize); | |
781aab86 | 59 | //~^ ERROR: transmute from an integer to a pointer |
f20569fa XL |
60 | |
61 | let _ = (1 + 1_usize) as *const usize; | |
62 | } | |
923072b8 FG |
63 | |
64 | unsafe fn _f<'a, 'b>(x: &'a u32) -> &'b u32 { | |
65 | std::mem::transmute(x) | |
66 | } | |
67 | ||
68 | unsafe fn _f2<'a, 'b>(x: *const (dyn Iterator<Item = u32> + 'a)) -> *const (dyn Iterator<Item = u32> + 'b) { | |
69 | std::mem::transmute(x) | |
70 | } | |
71 | ||
72 | unsafe fn _f3<'a, 'b>(x: fn(&'a u32)) -> fn(&'b u32) { | |
73 | std::mem::transmute(x) | |
74 | } | |
75 | ||
76 | unsafe fn _f4<'a, 'b>(x: std::borrow::Cow<'a, str>) -> std::borrow::Cow<'b, str> { | |
77 | std::mem::transmute(x) | |
78 | } | |
f20569fa XL |
79 | } |
80 | ||
81 | struct Usize(usize); | |
82 | ||
83 | #[warn(clippy::crosspointer_transmute)] | |
84 | fn crosspointer() { | |
85 | let mut int: Usize = Usize(0); | |
86 | let int_const_ptr: *const Usize = &int as *const Usize; | |
87 | let int_mut_ptr: *mut Usize = &mut int as *mut Usize; | |
88 | ||
89 | unsafe { | |
90 | let _: Usize = core::intrinsics::transmute(int_const_ptr); | |
781aab86 FG |
91 | //~^ ERROR: transmute from a type (`*const Usize`) to the type that it points to ( |
92 | //~| NOTE: `-D clippy::crosspointer-transmute` implied by `-D warnings` | |
f20569fa XL |
93 | |
94 | let _: Usize = core::intrinsics::transmute(int_mut_ptr); | |
781aab86 | 95 | //~^ ERROR: transmute from a type (`*mut Usize`) to the type that it points to (`U |
f20569fa XL |
96 | |
97 | let _: *const Usize = core::intrinsics::transmute(my_int()); | |
781aab86 | 98 | //~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*const Usi |
f20569fa XL |
99 | |
100 | let _: *mut Usize = core::intrinsics::transmute(my_int()); | |
781aab86 | 101 | //~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize |
f20569fa XL |
102 | } |
103 | } | |
104 | ||
105 | #[warn(clippy::transmute_int_to_char)] | |
106 | fn int_to_char() { | |
107 | let _: char = unsafe { std::mem::transmute(0_u32) }; | |
781aab86 FG |
108 | //~^ ERROR: transmute from a `u32` to a `char` |
109 | //~| NOTE: `-D clippy::transmute-int-to-char` implied by `-D warnings` | |
f20569fa | 110 | let _: char = unsafe { std::mem::transmute(0_i32) }; |
781aab86 | 111 | //~^ ERROR: transmute from a `i32` to a `char` |
04454e1e FG |
112 | |
113 | // These shouldn't warn | |
114 | const _: char = unsafe { std::mem::transmute(0_u32) }; | |
115 | const _: char = unsafe { std::mem::transmute(0_i32) }; | |
f20569fa XL |
116 | } |
117 | ||
118 | #[warn(clippy::transmute_int_to_bool)] | |
119 | fn int_to_bool() { | |
120 | let _: bool = unsafe { std::mem::transmute(0_u8) }; | |
781aab86 FG |
121 | //~^ ERROR: transmute from a `u8` to a `bool` |
122 | //~| NOTE: `-D clippy::transmute-int-to-bool` implied by `-D warnings` | |
f20569fa XL |
123 | } |
124 | ||
125 | #[warn(clippy::transmute_int_to_float)] | |
126 | mod int_to_float { | |
127 | fn test() { | |
128 | let _: f32 = unsafe { std::mem::transmute(0_u32) }; | |
781aab86 FG |
129 | //~^ ERROR: transmute from a `u32` to a `f32` |
130 | //~| NOTE: `-D clippy::transmute-int-to-float` implied by `-D warnings` | |
f20569fa | 131 | let _: f32 = unsafe { std::mem::transmute(0_i32) }; |
781aab86 | 132 | //~^ ERROR: transmute from a `i32` to a `f32` |
f20569fa | 133 | let _: f64 = unsafe { std::mem::transmute(0_u64) }; |
781aab86 | 134 | //~^ ERROR: transmute from a `u64` to a `f64` |
f20569fa | 135 | let _: f64 = unsafe { std::mem::transmute(0_i64) }; |
781aab86 | 136 | //~^ ERROR: transmute from a `i64` to a `f64` |
f20569fa XL |
137 | } |
138 | ||
139 | mod issue_5747 { | |
140 | const VALUE32: f32 = unsafe { std::mem::transmute(0_u32) }; | |
141 | const VALUE64: f64 = unsafe { std::mem::transmute(0_i64) }; | |
142 | ||
143 | const fn from_bits_32(v: i32) -> f32 { | |
144 | unsafe { std::mem::transmute(v) } | |
145 | } | |
146 | ||
147 | const fn from_bits_64(v: u64) -> f64 { | |
148 | unsafe { std::mem::transmute(v) } | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
3c0e092e XL |
153 | mod num_to_bytes { |
154 | fn test() { | |
155 | unsafe { | |
156 | let _: [u8; 1] = std::mem::transmute(0u8); | |
781aab86 FG |
157 | //~^ ERROR: transmute from a `u8` to a `[u8; 1]` |
158 | //~| NOTE: `-D clippy::transmute-num-to-bytes` implied by `-D warnings` | |
3c0e092e | 159 | let _: [u8; 4] = std::mem::transmute(0u32); |
781aab86 | 160 | //~^ ERROR: transmute from a `u32` to a `[u8; 4]` |
3c0e092e | 161 | let _: [u8; 16] = std::mem::transmute(0u128); |
781aab86 | 162 | //~^ ERROR: transmute from a `u128` to a `[u8; 16]` |
3c0e092e | 163 | let _: [u8; 1] = std::mem::transmute(0i8); |
781aab86 | 164 | //~^ ERROR: transmute from a `i8` to a `[u8; 1]` |
3c0e092e | 165 | let _: [u8; 4] = std::mem::transmute(0i32); |
781aab86 | 166 | //~^ ERROR: transmute from a `i32` to a `[u8; 4]` |
3c0e092e | 167 | let _: [u8; 16] = std::mem::transmute(0i128); |
781aab86 | 168 | //~^ ERROR: transmute from a `i128` to a `[u8; 16]` |
3c0e092e | 169 | let _: [u8; 4] = std::mem::transmute(0.0f32); |
781aab86 | 170 | //~^ ERROR: transmute from a `f32` to a `[u8; 4]` |
3c0e092e | 171 | let _: [u8; 8] = std::mem::transmute(0.0f64); |
781aab86 | 172 | //~^ ERROR: transmute from a `f64` to a `[u8; 8]` |
3c0e092e XL |
173 | } |
174 | } | |
175 | const fn test_const() { | |
176 | unsafe { | |
177 | let _: [u8; 1] = std::mem::transmute(0u8); | |
781aab86 | 178 | //~^ ERROR: transmute from a `u8` to a `[u8; 1]` |
3c0e092e | 179 | let _: [u8; 4] = std::mem::transmute(0u32); |
781aab86 | 180 | //~^ ERROR: transmute from a `u32` to a `[u8; 4]` |
3c0e092e | 181 | let _: [u8; 16] = std::mem::transmute(0u128); |
781aab86 | 182 | //~^ ERROR: transmute from a `u128` to a `[u8; 16]` |
3c0e092e | 183 | let _: [u8; 1] = std::mem::transmute(0i8); |
781aab86 | 184 | //~^ ERROR: transmute from a `i8` to a `[u8; 1]` |
3c0e092e | 185 | let _: [u8; 4] = std::mem::transmute(0i32); |
781aab86 | 186 | //~^ ERROR: transmute from a `i32` to a `[u8; 4]` |
3c0e092e | 187 | let _: [u8; 16] = std::mem::transmute(0i128); |
781aab86 | 188 | //~^ ERROR: transmute from a `i128` to a `[u8; 16]` |
3c0e092e XL |
189 | let _: [u8; 4] = std::mem::transmute(0.0f32); |
190 | let _: [u8; 8] = std::mem::transmute(0.0f64); | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
04454e1e FG |
195 | fn bytes_to_str(mb: &mut [u8]) { |
196 | const B: &[u8] = b""; | |
197 | ||
198 | let _: &str = unsafe { std::mem::transmute(B) }; | |
781aab86 FG |
199 | //~^ ERROR: transmute from a `&[u8]` to a `&str` |
200 | //~| NOTE: `-D clippy::transmute-bytes-to-str` implied by `-D warnings` | |
f20569fa | 201 | let _: &mut str = unsafe { std::mem::transmute(mb) }; |
781aab86 | 202 | //~^ ERROR: transmute from a `&mut [u8]` to a `&mut str` |
04454e1e | 203 | const _: &str = unsafe { std::mem::transmute(B) }; |
781aab86 | 204 | //~^ ERROR: transmute from a `&[u8]` to a `&str` |
f20569fa XL |
205 | } |
206 | ||
207 | fn main() {} |