Skip to content

Commit d8c9e73

Browse files
tamirdojeda
authored andcommitted
rust: enable clippy::ptr_cast_constness lint
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 [1] Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Acked-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent fcad9bb commit d8c9e73

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ export rust_common_flags := --edition=2021 \
485485
-Aclippy::needless_lifetimes \
486486
-Wclippy::no_mangle_with_rust_abi \
487487
-Wclippy::ptr_as_ptr \
488+
-Wclippy::ptr_cast_constness \
488489
-Wclippy::undocumented_unsafe_blocks \
489490
-Wclippy::unnecessary_safety_comment \
490491
-Wclippy::unnecessary_safety_doc \

rust/kernel/block/mq/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<T: Operations> Request<T> {
6969
// INVARIANT: By the safety requirements of this function, invariants are upheld.
7070
// SAFETY: By the safety requirement of this function, we own a
7171
// reference count that we can pass to `ARef`.
72-
unsafe { ARef::from_raw(NonNull::new_unchecked(ptr as *const Self as *mut Self)) }
72+
unsafe { ARef::from_raw(NonNull::new_unchecked(ptr.cast())) }
7373
}
7474

7575
/// Notify the block layer that a request is going to be processed now.
@@ -155,7 +155,7 @@ impl<T: Operations> Request<T> {
155155
// the private data associated with this request is initialized and
156156
// valid. The existence of `&self` guarantees that the private data is
157157
// valid as a shared reference.
158-
unsafe { Self::wrapper_ptr(self as *const Self as *mut Self).as_ref() }
158+
unsafe { Self::wrapper_ptr(core::ptr::from_ref(self).cast_mut()).as_ref() }
159159
}
160160
}
161161

rust/kernel/drm/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl<T: drm::Driver> Device<T> {
8383
major: T::INFO.major,
8484
minor: T::INFO.minor,
8585
patchlevel: T::INFO.patchlevel,
86-
name: T::INFO.name.as_char_ptr() as *mut _,
87-
desc: T::INFO.desc.as_char_ptr() as *mut _,
86+
name: T::INFO.name.as_char_ptr().cast_mut(),
87+
desc: T::INFO.desc.as_char_ptr().cast_mut(),
8888

8989
driver_features: drm::driver::FEAT_GEM,
9090
ioctls: T::IOCTLS.as_ptr(),

0 commit comments

Comments
 (0)