Skip to content

Commit 90872c2

Browse files
author
ceptontech
committed
feat(transmute_ptr_to_ptr): Handle a pointer wrapped in a struct
Now the program checks for transmutting from a struct containing a single raw pointer to a raw pointer. changelog: [`transmute_ptr_to_ptr`]: now checks for a pointer wrapped in a struct
1 parent f2bc177 commit 90872c2

File tree

5 files changed

+78
-22
lines changed

5 files changed

+78
-22
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
549549
| transmute_ptr_to_ref::check(cx, e, from_field_ty, to_ty, from_field_expr.clone(), path, self.msrv)
550550
| missing_transmute_annotations::check(cx, path, arg, from_ty, to_ty, e.hir_id)
551551
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
552-
| transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg, self.msrv)
552+
| transmute_ptr_to_ptr::check(cx, e, from_field_ty, to_ty, from_field_expr, self.msrv)
553553
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)
554554
| transmute_int_to_non_zero::check(cx, e, from_ty, to_ty, arg)
555555
| (unsound_collection_transmute::check(cx, e, from_ty, to_ty)

clippy_lints/src/transmute/transmute_ptr_to_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
e: &'tcx Expr<'_>,
1515
from_ty: Ty<'tcx>,
1616
to_ty: Ty<'tcx>,
17-
arg: &'tcx Expr<'_>,
17+
arg: Option<sugg::Sugg<'_>>,
1818
msrv: Msrv,
1919
) -> bool {
2020
match (from_ty.kind(), to_ty.kind()) {
@@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(
2525
e.span,
2626
"transmute from a pointer to a pointer",
2727
|diag| {
28-
if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) {
28+
if let Some(arg) = arg {
2929
if from_mutbl == to_mutbl
3030
&& to_pointee_ty.is_sized(cx.tcx, cx.typing_env())
3131
&& msrv.meets(cx, msrvs::POINTER_CAST)

tests/ui/transmute_ptr_to_ptr.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ struct GenericParam<T> {
2424
t: T,
2525
}
2626

27+
#[derive(Clone, Copy)]
28+
struct Ptr(*const u32);
2729
fn transmute_ptr_to_ptr() {
2830
let ptr = &1u32 as *const u32;
2931
let mut_ptr = &mut 1u32 as *mut u32;
3032
unsafe {
3133
// pointer-to-pointer transmutes; bad
34+
let _: *const f32 = Ptr(ptr).0.cast::<f32>();
35+
//~^ transmute_ptr_to_ptr
3236
let _: *const f32 = ptr.cast::<f32>();
3337
//~^ transmute_ptr_to_ptr
3438

@@ -59,6 +63,8 @@ fn transmute_ptr_to_ptr() {
5963

6064
let _: *mut u32 = ptr.cast_mut();
6165
//~^ transmute_ptr_to_ptr
66+
let _: *mut u32 = Ptr(ptr).0.cast_mut();
67+
//~^ transmute_ptr_to_ptr
6268
}
6369

6470
// transmute internal lifetimes, should not lint
@@ -81,9 +87,13 @@ const _: &() = {
8187
unsafe { transmute::<&'static Zst, &'static ()>(zst) }
8288
};
8389

90+
#[derive(Clone, Copy)]
91+
struct Ptr8(*const u8);
8492
#[clippy::msrv = "1.37"]
8593
fn msrv_1_37(ptr: *const u8) {
8694
unsafe {
95+
let _: *const i8 = Ptr8(ptr).0 as *const i8;
96+
//~^ transmute_ptr_to_ptr
8797
let _: *const i8 = ptr as *const i8;
8898
//~^ transmute_ptr_to_ptr
8999
}

tests/ui/transmute_ptr_to_ptr.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ struct GenericParam<T> {
2424
t: T,
2525
}
2626

27+
#[derive(Clone, Copy)]
28+
struct Ptr(*const u32);
2729
fn transmute_ptr_to_ptr() {
2830
let ptr = &1u32 as *const u32;
2931
let mut_ptr = &mut 1u32 as *mut u32;
3032
unsafe {
3133
// pointer-to-pointer transmutes; bad
34+
let _: *const f32 = transmute(Ptr(ptr));
35+
//~^ transmute_ptr_to_ptr
3236
let _: *const f32 = transmute(ptr);
3337
//~^ transmute_ptr_to_ptr
3438

@@ -59,6 +63,8 @@ fn transmute_ptr_to_ptr() {
5963

6064
let _: *mut u32 = transmute(ptr);
6165
//~^ transmute_ptr_to_ptr
66+
let _: *mut u32 = transmute(Ptr(ptr));
67+
//~^ transmute_ptr_to_ptr
6268
}
6369

6470
// transmute internal lifetimes, should not lint
@@ -81,9 +87,13 @@ const _: &() = {
8187
unsafe { transmute::<&'static Zst, &'static ()>(zst) }
8288
};
8389

90+
#[derive(Clone, Copy)]
91+
struct Ptr8(*const u8);
8492
#[clippy::msrv = "1.37"]
8593
fn msrv_1_37(ptr: *const u8) {
8694
unsafe {
95+
let _: *const i8 = transmute(Ptr8(ptr));
96+
//~^ transmute_ptr_to_ptr
8797
let _: *const i8 = transmute(ptr);
8898
//~^ transmute_ptr_to_ptr
8999
}

tests/ui/transmute_ptr_to_ptr.stderr

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
error: transmute from a pointer to a pointer
2-
--> tests/ui/transmute_ptr_to_ptr.rs:32:29
2+
--> tests/ui/transmute_ptr_to_ptr.rs:34:29
33
|
4-
LL | let _: *const f32 = transmute(ptr);
5-
| ^^^^^^^^^^^^^^
4+
LL | let _: *const f32 = transmute(Ptr(ptr));
5+
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::transmute_ptr_to_ptr)]`
99
help: use `pointer::cast` instead
1010
|
11+
LL - let _: *const f32 = transmute(Ptr(ptr));
12+
LL + let _: *const f32 = Ptr(ptr).0.cast::<f32>();
13+
|
14+
15+
error: transmute from a pointer to a pointer
16+
--> tests/ui/transmute_ptr_to_ptr.rs:36:29
17+
|
18+
LL | let _: *const f32 = transmute(ptr);
19+
| ^^^^^^^^^^^^^^
20+
|
21+
help: use `pointer::cast` instead
22+
|
1123
LL - let _: *const f32 = transmute(ptr);
1224
LL + let _: *const f32 = ptr.cast::<f32>();
1325
|
1426

1527
error: transmute from a pointer to a pointer
16-
--> tests/ui/transmute_ptr_to_ptr.rs:35:27
28+
--> tests/ui/transmute_ptr_to_ptr.rs:39:27
1729
|
1830
LL | let _: *mut f32 = transmute(mut_ptr);
1931
| ^^^^^^^^^^^^^^^^^^
@@ -25,37 +37,37 @@ LL + let _: *mut f32 = mut_ptr.cast::<f32>();
2537
|
2638

2739
error: transmute from a reference to a reference
28-
--> tests/ui/transmute_ptr_to_ptr.rs:39:23
40+
--> tests/ui/transmute_ptr_to_ptr.rs:43:23
2941
|
3042
LL | let _: &f32 = transmute(&1u32);
3143
| ^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)`
3244

3345
error: transmute from a reference to a reference
34-
--> tests/ui/transmute_ptr_to_ptr.rs:42:23
46+
--> tests/ui/transmute_ptr_to_ptr.rs:46:23
3547
|
3648
LL | let _: &f32 = transmute(&1f64);
3749
| ^^^^^^^^^^^^^^^^ help: try: `&*(&1f64 as *const f64 as *const f32)`
3850

3951
error: transmute from a reference to a reference
40-
--> tests/ui/transmute_ptr_to_ptr.rs:47:27
52+
--> tests/ui/transmute_ptr_to_ptr.rs:51:27
4153
|
4254
LL | let _: &mut f32 = transmute(&mut 1u32);
4355
| ^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)`
4456

4557
error: transmute from a reference to a reference
46-
--> tests/ui/transmute_ptr_to_ptr.rs:50:37
58+
--> tests/ui/transmute_ptr_to_ptr.rs:54:37
4759
|
4860
LL | let _: &GenericParam<f32> = transmute(&GenericParam { t: 1u32 });
4961
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`
5062

5163
error: transmute from a reference to a reference
52-
--> tests/ui/transmute_ptr_to_ptr.rs:54:27
64+
--> tests/ui/transmute_ptr_to_ptr.rs:58:27
5365
|
5466
LL | let u8_ref: &u8 = transmute(u64_ref);
5567
| ^^^^^^^^^^^^^^^^^^ help: try: `&*(u64_ref as *const u64 as *const u8)`
5668

5769
error: transmute from a pointer to a pointer
58-
--> tests/ui/transmute_ptr_to_ptr.rs:57:29
70+
--> tests/ui/transmute_ptr_to_ptr.rs:61:29
5971
|
6072
LL | let _: *const u32 = transmute(mut_ptr);
6173
| ^^^^^^^^^^^^^^^^^^
@@ -67,7 +79,7 @@ LL + let _: *const u32 = mut_ptr.cast_const();
6779
|
6880

6981
error: transmute from a pointer to a pointer
70-
--> tests/ui/transmute_ptr_to_ptr.rs:60:27
82+
--> tests/ui/transmute_ptr_to_ptr.rs:64:27
7183
|
7284
LL | let _: *mut u32 = transmute(ptr);
7385
| ^^^^^^^^^^^^^^
@@ -79,7 +91,19 @@ LL + let _: *mut u32 = ptr.cast_mut();
7991
|
8092

8193
error: transmute from a pointer to a pointer
82-
--> tests/ui/transmute_ptr_to_ptr.rs:72:14
94+
--> tests/ui/transmute_ptr_to_ptr.rs:66:27
95+
|
96+
LL | let _: *mut u32 = transmute(Ptr(ptr));
97+
| ^^^^^^^^^^^^^^^^^^^
98+
|
99+
help: use `pointer::cast_mut` instead
100+
|
101+
LL - let _: *mut u32 = transmute(Ptr(ptr));
102+
LL + let _: *mut u32 = Ptr(ptr).0.cast_mut();
103+
|
104+
105+
error: transmute from a pointer to a pointer
106+
--> tests/ui/transmute_ptr_to_ptr.rs:78:14
83107
|
84108
LL | unsafe { transmute(v) }
85109
| ^^^^^^^^^^^^
@@ -91,7 +115,19 @@ LL + unsafe { v as *const &() }
91115
|
92116

93117
error: transmute from a pointer to a pointer
94-
--> tests/ui/transmute_ptr_to_ptr.rs:87:28
118+
--> tests/ui/transmute_ptr_to_ptr.rs:95:28
119+
|
120+
LL | let _: *const i8 = transmute(Ptr8(ptr));
121+
| ^^^^^^^^^^^^^^^^^^^^
122+
|
123+
help: use an `as` cast instead
124+
|
125+
LL - let _: *const i8 = transmute(Ptr8(ptr));
126+
LL + let _: *const i8 = Ptr8(ptr).0 as *const i8;
127+
|
128+
129+
error: transmute from a pointer to a pointer
130+
--> tests/ui/transmute_ptr_to_ptr.rs:97:28
95131
|
96132
LL | let _: *const i8 = transmute(ptr);
97133
| ^^^^^^^^^^^^^^
@@ -103,7 +139,7 @@ LL + let _: *const i8 = ptr as *const i8;
103139
|
104140

105141
error: transmute from a pointer to a pointer
106-
--> tests/ui/transmute_ptr_to_ptr.rs:95:28
142+
--> tests/ui/transmute_ptr_to_ptr.rs:105:28
107143
|
108144
LL | let _: *const i8 = transmute(ptr);
109145
| ^^^^^^^^^^^^^^
@@ -115,7 +151,7 @@ LL + let _: *const i8 = ptr.cast::<i8>();
115151
|
116152

117153
error: transmute from a pointer to a pointer
118-
--> tests/ui/transmute_ptr_to_ptr.rs:103:26
154+
--> tests/ui/transmute_ptr_to_ptr.rs:113:26
119155
|
120156
LL | let _: *mut u8 = transmute(ptr);
121157
| ^^^^^^^^^^^^^^
@@ -127,7 +163,7 @@ LL + let _: *mut u8 = ptr as *mut u8;
127163
|
128164

129165
error: transmute from a pointer to a pointer
130-
--> tests/ui/transmute_ptr_to_ptr.rs:105:28
166+
--> tests/ui/transmute_ptr_to_ptr.rs:115:28
131167
|
132168
LL | let _: *const u8 = transmute(mut_ptr);
133169
| ^^^^^^^^^^^^^^^^^^
@@ -139,7 +175,7 @@ LL + let _: *const u8 = mut_ptr as *const u8;
139175
|
140176

141177
error: transmute from a pointer to a pointer
142-
--> tests/ui/transmute_ptr_to_ptr.rs:113:26
178+
--> tests/ui/transmute_ptr_to_ptr.rs:123:26
143179
|
144180
LL | let _: *mut u8 = transmute(ptr);
145181
| ^^^^^^^^^^^^^^
@@ -151,7 +187,7 @@ LL + let _: *mut u8 = ptr.cast_mut();
151187
|
152188

153189
error: transmute from a pointer to a pointer
154-
--> tests/ui/transmute_ptr_to_ptr.rs:115:28
190+
--> tests/ui/transmute_ptr_to_ptr.rs:125:28
155191
|
156192
LL | let _: *const u8 = transmute(mut_ptr);
157193
| ^^^^^^^^^^^^^^^^^^
@@ -162,5 +198,5 @@ LL - let _: *const u8 = transmute(mut_ptr);
162198
LL + let _: *const u8 = mut_ptr.cast_const();
163199
|
164200

165-
error: aborting due to 16 previous errors
201+
error: aborting due to 19 previous errors
166202

0 commit comments

Comments
 (0)