4 clippy
::needless_lifetimes
,
5 clippy
::missing_transmute_annotations
10 use std
::mem
::transmute
as my_transmute
;
11 use std
::vec
::Vec
as MyVec
;
13 fn my_int() -> Usize
{
17 fn my_vec() -> MyVec
<i32> {
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
) {
25 // let _: &'a T = core::intrinsics::transmute(t);
27 let _
: &'a U
= core
::intrinsics
::transmute(t
);
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`
33 let _
: *mut T
= core
::intrinsics
::transmute(t
);
34 //~^ ERROR: transmute from a reference to a pointer
36 let _
: *const U
= core
::intrinsics
::transmute(t
);
37 //~^ ERROR: transmute from a reference to a pointer
40 #[warn(clippy::useless_transmute)]
43 let _
: Vec
<i32> = core
::intrinsics
::transmute(my_vec());
44 //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
46 let _
: Vec
<i32> = core
::mem
::transmute(my_vec());
47 //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
49 let _
: Vec
<i32> = std
::intrinsics
::transmute(my_vec());
50 //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
52 let _
: Vec
<i32> = std
::mem
::transmute(my_vec());
53 //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
55 let _
: Vec
<i32> = my_transmute(my_vec());
56 //~^ ERROR: transmute from a type (`std::vec::Vec<i32>`) to itself
58 let _
: *const usize = std
::mem
::transmute(5_isize
);
59 //~^ ERROR: transmute from an integer to a pointer
61 let _
= 5_isize
as *const usize;
63 let _
: *const usize = std
::mem
::transmute(1 + 1usize
);
64 //~^ ERROR: transmute from an integer to a pointer
66 let _
= (1 + 1_usize
) as *const usize;
69 unsafe fn _f
<'a
, 'b
>(x
: &'a
u32) -> &'b
u32 {
70 std
::mem
::transmute(x
)
73 unsafe fn _f2
<'a
, 'b
>(x
: *const (dyn Iterator
<Item
= u32> + 'a
)) -> *const (dyn Iterator
<Item
= u32> + 'b
) {
74 std
::mem
::transmute(x
)
77 unsafe fn _f3
<'a
, 'b
>(x
: fn(&'a
u32)) -> fn(&'b
u32) {
78 std
::mem
::transmute(x
)
81 unsafe fn _f4
<'a
, 'b
>(x
: std
::borrow
::Cow
<'a
, str>) -> std
::borrow
::Cow
<'b
, str> {
82 std
::mem
::transmute(x
)
88 #[warn(clippy::crosspointer_transmute)]
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
;
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`
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
102 let _
: *const Usize
= core
::intrinsics
::transmute(my_int());
103 //~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*const Usi
105 let _
: *mut Usize
= core
::intrinsics
::transmute(my_int());
106 //~^ ERROR: transmute from a type (`Usize`) to a pointer to that type (`*mut Usize
110 #[warn(clippy::transmute_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`
117 #[warn(clippy::transmute_int_to_float)]
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`
132 const VALUE32
: f32 = unsafe { std::mem::transmute(0_u32) }
;
133 const VALUE64
: f64 = unsafe { std::mem::transmute(0_i64) }
;
135 const fn from_bits_32(v
: i32) -> f32 {
136 unsafe { std::mem::transmute(v) }
139 const fn from_bits_64(v
: u64) -> f64 {
140 unsafe { std::mem::transmute(v) }
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]`
167 const fn test_const() {
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);
187 fn bytes_to_str(mb
: &mut [u8]) {
188 const B
: &[u8] = b
"";
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`