1
1
#![ warn( clippy:: transmute_ptr_to_ptr) ]
2
2
#![ allow( clippy:: borrow_as_ptr, clippy:: missing_transmute_annotations) ]
3
3
4
+ use std:: mem:: transmute;
5
+
4
6
// Make sure we can modify lifetimes, which is one of the recommended uses
5
7
// of transmute
6
8
7
9
// Make sure we can do static lifetime transmutes
8
10
unsafe 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)
10
12
}
11
13
12
14
// Make sure we can do non-static lifetime transmutes
13
15
unsafe 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)
15
17
}
16
18
17
19
struct LifetimeParam < ' a > {
@@ -27,47 +29,78 @@ fn transmute_ptr_to_ptr() {
27
29
let mut_ptr = & mut 1u32 as * mut u32 ;
28
30
unsafe {
29
31
// 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
35
36
// 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
40
41
//:^ this test is here because both f32 and f64 are the same TypeVariant, but they are not
41
42
// 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
46
47
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
49
54
}
50
55
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
-
58
56
// transmute internal lifetimes, should not lint
59
57
let s = "hello world" . to_owned ( ) ;
60
58
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
63
66
}
64
67
65
68
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
66
69
const _: & ( ) = {
67
70
struct Zst ;
68
71
let zst = & Zst ;
69
72
70
- unsafe { std :: mem :: transmute :: < & ' static Zst , & ' static ( ) > ( zst) }
73
+ unsafe { transmute :: < & ' static Zst , & ' static ( ) > ( zst) }
71
74
} ;
72
75
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
+
73
106
fn main ( ) { }
0 commit comments