@@ -90,13 +90,15 @@ declare_lint! {
90
90
/// Any attempt to use the resulting pointers are undefined behavior as the resulting
91
91
/// pointers won't have any provenance.
92
92
///
93
- /// Alternatively, ` as` casts should be used, as they do not carry the provenance
94
- /// requirement or if the wanting to create pointers without provenance
95
- /// ` ptr::without_provenance_mut` should be used.
93
+ /// Alternatively, [`std::ptr::with_exposed_provenance`] or ` as` casts should be used,
94
+ /// as they do not carry the provenance requirement. If the wanting to create pointers
95
+ /// without provenance [`std:: ptr::without_provenance`] should be used instead .
96
96
///
97
- /// See [std::mem::transmute] in the reference for more details.
97
+ /// See [` std::mem::transmute` ] in the reference for more details.
98
98
///
99
- /// [std::mem::transmute]: https://doc.rust-lang.org/std/mem/fn.transmute.html
99
+ /// [`std::mem::transmute`]: https://doc.rust-lang.org/std/mem/fn.transmute.html
100
+ /// [`std::ptr::with_exposed_provenance`]: https://doc.rust-lang.org/std/ptr/fn.with_exposed_provenance.html
101
+ /// [`std::ptr::without_provenance`]: https://doc.rust-lang.org/std/ptr/fn.without_provenance.html
100
102
pub INTEGER_TO_PTR_TRANSMUTES ,
101
103
Warn ,
102
104
"detects integer to pointer transmutes" ,
@@ -152,36 +154,56 @@ fn check_int_to_ptr_transmute<'tcx>(
152
154
. layout_of ( cx. typing_env ( ) . as_query_input ( * inner_ty) )
153
155
. is_ok_and ( |layout| !layout. is_1zst ( ) )
154
156
{
155
- // does the argument needs parenthesis
156
- let mut paren_left = "" ;
157
- let mut paren_right = "" ;
158
- if matches ! ( arg. kind, hir:: ExprKind :: Binary ( ..) ) {
159
- paren_left = "(" ;
160
- paren_right = ")" ;
161
- }
162
-
163
157
cx. tcx . emit_node_span_lint (
164
158
INTEGER_TO_PTR_TRANSMUTES ,
165
159
expr. hir_id ,
166
160
expr. span ,
167
161
IntegerToPtrTransmutes {
168
- suggestion : if dst. is_ref ( ) {
169
- IntegerToPtrTransmutesSuggestion :: ToRef {
170
- dst : * inner_ty,
171
- ref_mutbl : mutbl. prefix_str ( ) ,
172
- ptr_mutbl : mutbl. ptr_str ( ) ,
173
- paren_left,
174
- paren_right,
175
- start_call : expr. span . shrink_to_lo ( ) . until ( arg. span ) ,
176
- end_call : arg. span . shrink_to_hi ( ) . until ( expr. span . shrink_to_hi ( ) ) ,
162
+ // FIXME: once https://github.com/rust-lang/rust/issues/144538 finishes,
163
+ // we can recommend the method in const context too.
164
+ suggestion : if !cx. tcx . hir_is_inside_const_context ( expr. hir_id ) {
165
+ let suffix = if mutbl. is_mut ( ) { "_mut" } else { "" } ;
166
+ if dst. is_ref ( ) {
167
+ IntegerToPtrTransmutesSuggestion :: ToRef {
168
+ dst : * inner_ty,
169
+ suffix,
170
+ ref_mutbl : mutbl. prefix_str ( ) ,
171
+ start_call : expr. span . shrink_to_lo ( ) . until ( arg. span ) ,
172
+ }
173
+ } else {
174
+ IntegerToPtrTransmutesSuggestion :: ToPtr {
175
+ dst : * inner_ty,
176
+ suffix,
177
+ start_call : expr. span . shrink_to_lo ( ) . until ( arg. span ) ,
178
+ }
177
179
}
178
180
} else {
179
- IntegerToPtrTransmutesSuggestion :: ToPtr {
180
- dst,
181
- paren_left,
182
- paren_right,
183
- start_call : expr. span . shrink_to_lo ( ) . until ( arg. span ) ,
184
- end_call : arg. span . shrink_to_hi ( ) . until ( expr. span . shrink_to_hi ( ) ) ,
181
+ // does the argument needs parenthesis
182
+ let mut paren_left = "" ;
183
+ let mut paren_right = "" ;
184
+ if matches ! ( arg. kind, hir:: ExprKind :: Binary ( ..) ) {
185
+ paren_left = "(" ;
186
+ paren_right = ")" ;
187
+ }
188
+
189
+ if dst. is_ref ( ) {
190
+ IntegerToPtrTransmutesSuggestion :: ToRefInConst {
191
+ dst : * inner_ty,
192
+ ref_mutbl : mutbl. prefix_str ( ) ,
193
+ ptr_mutbl : mutbl. ptr_str ( ) ,
194
+ paren_left,
195
+ paren_right,
196
+ start_call : expr. span . shrink_to_lo ( ) . until ( arg. span ) ,
197
+ end_call : arg. span . shrink_to_hi ( ) . until ( expr. span . shrink_to_hi ( ) ) ,
198
+ }
199
+ } else {
200
+ IntegerToPtrTransmutesSuggestion :: ToPtrInConst {
201
+ dst,
202
+ paren_left,
203
+ paren_right,
204
+ start_call : expr. span . shrink_to_lo ( ) . until ( arg. span ) ,
205
+ end_call : arg. span . shrink_to_hi ( ) . until ( expr. span . shrink_to_hi ( ) ) ,
206
+ }
185
207
}
186
208
} ,
187
209
} ,
0 commit comments