11#![ warn( clippy:: transmute_ptr_to_ptr) ]
22#![ allow( clippy:: borrow_as_ptr, clippy:: missing_transmute_annotations) ]
33
4+ use std:: mem:: transmute;
5+
46// Make sure we can modify lifetimes, which is one of the recommended uses
57// of transmute
68
79// Make sure we can do static lifetime transmutes
810unsafe fn transmute_lifetime_to_static < ' a , T > ( t : & ' a T ) -> & ' static T {
9- std :: mem :: transmute :: < & ' a T , & ' static T > ( t)
11+ transmute :: < & ' a T , & ' static T > ( t)
1012}
1113
1214// Make sure we can do non-static lifetime transmutes
1315unsafe fn transmute_lifetime < ' a , ' b , T > ( t : & ' a T , u : & ' b T ) -> & ' b T {
14- std :: mem :: transmute :: < & ' a T , & ' b T > ( t)
16+ transmute :: < & ' a T , & ' b T > ( t)
1517}
1618
1719struct LifetimeParam < ' a > {
@@ -27,47 +29,73 @@ fn transmute_ptr_to_ptr() {
2729 let mut_ptr = & mut 1u32 as * mut u32 ;
2830 unsafe {
2931 // pointer-to-pointer transmutes; bad
30- let _: * const f32 = std:: mem:: transmute ( ptr) ;
31- //~^ ERROR: transmute from a pointer to a pointer
32- //~| NOTE: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
33- let _: * mut f32 = std:: mem:: transmute ( mut_ptr) ;
34- //~^ ERROR: transmute from a pointer to a pointer
32+ let _: * const f32 = transmute ( ptr) ;
33+ //~^ transmute_ptr_to_ptr
34+ let _: * mut f32 = transmute ( mut_ptr) ;
35+ //~^ transmute_ptr_to_ptr
3536 // ref-ref transmutes; bad
36- let _: & f32 = std :: mem :: transmute ( & 1u32 ) ;
37- //~^ ERROR: transmute from a reference to a reference
38- let _: & f32 = std :: mem :: transmute ( & 1f64 ) ;
39- //~^ ERROR: transmute from a reference to a reference
37+ let _: & f32 = transmute ( & 1u32 ) ;
38+ //~^ transmute_ptr_to_ptr
39+ let _: & f32 = transmute ( & 1f64 ) ;
40+ //~^ transmute_ptr_to_ptr
4041 //:^ this test is here because both f32 and f64 are the same TypeVariant, but they are not
4142 // the same type
42- let _: & mut f32 = std :: mem :: transmute ( & mut 1u32 ) ;
43- //~^ ERROR: transmute from a reference to a reference
44- let _: & GenericParam < f32 > = std :: mem :: transmute ( & GenericParam { t : 1u32 } ) ;
45- //~^ ERROR: transmute from a reference to a reference
43+ let _: & mut f32 = transmute ( & mut 1u32 ) ;
44+ //~^ transmute_ptr_to_ptr
45+ let _: & GenericParam < f32 > = transmute ( & GenericParam { t : 1u32 } ) ;
46+ //~^ transmute_ptr_to_ptr
4647 let u64_ref: & u64 = & 0u64 ;
47- let u8_ref: & u8 = unsafe { std:: mem:: transmute ( u64_ref) } ;
48- //~^ ERROR: transmute from a reference to a reference
48+ let u8_ref: & u8 = transmute ( u64_ref) ;
49+ //~^ transmute_ptr_to_ptr
50+ let _: * const u32 = transmute ( mut_ptr) ;
51+ //~^ transmute_ptr_to_ptr
52+ let _: * mut u32 = transmute ( ptr) ;
53+ //~^ transmute_ptr_to_ptr
4954 }
5055
51- // these are recommendations for solving the above; if these lint we need to update
52- // those suggestions
53- let _ = ptr as * const f32 ;
54- let _ = mut_ptr as * mut f32 ;
55- let _ = unsafe { & * ( & 1u32 as * const u32 as * const f32 ) } ;
56- let _ = unsafe { & mut * ( & mut 1u32 as * mut u32 as * mut f32 ) } ;
57-
5856 // transmute internal lifetimes, should not lint
5957 let s = "hello world" . to_owned ( ) ;
6058 let lp = LifetimeParam { s : & s } ;
61- let _: & LifetimeParam < ' static > = unsafe { std :: mem :: transmute ( & lp) } ;
62- let _: & GenericParam < & LifetimeParam < ' static > > = unsafe { std :: mem :: transmute ( & GenericParam { t : & lp } ) } ;
59+ let _: & LifetimeParam < ' static > = unsafe { transmute ( & lp) } ;
60+ let _: & GenericParam < & LifetimeParam < ' static > > = unsafe { transmute ( & GenericParam { t : & lp } ) } ;
6361}
6462
6563// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
6664const _: & ( ) = {
6765 struct Zst ;
6866 let zst = & Zst ;
6967
70- unsafe { std :: mem :: transmute :: < & ' static Zst , & ' static ( ) > ( zst) }
68+ unsafe { transmute :: < & ' static Zst , & ' static ( ) > ( zst) }
7169} ;
7270
71+ #[ clippy:: msrv = "1.37" ]
72+ fn msrv_1_37 ( ptr : * const u8 ) {
73+ unsafe {
74+ let _: * const i8 = transmute ( ptr) ;
75+ }
76+ }
77+
78+ #[ clippy:: msrv = "1.38" ]
79+ fn msrv_1_38 ( ptr : * const u8 ) {
80+ unsafe {
81+ let _: * const i8 = transmute ( ptr) ;
82+ }
83+ }
84+
85+ #[ clippy:: msrv = "1.64" ]
86+ fn msrv_1_64 ( ptr : * const u8 , mut_ptr : * mut u8 ) {
87+ unsafe {
88+ let _: * mut u8 = transmute ( ptr) ;
89+ let _: * const u8 = transmute ( mut_ptr) ;
90+ }
91+ }
92+
93+ #[ clippy:: msrv = "1.65" ]
94+ fn msrv_1_65 ( ptr : * const u8 , mut_ptr : * mut u8 ) {
95+ unsafe {
96+ let _: * mut u8 = transmute ( ptr) ;
97+ let _: * const u8 = transmute ( mut_ptr) ;
98+ }
99+ }
100+
73101fn main ( ) { }
0 commit comments