]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/transmute.rs
Lint transmute from ptr to ref
[rust.git] / tests / compile-fail / transmute.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 extern crate core;
5
6 use std::mem::transmute as my_transmute;
7 use std::vec::Vec as MyVec;
8
9 fn my_vec() -> MyVec<i32> {
10     vec![]
11 }
12
13 #[allow(needless_lifetimes)]
14 #[deny(useless_transmute)]
15 unsafe fn _generic<'a, T, U: 'a>(t: &'a T) {
16     let _: &'a T = core::intrinsics::transmute(t);
17     //~^ ERROR transmute from a type (`&'a T`) to itself
18
19     let _: &'a U = core::intrinsics::transmute(t);
20 }
21
22 #[deny(transmute_ptr_to_ref)]
23 unsafe fn _ptr_to_ref<T, U>(p: *const T, m: *mut T, o: *const U, om: *mut U) {
24     let _: &T = std::mem::transmute(p);
25     //~^ ERROR transmute from a pointer type (`*const T`) to a reference type (`&T`)
26     //~| HELP try
27     //~| SUGGESTION = &*p;
28     let _: &T = &*p;
29
30     let _: &mut T = std::mem::transmute(m);
31     //~^ ERROR transmute from a pointer type (`*mut T`) to a reference type (`&mut T`)
32     //~| HELP try
33     //~| SUGGESTION = &mut *m;
34     let _: &mut T = &mut *m;
35
36     let _: &T = std::mem::transmute(m);
37     //~^ ERROR transmute from a pointer type (`*mut T`) to a reference type (`&T`)
38     //~| HELP try
39     //~| SUGGESTION = &*m;
40     let _: &T = &*m;
41
42     let _: &T = std::mem::transmute(o);
43     //~^ ERROR transmute from a pointer type (`*const U`) to a reference type (`&T`)
44     //~| HELP try
45     //~| SUGGESTION = &*(o as *const T);
46     let _: &T = &*(o as *const T);
47
48     let _: &mut T = std::mem::transmute(om);
49     //~^ ERROR transmute from a pointer type (`*mut U`) to a reference type (`&mut T`)
50     //~| HELP try
51     //~| SUGGESTION = &mut *(om as *mut T);
52     let _: &mut T = &mut *(om as *mut T);
53
54     let _: &T = std::mem::transmute(om);
55     //~^ ERROR transmute from a pointer type (`*mut U`) to a reference type (`&T`)
56     //~| HELP try
57     //~| SUGGESTION = &*(om as *const T);
58     let _: &T = &*(om as *const T);
59 }
60
61 #[deny(useless_transmute)]
62 fn useless() {
63     unsafe {
64         let _: Vec<i32> = core::intrinsics::transmute(my_vec());
65         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
66
67         let _: Vec<i32> = core::mem::transmute(my_vec());
68         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
69
70         let _: Vec<i32> = std::intrinsics::transmute(my_vec());
71         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
72
73         let _: Vec<i32> = std::mem::transmute(my_vec());
74         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
75
76         let _: Vec<i32> = my_transmute(my_vec());
77         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
78
79         let _: Vec<u32> = core::intrinsics::transmute(my_vec());
80         let _: Vec<u32> = core::mem::transmute(my_vec());
81         let _: Vec<u32> = std::intrinsics::transmute(my_vec());
82         let _: Vec<u32> = std::mem::transmute(my_vec());
83         let _: Vec<u32> = my_transmute(my_vec());
84     }
85 }
86
87 #[deny(crosspointer_transmute)]
88 fn crosspointer() {
89     let mut vec: Vec<i32> = vec![];
90     let vec_const_ptr: *const Vec<i32> = &vec as *const Vec<i32>;
91     let vec_mut_ptr: *mut Vec<i32> = &mut vec as *mut Vec<i32>;
92
93     unsafe {
94         let _: Vec<i32> = core::intrinsics::transmute(vec_const_ptr);
95         //~^ ERROR transmute from a type (`*const collections::vec::Vec<i32>`) to the type that it points to (`collections::vec::Vec<i32>`)
96
97         let _: Vec<i32> = core::intrinsics::transmute(vec_mut_ptr);
98         //~^ ERROR transmute from a type (`*mut collections::vec::Vec<i32>`) to the type that it points to (`collections::vec::Vec<i32>`)
99
100         let _: *const Vec<i32> = core::intrinsics::transmute(my_vec());
101         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to a pointer to that type (`*const collections::vec::Vec<i32>`)
102
103         let _: *mut Vec<i32> = core::intrinsics::transmute(my_vec());
104         //~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to a pointer to that type (`*mut collections::vec::Vec<i32>`)
105     }
106 }
107
108 fn main() {
109     useless();
110     crosspointer();
111 }