[PATCH v10 2/6] rust: enable `clippy::ptr_cast_constness` lint
Boqun Feng
boqun.feng at gmail.com
Fri Apr 18 16:49:57 UTC 2025
On Fri, Apr 18, 2025 at 11:37:18AM -0400, Tamir Duberstein wrote:
> In Rust 1.72.0, Clippy introduced the `ptr_cast_constness` lint [1]:
>
> > Though `as` casts between raw pointers are not terrible,
> > `pointer::cast_mut` and `pointer::cast_const` are safer because they
> > cannot accidentally cast the pointer to another type.
>
> There are only 2 affected sites:
> - `*mut T as *const U as *mut U` becomes `(*mut T).cast()`
> - `&self as *const Self as *mut Self` becomes
> `core::ptr::from_ref(self).cast_mut()`.
>
> Apply these changes and enable the lint -- no functional change
> intended.
>
> Link: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_cast_constness [1]
> Reviewed-by: Benno Lossin <benno.lossin at proton.me>
> Signed-off-by: Tamir Duberstein <tamird at gmail.com>
Reviewed-by: Boqun Feng <boqun.feng at gmail.com>
Regards,
Boqun
> ---
> Makefile | 1 +
> rust/kernel/block/mq/request.rs | 4 ++--
> 2 files changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 5d2931344490..7b85b2a8d371 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -481,6 +481,7 @@ export rust_common_flags := --edition=2021 \
> -Aclippy::needless_lifetimes \
> -Wclippy::no_mangle_with_rust_abi \
> -Wclippy::ptr_as_ptr \
> + -Wclippy::ptr_cast_constness \
> -Wclippy::undocumented_unsafe_blocks \
> -Wclippy::unnecessary_safety_comment \
> -Wclippy::unnecessary_safety_doc \
> diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request.rs
> index 4a5b7ec914ef..af5c9ac94f36 100644
> --- a/rust/kernel/block/mq/request.rs
> +++ b/rust/kernel/block/mq/request.rs
> @@ -69,7 +69,7 @@ pub(crate) unsafe fn aref_from_raw(ptr: *mut bindings::request) -> ARef<Self> {
> // INVARIANT: By the safety requirements of this function, invariants are upheld.
> // SAFETY: By the safety requirement of this function, we own a
> // reference count that we can pass to `ARef`.
> - unsafe { ARef::from_raw(NonNull::new_unchecked(ptr as *const Self as *mut Self)) }
> + unsafe { ARef::from_raw(NonNull::new_unchecked(ptr.cast())) }
> }
>
> /// Notify the block layer that a request is going to be processed now.
> @@ -155,7 +155,7 @@ pub(crate) fn wrapper_ref(&self) -> &RequestDataWrapper {
> // the private data associated with this request is initialized and
> // valid. The existence of `&self` guarantees that the private data is
> // valid as a shared reference.
> - unsafe { Self::wrapper_ptr(self as *const Self as *mut Self).as_ref() }
> + unsafe { Self::wrapper_ptr(core::ptr::from_ref(self).cast_mut()).as_ref() }
> }
> }
>
>
> --
> 2.49.0
>
More information about the dri-devel
mailing list