Skip to content

Commit 7010d3c

Browse files
committed
Don't suggest .cast_mut/.cast_const for types with erased lifetimes
1 parent afe811a commit 7010d3c

8 files changed

+42
-11
lines changed

clippy_lints/src/casts/ptr_cast_constness.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::sugg::Sugg;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, Mutability};
66
use rustc_lint::LateContext;
7-
use rustc_middle::ty::{self, Ty};
7+
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
88

99
use super::PTR_CAST_CONSTNESS;
1010

@@ -24,6 +24,7 @@ pub(super) fn check<'tcx>(
2424
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)
2525
)
2626
&& from_ty == to_ty
27+
&& !from_ty.has_erased_regions()
2728
{
2829
let sugg = Sugg::hir(cx, cast_expr, "_");
2930
let constness = match *to_mutbl {

clippy_lints/src/transmute/transmute_ptr_to_ptr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::sugg;
55
use rustc_errors::Applicability;
66
use rustc_hir::Expr;
77
use rustc_lint::LateContext;
8-
use rustc_middle::ty::{self, Ty};
8+
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
99

1010
/// Checks for `transmute_ptr_to_ptr` lint.
1111
/// Returns `true` if it's triggered, otherwise returns `false`.
@@ -42,6 +42,7 @@ pub(super) fn check<'tcx>(
4242
(ty::Mutability::Mut, ty::Mutability::Not) => Some("cast_const"),
4343
_ => None,
4444
}
45+
&& !from_pointee_ty.has_erased_regions()
4546
&& msrv.meets(msrvs::POINTER_CAST_CONSTNESS)
4647
{
4748
diag.span_suggestion_verbose(

tests/ui/ptr_cast_constness.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ fn main() {
4646
let _ = external!($ptr as *const u32);
4747
}
4848

49+
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
50+
v as _
51+
}
52+
4953
#[clippy::msrv = "1.64"]
5054
fn _msrv_1_64() {
5155
let ptr: *const u32 = &42_u32;

tests/ui/ptr_cast_constness.rs

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ fn main() {
4646
let _ = external!($ptr as *const u32);
4747
}
4848

49+
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
50+
v as _
51+
}
52+
4953
#[clippy::msrv = "1.64"]
5054
fn _msrv_1_64() {
5155
let ptr: *const u32 = &42_u32;

tests/ui/ptr_cast_constness.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ LL | let _ = mut_ptr as *const u32;
3232
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`
3333

3434
error: `as` casting between raw pointers while changing only its constness
35-
--> tests/ui/ptr_cast_constness.rs:64:13
35+
--> tests/ui/ptr_cast_constness.rs:68:13
3636
|
3737
LL | let _ = ptr as *mut u32;
3838
| ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()`
3939

4040
error: `as` casting between raw pointers while changing only its constness
41-
--> tests/ui/ptr_cast_constness.rs:65:13
41+
--> tests/ui/ptr_cast_constness.rs:69:13
4242
|
4343
LL | let _ = mut_ptr as *const u32;
4444
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`

tests/ui/transmute_ptr_to_ptr.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ fn transmute_ptr_to_ptr() {
6060
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
6161
}
6262

63+
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
64+
unsafe { v as *const &() }
65+
//~^ transmute_ptr_to_ptr
66+
}
67+
6368
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
6469
const _: &() = {
6570
struct Zst;

tests/ui/transmute_ptr_to_ptr.rs

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ fn transmute_ptr_to_ptr() {
6060
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
6161
}
6262

63+
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
64+
unsafe { transmute(v) }
65+
//~^ transmute_ptr_to_ptr
66+
}
67+
6368
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
6469
const _: &() = {
6570
struct Zst;

tests/ui/transmute_ptr_to_ptr.stderr

+18-7
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ LL | let _: *mut u32 = ptr.cast_mut();
7575
| ~~~~~~~~~~~~~~
7676

7777
error: transmute from a pointer to a pointer
78-
--> tests/ui/transmute_ptr_to_ptr.rs:74:28
78+
--> tests/ui/transmute_ptr_to_ptr.rs:64:14
79+
|
80+
LL | unsafe { transmute(v) }
81+
| ^^^^^^^^^^^^
82+
|
83+
help: use an `as` cast instead
84+
|
85+
LL | unsafe { v as *const &() }
86+
| ~~~~~~~~~~~~~~~
87+
88+
error: transmute from a pointer to a pointer
89+
--> tests/ui/transmute_ptr_to_ptr.rs:79:28
7990
|
8091
LL | let _: *const i8 = transmute(ptr);
8192
| ^^^^^^^^^^^^^^
@@ -86,7 +97,7 @@ LL | let _: *const i8 = ptr as *const i8;
8697
| ~~~~~~~~~~~~~~~~
8798

8899
error: transmute from a pointer to a pointer
89-
--> tests/ui/transmute_ptr_to_ptr.rs:81:28
100+
--> tests/ui/transmute_ptr_to_ptr.rs:86:28
90101
|
91102
LL | let _: *const i8 = transmute(ptr);
92103
| ^^^^^^^^^^^^^^
@@ -97,7 +108,7 @@ LL | let _: *const i8 = ptr.cast::<i8>();
97108
| ~~~~~~~~~~~~~~~~
98109

99110
error: transmute from a pointer to a pointer
100-
--> tests/ui/transmute_ptr_to_ptr.rs:88:26
111+
--> tests/ui/transmute_ptr_to_ptr.rs:93:26
101112
|
102113
LL | let _: *mut u8 = transmute(ptr);
103114
| ^^^^^^^^^^^^^^
@@ -108,7 +119,7 @@ LL | let _: *mut u8 = ptr as *mut u8;
108119
| ~~~~~~~~~~~~~~
109120

110121
error: transmute from a pointer to a pointer
111-
--> tests/ui/transmute_ptr_to_ptr.rs:89:28
122+
--> tests/ui/transmute_ptr_to_ptr.rs:94:28
112123
|
113124
LL | let _: *const u8 = transmute(mut_ptr);
114125
| ^^^^^^^^^^^^^^^^^^
@@ -119,7 +130,7 @@ LL | let _: *const u8 = mut_ptr as *const u8;
119130
| ~~~~~~~~~~~~~~~~~~~~
120131

121132
error: transmute from a pointer to a pointer
122-
--> tests/ui/transmute_ptr_to_ptr.rs:96:26
133+
--> tests/ui/transmute_ptr_to_ptr.rs:101:26
123134
|
124135
LL | let _: *mut u8 = transmute(ptr);
125136
| ^^^^^^^^^^^^^^
@@ -130,7 +141,7 @@ LL | let _: *mut u8 = ptr.cast_mut();
130141
| ~~~~~~~~~~~~~~
131142

132143
error: transmute from a pointer to a pointer
133-
--> tests/ui/transmute_ptr_to_ptr.rs:97:28
144+
--> tests/ui/transmute_ptr_to_ptr.rs:102:28
134145
|
135146
LL | let _: *const u8 = transmute(mut_ptr);
136147
| ^^^^^^^^^^^^^^^^^^
@@ -140,5 +151,5 @@ help: use `pointer::cast_const` instead
140151
LL | let _: *const u8 = mut_ptr.cast_const();
141152
| ~~~~~~~~~~~~~~~~~~~~
142153

143-
error: aborting due to 15 previous errors
154+
error: aborting due to 16 previous errors
144155

0 commit comments

Comments
 (0)