Skip to content

Commit e9ceae4

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 5ac2656 commit e9ceae4

File tree

5 files changed

+116
-24
lines changed

5 files changed

+116
-24
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
555555
| transmute_ptr_to_ref::check(cx, e, from_field_ty, to_ty, from_field_expr.clone(), path, self.msrv)
556556
| missing_transmute_annotations::check(cx, path, arg, from_ty, to_ty, e.hir_id)
557557
| transmute_ref_to_ref::check(cx, e, from_ty, to_ty, arg, const_context)
558-
| transmute_ptr_to_ptr::check(cx, e, from_ty, to_ty, arg, self.msrv)
558+
| transmute_ptr_to_ptr::check(cx, e, from_field_ty, to_ty, from_field_expr, self.msrv)
559559
| transmute_int_to_bool::check(cx, e, from_ty, to_ty, arg)
560560
| transmute_int_to_non_zero::check(cx, e, from_ty, to_ty, arg)
561561
| (unsound_collection_transmute::check(cx, e, from_ty, to_ty)

clippy_lints/src/transmute/transmute_ptr_to_ptr.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ 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: sugg::Sugg<'_>,
1818
msrv: Msrv,
1919
) -> bool {
20-
let mut applicability = Applicability::MachineApplicable;
21-
let arg_sugg = sugg::Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut applicability);
2220
match (from_ty.kind(), to_ty.kind()) {
2321
(ty::RawPtr(from_pointee_ty, from_mutbl), ty::RawPtr(to_pointee_ty, to_mutbl)) => {
2422
span_lint_and_then(
@@ -34,7 +32,7 @@ pub(super) fn check<'tcx>(
3432
diag.span_suggestion_verbose(
3533
e.span,
3634
"use `pointer::cast` instead",
37-
format!("{}.cast::<{to_pointee_ty}>()", arg_sugg.maybe_paren()),
35+
format!("{}.cast::<{to_pointee_ty}>()", arg.maybe_paren()),
3836
Applicability::MaybeIncorrect,
3937
);
4038
} else if from_pointee_ty == to_pointee_ty
@@ -49,14 +47,14 @@ pub(super) fn check<'tcx>(
4947
diag.span_suggestion_verbose(
5048
e.span,
5149
format!("use `pointer::{method}` instead"),
52-
format!("{}.{method}()", arg_sugg.maybe_paren()),
50+
format!("{}.{method}()", arg.maybe_paren()),
5351
Applicability::MaybeIncorrect,
5452
);
5553
} else {
5654
diag.span_suggestion_verbose(
5755
e.span,
5856
"use an `as` cast instead",
59-
arg_sugg.as_ty(to_ty),
57+
arg.as_ty(to_ty),
6058
Applicability::MaybeIncorrect,
6159
);
6260
}

tests/ui/transmute_ptr_to_ptr.fixed

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

27+
#[derive(Clone, Copy)]
28+
struct PtrNamed {
29+
ptr: *const u32,
30+
}
31+
#[derive(Clone, Copy)]
32+
struct Ptr(*const u32);
33+
2734
fn transmute_ptr_to_ptr() {
2835
let ptr = &1u32 as *const u32;
2936
let mut_ptr = &mut 1u32 as *mut u32;
@@ -68,6 +75,18 @@ fn transmute_ptr_to_ptr() {
6875
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
6976
}
7077

78+
fn issue1966() {
79+
let ptr = &1u32 as *const u32;
80+
unsafe {
81+
let _: *const f32 = Ptr(ptr).0.cast::<f32>();
82+
//~^ transmute_ptr_to_ptr
83+
let _: *const f32 = PtrNamed { ptr }.ptr.cast::<f32>();
84+
//~^ transmute_ptr_to_ptr
85+
let _: *mut u32 = Ptr(ptr).0.cast_mut();
86+
//~^ transmute_ptr_to_ptr
87+
}
88+
}
89+
7190
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
7291
unsafe { v as *const &() }
7392
//~^ transmute_ptr_to_ptr
@@ -81,11 +100,15 @@ const _: &() = {
81100
unsafe { transmute::<&'static Zst, &'static ()>(zst) }
82101
};
83102

103+
#[derive(Clone, Copy)]
104+
struct Ptr8(*const u8);
84105
#[clippy::msrv = "1.37"]
85106
fn msrv_1_37(ptr: *const u8) {
86107
unsafe {
87108
let _: *const i8 = ptr as *const i8;
88109
//~^ transmute_ptr_to_ptr
110+
let _: *const i8 = Ptr8(ptr).0 as *const i8;
111+
//~^ transmute_ptr_to_ptr
89112
}
90113
}
91114

tests/ui/transmute_ptr_to_ptr.rs

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

27+
#[derive(Clone, Copy)]
28+
struct PtrNamed {
29+
ptr: *const u32,
30+
}
31+
#[derive(Clone, Copy)]
32+
struct Ptr(*const u32);
33+
2734
fn transmute_ptr_to_ptr() {
2835
let ptr = &1u32 as *const u32;
2936
let mut_ptr = &mut 1u32 as *mut u32;
@@ -68,6 +75,18 @@ fn transmute_ptr_to_ptr() {
6875
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
6976
}
7077

78+
fn issue1966() {
79+
let ptr = &1u32 as *const u32;
80+
unsafe {
81+
let _: *const f32 = transmute(Ptr(ptr));
82+
//~^ transmute_ptr_to_ptr
83+
let _: *const f32 = transmute(PtrNamed { ptr });
84+
//~^ transmute_ptr_to_ptr
85+
let _: *mut u32 = transmute(Ptr(ptr));
86+
//~^ transmute_ptr_to_ptr
87+
}
88+
}
89+
7190
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
7291
unsafe { transmute(v) }
7392
//~^ transmute_ptr_to_ptr
@@ -81,11 +100,15 @@ const _: &() = {
81100
unsafe { transmute::<&'static Zst, &'static ()>(zst) }
82101
};
83102

103+
#[derive(Clone, Copy)]
104+
struct Ptr8(*const u8);
84105
#[clippy::msrv = "1.37"]
85106
fn msrv_1_37(ptr: *const u8) {
86107
unsafe {
87108
let _: *const i8 = transmute(ptr);
88109
//~^ transmute_ptr_to_ptr
110+
let _: *const i8 = transmute(Ptr8(ptr));
111+
//~^ transmute_ptr_to_ptr
89112
}
90113
}
91114

tests/ui/transmute_ptr_to_ptr.stderr

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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:39:29
33
|
44
LL | let _: *const f32 = transmute(ptr);
55
| ^^^^^^^^^^^^^^
@@ -13,7 +13,7 @@ LL + let _: *const f32 = ptr.cast::<f32>();
1313
|
1414

1515
error: transmute from a pointer to a pointer
16-
--> tests/ui/transmute_ptr_to_ptr.rs:35:27
16+
--> tests/ui/transmute_ptr_to_ptr.rs:42:27
1717
|
1818
LL | let _: *mut f32 = transmute(mut_ptr);
1919
| ^^^^^^^^^^^^^^^^^^
@@ -25,37 +25,37 @@ LL + let _: *mut f32 = mut_ptr.cast::<f32>();
2525
|
2626

2727
error: transmute from a reference to a reference
28-
--> tests/ui/transmute_ptr_to_ptr.rs:39:23
28+
--> tests/ui/transmute_ptr_to_ptr.rs:46:23
2929
|
3030
LL | let _: &f32 = transmute(&1u32);
3131
| ^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)`
3232

3333
error: transmute from a reference to a reference
34-
--> tests/ui/transmute_ptr_to_ptr.rs:42:23
34+
--> tests/ui/transmute_ptr_to_ptr.rs:49:23
3535
|
3636
LL | let _: &f32 = transmute(&1f64);
3737
| ^^^^^^^^^^^^^^^^ help: try: `&*(&1f64 as *const f64 as *const f32)`
3838

3939
error: transmute from a reference to a reference
40-
--> tests/ui/transmute_ptr_to_ptr.rs:47:27
40+
--> tests/ui/transmute_ptr_to_ptr.rs:54:27
4141
|
4242
LL | let _: &mut f32 = transmute(&mut 1u32);
4343
| ^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)`
4444

4545
error: transmute from a reference to a reference
46-
--> tests/ui/transmute_ptr_to_ptr.rs:50:37
46+
--> tests/ui/transmute_ptr_to_ptr.rs:57:37
4747
|
4848
LL | let _: &GenericParam<f32> = transmute(&GenericParam { t: 1u32 });
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`
5050

5151
error: transmute from a reference to a reference
52-
--> tests/ui/transmute_ptr_to_ptr.rs:54:27
52+
--> tests/ui/transmute_ptr_to_ptr.rs:61:27
5353
|
5454
LL | let u8_ref: &u8 = transmute(u64_ref);
5555
| ^^^^^^^^^^^^^^^^^^ help: try: `&*(u64_ref as *const u64 as *const u8)`
5656

5757
error: transmute from a pointer to a pointer
58-
--> tests/ui/transmute_ptr_to_ptr.rs:57:29
58+
--> tests/ui/transmute_ptr_to_ptr.rs:64:29
5959
|
6060
LL | let _: *const u32 = transmute(mut_ptr);
6161
| ^^^^^^^^^^^^^^^^^^
@@ -67,7 +67,7 @@ LL + let _: *const u32 = mut_ptr.cast_const();
6767
|
6868

6969
error: transmute from a pointer to a pointer
70-
--> tests/ui/transmute_ptr_to_ptr.rs:60:27
70+
--> tests/ui/transmute_ptr_to_ptr.rs:67:27
7171
|
7272
LL | let _: *mut u32 = transmute(ptr);
7373
| ^^^^^^^^^^^^^^
@@ -79,7 +79,43 @@ LL + let _: *mut u32 = ptr.cast_mut();
7979
|
8080

8181
error: transmute from a pointer to a pointer
82-
--> tests/ui/transmute_ptr_to_ptr.rs:72:14
82+
--> tests/ui/transmute_ptr_to_ptr.rs:81:29
83+
|
84+
LL | let _: *const f32 = transmute(Ptr(ptr));
85+
| ^^^^^^^^^^^^^^^^^^^
86+
|
87+
help: use `pointer::cast` instead
88+
|
89+
LL - let _: *const f32 = transmute(Ptr(ptr));
90+
LL + let _: *const f32 = Ptr(ptr).0.cast::<f32>();
91+
|
92+
93+
error: transmute from a pointer to a pointer
94+
--> tests/ui/transmute_ptr_to_ptr.rs:83:29
95+
|
96+
LL | let _: *const f32 = transmute(PtrNamed { ptr });
97+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
98+
|
99+
help: use `pointer::cast` instead
100+
|
101+
LL - let _: *const f32 = transmute(PtrNamed { ptr });
102+
LL + let _: *const f32 = PtrNamed { ptr }.ptr.cast::<f32>();
103+
|
104+
105+
error: transmute from a pointer to a pointer
106+
--> tests/ui/transmute_ptr_to_ptr.rs:85:27
107+
|
108+
LL | let _: *mut u32 = transmute(Ptr(ptr));
109+
| ^^^^^^^^^^^^^^^^^^^
110+
|
111+
help: use `pointer::cast_mut` instead
112+
|
113+
LL - let _: *mut u32 = transmute(Ptr(ptr));
114+
LL + let _: *mut u32 = Ptr(ptr).0.cast_mut();
115+
|
116+
117+
error: transmute from a pointer to a pointer
118+
--> tests/ui/transmute_ptr_to_ptr.rs:91:14
83119
|
84120
LL | unsafe { transmute(v) }
85121
| ^^^^^^^^^^^^
@@ -91,7 +127,7 @@ LL + unsafe { v as *const &() }
91127
|
92128

93129
error: transmute from a pointer to a pointer
94-
--> tests/ui/transmute_ptr_to_ptr.rs:87:28
130+
--> tests/ui/transmute_ptr_to_ptr.rs:108:28
95131
|
96132
LL | let _: *const i8 = transmute(ptr);
97133
| ^^^^^^^^^^^^^^
@@ -103,7 +139,19 @@ 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:110:28
143+
|
144+
LL | let _: *const i8 = transmute(Ptr8(ptr));
145+
| ^^^^^^^^^^^^^^^^^^^^
146+
|
147+
help: use an `as` cast instead
148+
|
149+
LL - let _: *const i8 = transmute(Ptr8(ptr));
150+
LL + let _: *const i8 = Ptr8(ptr).0 as *const i8;
151+
|
152+
153+
error: transmute from a pointer to a pointer
154+
--> tests/ui/transmute_ptr_to_ptr.rs:118:28
107155
|
108156
LL | let _: *const i8 = transmute(ptr);
109157
| ^^^^^^^^^^^^^^
@@ -115,7 +163,7 @@ LL + let _: *const i8 = ptr.cast::<i8>();
115163
|
116164

117165
error: transmute from a pointer to a pointer
118-
--> tests/ui/transmute_ptr_to_ptr.rs:103:26
166+
--> tests/ui/transmute_ptr_to_ptr.rs:126:26
119167
|
120168
LL | let _: *mut u8 = transmute(ptr);
121169
| ^^^^^^^^^^^^^^
@@ -127,7 +175,7 @@ LL + let _: *mut u8 = ptr as *mut u8;
127175
|
128176

129177
error: transmute from a pointer to a pointer
130-
--> tests/ui/transmute_ptr_to_ptr.rs:105:28
178+
--> tests/ui/transmute_ptr_to_ptr.rs:128:28
131179
|
132180
LL | let _: *const u8 = transmute(mut_ptr);
133181
| ^^^^^^^^^^^^^^^^^^
@@ -139,7 +187,7 @@ LL + let _: *const u8 = mut_ptr as *const u8;
139187
|
140188

141189
error: transmute from a pointer to a pointer
142-
--> tests/ui/transmute_ptr_to_ptr.rs:113:26
190+
--> tests/ui/transmute_ptr_to_ptr.rs:136:26
143191
|
144192
LL | let _: *mut u8 = transmute(ptr);
145193
| ^^^^^^^^^^^^^^
@@ -151,7 +199,7 @@ LL + let _: *mut u8 = ptr.cast_mut();
151199
|
152200

153201
error: transmute from a pointer to a pointer
154-
--> tests/ui/transmute_ptr_to_ptr.rs:115:28
202+
--> tests/ui/transmute_ptr_to_ptr.rs:138:28
155203
|
156204
LL | let _: *const u8 = transmute(mut_ptr);
157205
| ^^^^^^^^^^^^^^^^^^
@@ -162,5 +210,5 @@ LL - let _: *const u8 = transmute(mut_ptr);
162210
LL + let _: *const u8 = mut_ptr.cast_const();
163211
|
164212

165-
error: aborting due to 16 previous errors
213+
error: aborting due to 20 previous errors
166214

0 commit comments

Comments
 (0)