]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: enable `clippy::ptr_cast_constness` lint
authorTamir Duberstein <tamird@gmail.com>
Sun, 15 Jun 2025 20:55:06 +0000 (16:55 -0400)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 22 Jun 2025 21:08:50 +0000 (23:08 +0200)
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 3 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()`.
- `*const T as *mut _` becommes `(*const T).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
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/r/20250615-ptr-as-ptr-v12-2-f43b024581e8@gmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Makefile
rust/kernel/block/mq/request.rs
rust/kernel/drm/device.rs

index 25c19877b903b62a984bfcf041068a5fed4258ef..63b705d996319671eee2640d1b08c80b98a7744f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -485,6 +485,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 \
index 4a5b7ec914efa598c65881b07c4ece59214fd7e7..af5c9ac94f3689dd776564ca225ecf90fc77cff8 100644 (file)
@@ -69,7 +69,7 @@ impl<T: Operations> Request<T> {
         // 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 @@ impl<T: Operations> Request<T> {
         // 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() }
     }
 }
 
index 624d7a4c83ead64b93325189f481d9b37c3c6eae..ef66deb7ce23c6b9b643436a3c2c12c177d8c288 100644 (file)
@@ -83,8 +83,8 @@ impl<T: drm::Driver> Device<T> {
         major: T::INFO.major,
         minor: T::INFO.minor,
         patchlevel: T::INFO.patchlevel,
-        name: T::INFO.name.as_char_ptr() as *mut _,
-        desc: T::INFO.desc.as_char_ptr() as *mut _,
+        name: T::INFO.name.as_char_ptr().cast_mut(),
+        desc: T::INFO.desc.as_char_ptr().cast_mut(),
 
         driver_features: drm::driver::FEAT_GEM,
         ioctls: T::IOCTLS.as_ptr(),