Don't suggest .cast_mut/.cast_const for types with erased lifetimes

This commit is contained in:
Alex Macleod 2024-07-23 11:11:22 +00:00
parent afe811ad30
commit 7010d3c67e
8 changed files with 42 additions and 11 deletions

View File

@ -4,7 +4,7 @@
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{Expr, Mutability}; use rustc_hir::{Expr, Mutability};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty, TypeVisitableExt};
use super::PTR_CAST_CONSTNESS; use super::PTR_CAST_CONSTNESS;
@ -24,6 +24,7 @@ pub(super) fn check<'tcx>(
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not) (Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)
) )
&& from_ty == to_ty && from_ty == to_ty
&& !from_ty.has_erased_regions()
{ {
let sugg = Sugg::hir(cx, cast_expr, "_"); let sugg = Sugg::hir(cx, cast_expr, "_");
let constness = match *to_mutbl { let constness = match *to_mutbl {

View File

@ -5,7 +5,7 @@
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::Expr; use rustc_hir::Expr;
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty, TypeVisitableExt};
/// Checks for `transmute_ptr_to_ptr` lint. /// Checks for `transmute_ptr_to_ptr` lint.
/// Returns `true` if it's triggered, otherwise returns `false`. /// Returns `true` if it's triggered, otherwise returns `false`.
@ -42,6 +42,7 @@ pub(super) fn check<'tcx>(
(ty::Mutability::Mut, ty::Mutability::Not) => Some("cast_const"), (ty::Mutability::Mut, ty::Mutability::Not) => Some("cast_const"),
_ => None, _ => None,
} }
&& !from_pointee_ty.has_erased_regions()
&& msrv.meets(msrvs::POINTER_CAST_CONSTNESS) && msrv.meets(msrvs::POINTER_CAST_CONSTNESS)
{ {
diag.span_suggestion_verbose( diag.span_suggestion_verbose(

View File

@ -46,6 +46,10 @@ fn main() {
let _ = external!($ptr as *const u32); let _ = external!($ptr as *const u32);
} }
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
v as _
}
#[clippy::msrv = "1.64"] #[clippy::msrv = "1.64"]
fn _msrv_1_64() { fn _msrv_1_64() {
let ptr: *const u32 = &42_u32; let ptr: *const u32 = &42_u32;

View File

@ -46,6 +46,10 @@ fn main() {
let _ = external!($ptr as *const u32); let _ = external!($ptr as *const u32);
} }
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
v as _
}
#[clippy::msrv = "1.64"] #[clippy::msrv = "1.64"]
fn _msrv_1_64() { fn _msrv_1_64() {
let ptr: *const u32 = &42_u32; let ptr: *const u32 = &42_u32;

View File

@ -32,13 +32,13 @@ LL | let _ = mut_ptr as *const u32;
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()` | ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`
error: `as` casting between raw pointers while changing only its constness error: `as` casting between raw pointers while changing only its constness
--> tests/ui/ptr_cast_constness.rs:64:13 --> tests/ui/ptr_cast_constness.rs:68:13
| |
LL | let _ = ptr as *mut u32; LL | let _ = ptr as *mut u32;
| ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()` | ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()`
error: `as` casting between raw pointers while changing only its constness error: `as` casting between raw pointers while changing only its constness
--> tests/ui/ptr_cast_constness.rs:65:13 --> tests/ui/ptr_cast_constness.rs:69:13
| |
LL | let _ = mut_ptr as *const u32; LL | let _ = mut_ptr as *const u32;
| ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()` | ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()`

View File

@ -60,6 +60,11 @@ fn transmute_ptr_to_ptr() {
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) }; let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
} }
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
unsafe { v as *const &() }
//~^ transmute_ptr_to_ptr
}
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959) // dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
const _: &() = { const _: &() = {
struct Zst; struct Zst;

View File

@ -60,6 +60,11 @@ fn transmute_ptr_to_ptr() {
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) }; let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
} }
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
unsafe { transmute(v) }
//~^ transmute_ptr_to_ptr
}
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959) // dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
const _: &() = { const _: &() = {
struct Zst; struct Zst;

View File

@ -75,7 +75,18 @@ LL | let _: *mut u32 = ptr.cast_mut();
| ~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> tests/ui/transmute_ptr_to_ptr.rs:74:28 --> tests/ui/transmute_ptr_to_ptr.rs:64:14
|
LL | unsafe { transmute(v) }
| ^^^^^^^^^^^^
|
help: use an `as` cast instead
|
LL | unsafe { v as *const &() }
| ~~~~~~~~~~~~~~~
error: transmute from a pointer to a pointer
--> tests/ui/transmute_ptr_to_ptr.rs:79:28
| |
LL | let _: *const i8 = transmute(ptr); LL | let _: *const i8 = transmute(ptr);
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@ -86,7 +97,7 @@ LL | let _: *const i8 = ptr as *const i8;
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> tests/ui/transmute_ptr_to_ptr.rs:81:28 --> tests/ui/transmute_ptr_to_ptr.rs:86:28
| |
LL | let _: *const i8 = transmute(ptr); LL | let _: *const i8 = transmute(ptr);
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@ -97,7 +108,7 @@ LL | let _: *const i8 = ptr.cast::<i8>();
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> tests/ui/transmute_ptr_to_ptr.rs:88:26 --> tests/ui/transmute_ptr_to_ptr.rs:93:26
| |
LL | let _: *mut u8 = transmute(ptr); LL | let _: *mut u8 = transmute(ptr);
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@ -108,7 +119,7 @@ LL | let _: *mut u8 = ptr as *mut u8;
| ~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> tests/ui/transmute_ptr_to_ptr.rs:89:28 --> tests/ui/transmute_ptr_to_ptr.rs:94:28
| |
LL | let _: *const u8 = transmute(mut_ptr); LL | let _: *const u8 = transmute(mut_ptr);
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
@ -119,7 +130,7 @@ LL | let _: *const u8 = mut_ptr as *const u8;
| ~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> tests/ui/transmute_ptr_to_ptr.rs:96:26 --> tests/ui/transmute_ptr_to_ptr.rs:101:26
| |
LL | let _: *mut u8 = transmute(ptr); LL | let _: *mut u8 = transmute(ptr);
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@ -130,7 +141,7 @@ LL | let _: *mut u8 = ptr.cast_mut();
| ~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~
error: transmute from a pointer to a pointer error: transmute from a pointer to a pointer
--> tests/ui/transmute_ptr_to_ptr.rs:97:28 --> tests/ui/transmute_ptr_to_ptr.rs:102:28
| |
LL | let _: *const u8 = transmute(mut_ptr); LL | let _: *const u8 = transmute(mut_ptr);
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
@ -140,5 +151,5 @@ help: use `pointer::cast_const` instead
LL | let _: *const u8 = mut_ptr.cast_const(); LL | let _: *const u8 = mut_ptr.cast_const();
| ~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~
error: aborting due to 15 previous errors error: aborting due to 16 previous errors