diff --git a/clippy_lints/src/casts/ptr_cast_constness.rs b/clippy_lints/src/casts/ptr_cast_constness.rs index 921693567fc..7513e18d408 100644 --- a/clippy_lints/src/casts/ptr_cast_constness.rs +++ b/clippy_lints/src/casts/ptr_cast_constness.rs @@ -4,7 +4,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, Mutability}; use rustc_lint::LateContext; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Ty, TypeVisitableExt}; use super::PTR_CAST_CONSTNESS; @@ -24,6 +24,7 @@ pub(super) fn check<'tcx>( (Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not) ) && from_ty == to_ty + && !from_ty.has_erased_regions() { let sugg = Sugg::hir(cx, cast_expr, "_"); let constness = match *to_mutbl { diff --git a/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs b/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs index 36a8e9163a0..0772b284968 100644 --- a/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs +++ b/clippy_lints/src/transmute/transmute_ptr_to_ptr.rs @@ -5,7 +5,7 @@ use rustc_errors::Applicability; use rustc_hir::Expr; use rustc_lint::LateContext; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Ty, TypeVisitableExt}; /// Checks for `transmute_ptr_to_ptr` lint. /// 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"), _ => None, } + && !from_pointee_ty.has_erased_regions() && msrv.meets(msrvs::POINTER_CAST_CONSTNESS) { diag.span_suggestion_verbose( diff --git a/tests/ui/ptr_cast_constness.fixed b/tests/ui/ptr_cast_constness.fixed index 33c0725faad..21ac42196e1 100644 --- a/tests/ui/ptr_cast_constness.fixed +++ b/tests/ui/ptr_cast_constness.fixed @@ -46,6 +46,10 @@ fn main() { let _ = external!($ptr as *const u32); } +fn lifetime_to_static(v: *mut &()) -> *const &'static () { + v as _ +} + #[clippy::msrv = "1.64"] fn _msrv_1_64() { let ptr: *const u32 = &42_u32; diff --git a/tests/ui/ptr_cast_constness.rs b/tests/ui/ptr_cast_constness.rs index 24d959856db..5ce590b2b7e 100644 --- a/tests/ui/ptr_cast_constness.rs +++ b/tests/ui/ptr_cast_constness.rs @@ -46,6 +46,10 @@ fn main() { let _ = external!($ptr as *const u32); } +fn lifetime_to_static(v: *mut &()) -> *const &'static () { + v as _ +} + #[clippy::msrv = "1.64"] fn _msrv_1_64() { let ptr: *const u32 = &42_u32; diff --git a/tests/ui/ptr_cast_constness.stderr b/tests/ui/ptr_cast_constness.stderr index 322c3585e62..2c52ebd3464 100644 --- a/tests/ui/ptr_cast_constness.stderr +++ b/tests/ui/ptr_cast_constness.stderr @@ -32,13 +32,13 @@ LL | let _ = mut_ptr as *const u32; | ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()` 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; | ^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `ptr.cast_mut()` 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; | ^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `mut_ptr.cast_const()` diff --git a/tests/ui/transmute_ptr_to_ptr.fixed b/tests/ui/transmute_ptr_to_ptr.fixed index 7a84f2b53f6..39e67b25052 100644 --- a/tests/ui/transmute_ptr_to_ptr.fixed +++ b/tests/ui/transmute_ptr_to_ptr.fixed @@ -60,6 +60,11 @@ fn transmute_ptr_to_ptr() { 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) const _: &() = { struct Zst; diff --git a/tests/ui/transmute_ptr_to_ptr.rs b/tests/ui/transmute_ptr_to_ptr.rs index 1dc8e2af5bb..580b2855173 100644 --- a/tests/ui/transmute_ptr_to_ptr.rs +++ b/tests/ui/transmute_ptr_to_ptr.rs @@ -60,6 +60,11 @@ fn transmute_ptr_to_ptr() { 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) const _: &() = { struct Zst; diff --git a/tests/ui/transmute_ptr_to_ptr.stderr b/tests/ui/transmute_ptr_to_ptr.stderr index 429341289a3..8801eb943ce 100644 --- a/tests/ui/transmute_ptr_to_ptr.stderr +++ b/tests/ui/transmute_ptr_to_ptr.stderr @@ -75,7 +75,18 @@ LL | let _: *mut u32 = ptr.cast_mut(); | ~~~~~~~~~~~~~~ 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); | ^^^^^^^^^^^^^^ @@ -86,7 +97,7 @@ LL | let _: *const i8 = ptr as *const i8; | ~~~~~~~~~~~~~~~~ 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); | ^^^^^^^^^^^^^^ @@ -97,7 +108,7 @@ LL | let _: *const i8 = ptr.cast::(); | ~~~~~~~~~~~~~~~~ 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); | ^^^^^^^^^^^^^^ @@ -108,7 +119,7 @@ LL | let _: *mut u8 = ptr as *mut u8; | ~~~~~~~~~~~~~~ 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); | ^^^^^^^^^^^^^^^^^^ @@ -119,7 +130,7 @@ LL | let _: *const u8 = mut_ptr as *const u8; | ~~~~~~~~~~~~~~~~~~~~ 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); | ^^^^^^^^^^^^^^ @@ -130,7 +141,7 @@ LL | let _: *mut u8 = ptr.cast_mut(); | ~~~~~~~~~~~~~~ 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); | ^^^^^^^^^^^^^^^^^^ @@ -140,5 +151,5 @@ help: use `pointer::cast_const` instead LL | let _: *const u8 = mut_ptr.cast_const(); | ~~~~~~~~~~~~~~~~~~~~ -error: aborting due to 15 previous errors +error: aborting due to 16 previous errors