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,78 @@ 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 } ) } ;
61+ }
62+
63+ fn lifetime_to_static ( v : * mut & ( ) ) -> * const & ' static ( ) {
64+ unsafe { transmute ( v) }
65+ //~^ transmute_ptr_to_ptr
6366}
6467
6568// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
6669const _: & ( ) = {
6770 struct Zst ;
6871 let zst = & Zst ;
6972
70- unsafe { std :: mem :: transmute :: < & ' static Zst , & ' static ( ) > ( zst) }
73+ unsafe { transmute :: < & ' static Zst , & ' static ( ) > ( zst) }
7174} ;
7275
76+ #[ clippy:: msrv = "1.37" ]
77+ fn msrv_1_37 ( ptr : * const u8 ) {
78+ unsafe {
79+ let _: * const i8 = transmute ( ptr) ;
80+ }
81+ }
82+
83+ #[ clippy:: msrv = "1.38" ]
84+ fn msrv_1_38 ( ptr : * const u8 ) {
85+ unsafe {
86+ let _: * const i8 = transmute ( ptr) ;
87+ }
88+ }
89+
90+ #[ clippy:: msrv = "1.64" ]
91+ fn msrv_1_64 ( ptr : * const u8 , mut_ptr : * mut u8 ) {
92+ unsafe {
93+ let _: * mut u8 = transmute ( ptr) ;
94+ let _: * const u8 = transmute ( mut_ptr) ;
95+ }
96+ }
97+
98+ #[ clippy:: msrv = "1.65" ]
99+ fn msrv_1_65 ( ptr : * const u8 , mut_ptr : * mut u8 ) {
100+ unsafe {
101+ let _: * mut u8 = transmute ( ptr) ;
102+ let _: * const u8 = transmute ( mut_ptr) ;
103+ }
104+ }
105+
73106fn main ( ) { }
0 commit comments