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