]> git.lizzy.rs Git - rust.git/blob - tests/ui/transmute.rs
Merge pull request #3291 from JoshMcguigan/cmp_owned-3289
[rust.git] / tests / ui / transmute.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11
12
13
14 #![allow(dead_code)]
15
16 extern crate core;
17
18 use std::mem::transmute as my_transmute;
19 use std::vec::Vec as MyVec;
20
21 fn my_int() -> Usize {
22     Usize(42)
23 }
24
25 fn my_vec() -> MyVec<i32> {
26     vec![]
27 }
28
29 #[allow(clippy::needless_lifetimes, clippy::transmute_ptr_to_ptr)]
30 #[warn(clippy::useless_transmute)]
31 unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
32     let _: &'a T = core::intrinsics::transmute(t);
33
34     let _: &'a U = core::intrinsics::transmute(t);
35
36     let _: *const T = core::intrinsics::transmute(t);
37
38     let _: *mut T = core::intrinsics::transmute(t);
39
40     let _: *const U = core::intrinsics::transmute(t);
41 }
42
43 #[warn(clippy::transmute_ptr_to_ref)]
44 unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
45     let _: &T = std::mem::transmute(p);
46     let _: &T = &*p;
47
48     let _: &mut T = std::mem::transmute(m);
49     let _: &mut T = &mut *m;
50
51     let _: &T = std::mem::transmute(m);
52     let _: &T = &*m;
53
54     let _: &mut T = std::mem::transmute(p as *mut T);
55     let _ = &mut *(p as *mut T);
56
57     let _: &T = std::mem::transmute(o);
58     let _: &T = &*(o as *const T);
59
60     let _: &mut T = std::mem::transmute(om);
61     let _: &mut T = &mut *(om as *mut T);
62
63     let _: &T = std::mem::transmute(om);
64     let _: &T = &*(om as *const T);
65 }
66
67 #[warn(clippy::transmute_ptr_to_ref)]
68 fn issue1231() {
69     struct Foo<'a, T: 'a> {
70         bar: &'a T,
71     }
72
73     let raw = 42 as *const i32;
74     let _: &Foo<u8> = unsafe { std::mem::transmute::<_, &Foo<_>>(raw) };
75
76     let _: &Foo<&u8> = unsafe { std::mem::transmute::<_, &Foo<&_>>(raw) };
77
78     type Bar<'a> = &'a u8;
79     let raw = 42 as *const i32;
80     unsafe { std::mem::transmute::<_, Bar>(raw) };
81 }
82
83 #[warn(clippy::useless_transmute)]
84 fn useless() {
85     unsafe {
86         let _: Vec<i32> = core::intrinsics::transmute(my_vec());
87
88         let _: Vec<i32> = core::mem::transmute(my_vec());
89
90         let _: Vec<i32> = std::intrinsics::transmute(my_vec());
91
92         let _: Vec<i32> = std::mem::transmute(my_vec());
93
94         let _: Vec<i32> = my_transmute(my_vec());
95
96         let _: Vec<u32> = core::intrinsics::transmute(my_vec());
97         let _: Vec<u32> = core::mem::transmute(my_vec());
98         let _: Vec<u32> = std::intrinsics::transmute(my_vec());
99         let _: Vec<u32> = std::mem::transmute(my_vec());
100         let _: Vec<u32> = my_transmute(my_vec());
101
102         let _: *const usize = std::mem::transmute(5_isize);
103
104         let _  = 5_isize as *const usize;
105
106         let _: *const usize = std::mem::transmute(1+1usize);
107
108         let _  = (1+1_usize) as *const usize;
109     }
110 }
111
112 struct Usize(usize);
113
114 #[warn(clippy::crosspointer_transmute)]
115 fn crosspointer() {
116     let mut int: Usize = Usize(0);
117     let int_const_ptr: *const Usize = &int as *const Usize;
118     let int_mut_ptr: *mut Usize = &mut int as *mut Usize;
119
120     unsafe {
121         let _: Usize = core::intrinsics::transmute(int_const_ptr);
122
123         let _: Usize = core::intrinsics::transmute(int_mut_ptr);
124
125         let _: *const Usize = core::intrinsics::transmute(my_int());
126
127         let _: *mut Usize = core::intrinsics::transmute(my_int());
128     }
129 }
130
131 #[warn(clippy::transmute_int_to_char)]
132 fn int_to_char() {
133     let _: char = unsafe { std::mem::transmute(0_u32) };
134     let _: char = unsafe { std::mem::transmute(0_i32) };
135 }
136
137 #[warn(clippy::transmute_int_to_bool)]
138 fn int_to_bool() {
139     let _: bool = unsafe { std::mem::transmute(0_u8) };
140 }
141
142 #[warn(clippy::transmute_int_to_float)]
143 fn int_to_float() {
144     let _: f32 = unsafe { std::mem::transmute(0_u32) };
145     let _: f32 = unsafe { std::mem::transmute(0_i32) };
146 }
147
148 fn bytes_to_str(b: &[u8], mb: &mut [u8]) {
149     let _: &str = unsafe { std::mem::transmute(b) };
150     let _: &mut str = unsafe { std::mem::transmute(mb) };
151 }
152
153 // Make sure we can modify lifetimes, which is one of the recommended uses
154 // of transmute
155
156 // Make sure we can do static lifetime transmutes
157 #[warn(clippy::transmute_ptr_to_ptr)]
158 unsafe fn transmute_lifetime_to_static<'a, T>(t: &'a T) -> &'static T {
159     std::mem::transmute::<&'a T, &'static T>(t)
160 }
161
162 // Make sure we can do non-static lifetime transmutes
163 #[warn(clippy::transmute_ptr_to_ptr)]
164 unsafe fn transmute_lifetime<'a, 'b, T>(t: &'a T, u: &'b T) -> &'b T {
165     std::mem::transmute::<&'a T, &'b T>(t)
166 }
167
168 struct LifetimeParam<'a> {
169     s: &'a str,
170 }
171
172 struct GenericParam<T> {
173     t: T,
174 }
175
176 #[warn(clippy::transmute_ptr_to_ptr)]
177 fn transmute_ptr_to_ptr() {
178     let ptr = &1u32 as *const u32;
179     let mut_ptr = &mut 1u32 as *mut u32;
180     unsafe {
181         // pointer-to-pointer transmutes; bad
182         let _: *const f32 = std::mem::transmute(ptr);
183         let _: *mut f32 = std::mem::transmute(mut_ptr);
184         // ref-ref transmutes; bad
185         let _: &f32 = std::mem::transmute(&1u32);
186         let _: &f64 = std::mem::transmute(&1f32);
187         // ^ this test is here because both f32 and f64 are the same TypeVariant, but they are not
188         // the same type
189         let _: &mut f32 = std::mem::transmute(&mut 1u32);
190         let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
191     }
192
193     // these are recommendations for solving the above; if these lint we need to update
194     // those suggestions
195     let _ = ptr as *const f32;
196     let _ = mut_ptr as *mut f32;
197     let _ = unsafe { &*(&1u32 as *const u32 as *const f32) };
198     let _ = unsafe { &mut *(&mut 1u32 as *mut u32 as *mut f32) };
199
200     // transmute internal lifetimes, should not lint
201     let s = "hello world".to_owned();
202     let lp = LifetimeParam { s: &s };
203     let _: &LifetimeParam<'static> = unsafe { std::mem::transmute(&lp) };
204     let _: &GenericParam<&LifetimeParam<'static>> = unsafe {
205         std::mem::transmute(&GenericParam { t: &lp})
206     };
207 }
208
209 fn main() { }