Skip to content

Commit 31278ff

Browse files
committed
Auto merge of #111567 - Urgau:uplift_cast_ref_to_mut, r=b-naber
Uplift `clippy::cast_ref_to_mut` lint This PR aims at uplifting the `clippy::cast_ref_to_mut` lint into rustc. ## `cast_ref_to_mut` (deny-by-default) The `cast_ref_to_mut` lint checks for casts of `&T` to `&mut T` without using interior mutability. ### Example ```rust,compile_fail fn x(r: &i32) { unsafe { *(r as *const i32 as *mut i32) += 1; } } ``` ### Explanation Casting `&T` to `&mut T` without interior mutability is undefined behavior, as it's a violation of Rust reference aliasing requirements. ----- Mostly followed the instructions for uplifting a clippy lint described here: rust-lang/rust#99696 (review) `@rustbot` label: +I-lang-nominated r? compiler ----- For Clippy: changelog: Moves: Uplifted `clippy::cast_ref_to_mut` into rustc
2 parents 405badc + 601399c commit 31278ff

File tree

2 files changed

+2
-0
lines changed

2 files changed

+2
-0
lines changed

core/src/ptr/const_ptr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl<T: ?Sized> *const T {
104104
/// refactored.
105105
#[stable(feature = "ptr_const_cast", since = "1.65.0")]
106106
#[rustc_const_stable(feature = "ptr_const_cast", since = "1.65.0")]
107+
#[rustc_diagnostic_item = "ptr_cast_mut"]
107108
#[inline(always)]
108109
pub const fn cast_mut(self) -> *mut T {
109110
self as _

core/src/ptr/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ where
698698
#[inline(always)]
699699
#[must_use]
700700
#[unstable(feature = "ptr_from_ref", issue = "106116")]
701+
#[rustc_diagnostic_item = "ptr_from_ref"]
701702
pub const fn from_ref<T: ?Sized>(r: &T) -> *const T {
702703
r
703704
}

0 commit comments

Comments
 (0)