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