Skip to content

Commit 1da4959

Browse files
committed
Prefer verbose suggestions for integer_to_ptr_transmutes lint
1 parent 02a67cc commit 1da4959

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,8 @@ pub(crate) struct IntegerToPtrTransmutes<'tcx> {
15611561
pub(crate) enum IntegerToPtrTransmutesSuggestion<'tcx> {
15621562
#[multipart_suggestion(
15631563
lint_suggestion_with_exposed_provenance,
1564-
applicability = "machine-applicable"
1564+
applicability = "machine-applicable",
1565+
style = "verbose"
15651566
)]
15661567
ToPtr {
15671568
dst: Ty<'tcx>,
@@ -1571,7 +1572,8 @@ pub(crate) enum IntegerToPtrTransmutesSuggestion<'tcx> {
15711572
},
15721573
#[multipart_suggestion(
15731574
lint_suggestion_with_exposed_provenance,
1574-
applicability = "machine-applicable"
1575+
applicability = "machine-applicable",
1576+
style = "verbose"
15751577
)]
15761578
ToRef {
15771579
dst: Ty<'tcx>,

tests/ui/lint/int_to_ptr.stderr

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,170 +2,206 @@ warning: transmuting an integer to a pointer creates a pointer without provenanc
22
--> $DIR/int_to_ptr.rs:10:36
33
|
44
LL | let _ptr: *const u8 = unsafe { std::mem::transmute::<usize, *const u8>(a) };
5-
| ----------------------------------------^^
6-
| |
7-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance::<u8>(`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86
|
97
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
108
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
119
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
1210
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
1311
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
1412
= note: `#[warn(integer_to_ptr_transmutes)]` on by default
13+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
14+
|
15+
LL - let _ptr: *const u8 = unsafe { std::mem::transmute::<usize, *const u8>(a) };
16+
LL + let _ptr: *const u8 = unsafe { std::ptr::with_exposed_provenance::<u8>(a) };
17+
|
1518

1619
warning: transmuting an integer to a pointer creates a pointer without provenance
1720
--> $DIR/int_to_ptr.rs:12:34
1821
|
1922
LL | let _ptr: *mut u8 = unsafe { std::mem::transmute::<usize, *mut u8>(a) };
20-
| --------------------------------------^^
21-
| |
22-
| help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance_mut::<u8>(`
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2324
|
2425
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
2526
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
2627
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
2728
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
2829
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
30+
help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance
31+
|
32+
LL - let _ptr: *mut u8 = unsafe { std::mem::transmute::<usize, *mut u8>(a) };
33+
LL + let _ptr: *mut u8 = unsafe { std::ptr::with_exposed_provenance_mut::<u8>(a) };
34+
|
2935

3036
warning: transmuting an integer to a pointer creates a pointer without provenance
3137
--> $DIR/int_to_ptr.rs:14:38
3238
|
3339
LL | let _ref: &'static u8 = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
34-
| ------------------------------------------^^
35-
| |
36-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `&*std::ptr::with_exposed_provenance::<u8>(`
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3741
|
3842
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
3943
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
4044
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
4145
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
4246
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
47+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
48+
|
49+
LL - let _ref: &'static u8 = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
50+
LL + let _ref: &'static u8 = unsafe { &*std::ptr::with_exposed_provenance::<u8>(a) };
51+
|
4352

4453
warning: transmuting an integer to a pointer creates a pointer without provenance
4554
--> $DIR/int_to_ptr.rs:16:42
4655
|
4756
LL | let _ref: &'static mut u8 = unsafe { std::mem::transmute::<usize, &'static mut u8>(a) };
48-
| ----------------------------------------------^^
49-
| |
50-
| help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance: `&mut *std::ptr::with_exposed_provenance_mut::<u8>(`
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5158
|
5259
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
5360
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
5461
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
5562
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
5663
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
64+
help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance
65+
|
66+
LL - let _ref: &'static mut u8 = unsafe { std::mem::transmute::<usize, &'static mut u8>(a) };
67+
LL + let _ref: &'static mut u8 = unsafe { &mut *std::ptr::with_exposed_provenance_mut::<u8>(a) };
68+
|
5769

5870
warning: transmuting an integer to a pointer creates a pointer without provenance
5971
--> $DIR/int_to_ptr.rs:19:25
6072
|
6173
LL | let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
62-
| ----------------------------------------^^^^^^^^
63-
| |
64-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance::<u8>(`
74+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6575
|
6676
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
6777
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
6878
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
6979
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
7080
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
81+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
82+
|
83+
LL - let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
84+
LL + let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(42usize) };
85+
|
7186

7287
warning: transmuting an integer to a pointer creates a pointer without provenance
7388
--> $DIR/int_to_ptr.rs:21:25
7489
|
7590
LL | let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
76-
| ----------------------------------------^^^^^^
77-
| |
78-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance::<u8>(`
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7992
|
8093
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
8194
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
8295
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
8396
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
8497
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
98+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
99+
|
100+
LL - let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
101+
LL + let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(a + a) };
102+
|
85103

86104
warning: transmuting an integer to a pointer creates a pointer without provenance
87105
--> $DIR/int_to_ptr.rs:26:36
88106
|
89107
LL | let _ptr: *const u8 = unsafe { std::mem::transmute::<usize, *const u8>(a) };
90-
| ----------------------------------------^^
91-
| |
92-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance::<u8>(`
108+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
93109
|
94110
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
95111
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
96112
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
97113
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
98114
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
115+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
116+
|
117+
LL - let _ptr: *const u8 = unsafe { std::mem::transmute::<usize, *const u8>(a) };
118+
LL + let _ptr: *const u8 = unsafe { std::ptr::with_exposed_provenance::<u8>(a) };
119+
|
99120

100121
warning: transmuting an integer to a pointer creates a pointer without provenance
101122
--> $DIR/int_to_ptr.rs:28:34
102123
|
103124
LL | let _ptr: *mut u8 = unsafe { std::mem::transmute::<usize, *mut u8>(a) };
104-
| --------------------------------------^^
105-
| |
106-
| help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance_mut::<u8>(`
125+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107126
|
108127
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
109128
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
110129
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
111130
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
112131
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
132+
help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance
133+
|
134+
LL - let _ptr: *mut u8 = unsafe { std::mem::transmute::<usize, *mut u8>(a) };
135+
LL + let _ptr: *mut u8 = unsafe { std::ptr::with_exposed_provenance_mut::<u8>(a) };
136+
|
113137

114138
warning: transmuting an integer to a pointer creates a pointer without provenance
115139
--> $DIR/int_to_ptr.rs:30:38
116140
|
117141
LL | let _ref: &'static u8 = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
118-
| ------------------------------------------^^
119-
| |
120-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `&*std::ptr::with_exposed_provenance::<u8>(`
142+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121143
|
122144
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
123145
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
124146
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
125147
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
126148
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
149+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
150+
|
151+
LL - let _ref: &'static u8 = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
152+
LL + let _ref: &'static u8 = unsafe { &*std::ptr::with_exposed_provenance::<u8>(a) };
153+
|
127154

128155
warning: transmuting an integer to a pointer creates a pointer without provenance
129156
--> $DIR/int_to_ptr.rs:32:42
130157
|
131158
LL | let _ref: &'static mut u8 = unsafe { std::mem::transmute::<usize, &'static mut u8>(a) };
132-
| ----------------------------------------------^^
133-
| |
134-
| help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance: `&mut *std::ptr::with_exposed_provenance_mut::<u8>(`
159+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135160
|
136161
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
137162
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
138163
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
139164
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
140165
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
166+
help: use `std::ptr::with_exposed_provenance_mut` instead to use a previously exposed provenance
167+
|
168+
LL - let _ref: &'static mut u8 = unsafe { std::mem::transmute::<usize, &'static mut u8>(a) };
169+
LL + let _ref: &'static mut u8 = unsafe { &mut *std::ptr::with_exposed_provenance_mut::<u8>(a) };
170+
|
141171

142172
warning: transmuting an integer to a pointer creates a pointer without provenance
143173
--> $DIR/int_to_ptr.rs:35:25
144174
|
145175
LL | let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
146-
| ----------------------------------------^^^^^^^^
147-
| |
148-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance::<u8>(`
176+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
149177
|
150178
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
151179
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
152180
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
153181
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
154182
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
183+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
184+
|
185+
LL - let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
186+
LL + let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(42usize) };
187+
|
155188

156189
warning: transmuting an integer to a pointer creates a pointer without provenance
157190
--> $DIR/int_to_ptr.rs:37:25
158191
|
159192
LL | let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
160-
| ----------------------------------------^^^^^^
161-
| |
162-
| help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance: `std::ptr::with_exposed_provenance::<u8>(`
193+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163194
|
164195
= note: this is dangerous because dereferencing the resulting pointer is undefined behavior
165196
= note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
166197
= help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
167198
= help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
168199
= help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
200+
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
201+
|
202+
LL - let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
203+
LL + let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(a + a) };
204+
|
169205

170206
warning: 12 warnings emitted
171207

0 commit comments

Comments
 (0)