Skip to content

Commit a2f8fde

Browse files
committed
Recommend with_exposed_provenance instead of as casts in non-const
1 parent 996a9ab commit a2f8fde

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,10 +1617,23 @@ pub(crate) struct IntegerToPtrTransmutes<'tcx> {
16171617
}
16181618

16191619
#[derive(Subdiagnostic)]
1620-
// FIXME: recommend `with_exposed_provenance` when it's const-stable
1620+
// FIXME: always recommend `with_exposed_provenance` when it's const-stable
16211621
pub(crate) enum IntegerToPtrTransmutesSuggestion<'tcx> {
16221622
#[multipart_suggestion(lint_suggestion_as, applicability = "machine-applicable")]
16231623
ToPtr {
1624+
dst: Ty<'tcx>,
1625+
#[suggestion_part(code = "std::ptr::with_exposed_provenance::<{dst}>(")]
1626+
start_call: Span,
1627+
},
1628+
#[multipart_suggestion(lint_suggestion_as, applicability = "machine-applicable")]
1629+
ToRef {
1630+
dst: Ty<'tcx>,
1631+
ref_mutbl: &'static str,
1632+
#[suggestion_part(code = "&{ref_mutbl}*std::ptr::with_exposed_provenance::<{dst}>(")]
1633+
start_call: Span,
1634+
},
1635+
#[multipart_suggestion(lint_suggestion_as, applicability = "machine-applicable")]
1636+
ToPtrInConst {
16241637
dst: Ty<'tcx>,
16251638
paren_left: &'static str,
16261639
paren_right: &'static str,
@@ -1630,7 +1643,7 @@ pub(crate) enum IntegerToPtrTransmutesSuggestion<'tcx> {
16301643
end_call: Span,
16311644
},
16321645
#[multipart_suggestion(lint_suggestion_as, applicability = "machine-applicable")]
1633-
ToRef {
1646+
ToRefInConst {
16341647
dst: Ty<'tcx>,
16351648
ptr_mutbl: &'static str,
16361649
ref_mutbl: &'static str,

compiler/rustc_lint/src/transmute.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,38 @@ fn check_int_to_ptr_transmute<'tcx>(
165165
expr.hir_id,
166166
expr.span,
167167
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()),
168+
suggestion: if !cx.tcx.hir_is_inside_const_context(expr.hir_id) {
169+
if dst.is_ref() {
170+
IntegerToPtrTransmutesSuggestion::ToRef {
171+
dst: *inner_ty,
172+
ref_mutbl: mutbl.prefix_str(),
173+
start_call: expr.span.shrink_to_lo().until(arg.span),
174+
}
175+
} else {
176+
IntegerToPtrTransmutesSuggestion::ToPtr {
177+
dst: *inner_ty,
178+
start_call: expr.span.shrink_to_lo().until(arg.span),
179+
}
177180
}
178181
} 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()),
182+
if dst.is_ref() {
183+
IntegerToPtrTransmutesSuggestion::ToRefInConst {
184+
dst: *inner_ty,
185+
ref_mutbl: mutbl.prefix_str(),
186+
ptr_mutbl: mutbl.ptr_str(),
187+
paren_left,
188+
paren_right,
189+
start_call: expr.span.shrink_to_lo().until(arg.span),
190+
end_call: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
191+
}
192+
} else {
193+
IntegerToPtrTransmutesSuggestion::ToPtrInConst {
194+
dst,
195+
paren_left,
196+
paren_right,
197+
start_call: expr.span.shrink_to_lo().until(arg.span),
198+
end_call: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
199+
}
185200
}
186201
},
187202
},

tests/ui/lint/int_to_ptr.fixed

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#![allow(dead_code)]
88

99
unsafe fn should_lint(a: usize) {
10-
let _ptr = unsafe { a as *const u8 };
10+
let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(a) };
1111
//~^ WARN transmuting an integer to a pointer
12-
let _ptr = unsafe { &*(a as *const u8) };
12+
let _ptr = unsafe { &*std::ptr::with_exposed_provenance::<u8>(a) };
1313
//~^ WARN transmuting an integer to a pointer
14-
let _ptr = unsafe { 42usize as *const u8 };
14+
let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(42usize) };
1515
//~^ WARN transmuting an integer to a pointer
16-
let _ptr = unsafe { (a + a) as *const u8 };
16+
let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(a + a) };
1717
//~^ WARN transmuting an integer to a pointer
1818
}
1919

tests/ui/lint/int_to_ptr.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a) };
1313
help: use `as` cast instead to use a previously exposed provenance
1414
|
1515
LL - let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a) };
16-
LL + let _ptr = unsafe { a as *const u8 };
16+
LL + let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(a) };
1717
|
1818

1919
warning: transmuting an integer to a pointer creates a pointer without provenance
@@ -30,7 +30,7 @@ LL | let _ptr = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
3030
help: use `as` cast instead to use a previously exposed provenance
3131
|
3232
LL - let _ptr = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
33-
LL + let _ptr = unsafe { &*(a as *const u8) };
33+
LL + let _ptr = unsafe { &*std::ptr::with_exposed_provenance::<u8>(a) };
3434
|
3535

3636
warning: transmuting an integer to a pointer creates a pointer without provenance
@@ -47,7 +47,7 @@ LL | let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
4747
help: use `as` cast instead to use a previously exposed provenance
4848
|
4949
LL - let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
50-
LL + let _ptr = unsafe { 42usize as *const u8 };
50+
LL + let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(42usize) };
5151
|
5252

5353
warning: transmuting an integer to a pointer creates a pointer without provenance
@@ -64,7 +64,7 @@ LL | let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
6464
help: use `as` cast instead to use a previously exposed provenance
6565
|
6666
LL - let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
67-
LL + let _ptr = unsafe { (a + a) as *const u8 };
67+
LL + let _ptr = unsafe { std::ptr::with_exposed_provenance::<u8>(a + a) };
6868
|
6969

7070
warning: transmuting an integer to a pointer creates a pointer without provenance

0 commit comments

Comments
 (0)