Skip to content

Commit fcad9bb

Browse files
tamirdojeda
authored andcommitted
rust: enable clippy::ptr_as_ptr lint
In Rust 1.51.0, Clippy introduced the `ptr_as_ptr` lint [1]: > Though `as` casts between raw pointers are not terrible, > `pointer::cast` is safer because it cannot accidentally change the > pointer's mutability, nor cast the pointer to other types like `usize`. There are a few classes of changes required: - Modules generated by bindgen are marked `#[allow(clippy::ptr_as_ptr)]`. - Inferred casts (` as _`) are replaced with `.cast()`. - Ascribed casts (` as *... T`) are replaced with `.cast::<T>()`. - Multistep casts from references (` as *const _ as *const T`) are replaced with `core::ptr::from_ref(&x).cast()` with or without `::<T>` according to the previous rules. The `core::ptr::from_ref` call is required because `(x as *const _).cast::<T>()` results in inference failure. - Native literal C strings are replaced with `c_str!().as_char_ptr()`. - `*mut *mut T as _` is replaced with `let *mut *const T = (*mut *mut T)`.cast();` since pointer to pointer can be confusing. Apply these changes and enable the lint -- no functional change intended. Link: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_as_ptr [1] Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Acked-by: Viresh Kumar <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Acked-by: Tejun Heo <[email protected]> Acked-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Added `.cast()` for `opp`. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 86731a2 commit fcad9bb

25 files changed

+41
-32
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ export rust_common_flags := --edition=2021 \
484484
-Wclippy::needless_bitwise_bool \
485485
-Aclippy::needless_lifetimes \
486486
-Wclippy::no_mangle_with_rust_abi \
487+
-Wclippy::ptr_as_ptr \
487488
-Wclippy::undocumented_unsafe_blocks \
488489
-Wclippy::unnecessary_safety_comment \
489490
-Wclippy::unnecessary_safety_doc \

rust/bindings/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)]
2626

2727
#[allow(dead_code)]
28+
#[allow(clippy::ptr_as_ptr)]
2829
#[allow(clippy::undocumented_unsafe_blocks)]
2930
#[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))]
3031
mod bindings_raw {

rust/kernel/alloc/allocator_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ unsafe impl Allocator for Cmalloc {
8282

8383
// SAFETY: Returns either NULL or a pointer to a memory allocation that satisfies or
8484
// exceeds the given size and alignment requirements.
85-
let dst = unsafe { libc_aligned_alloc(layout.align(), layout.size()) } as *mut u8;
85+
let dst = unsafe { libc_aligned_alloc(layout.align(), layout.size()) }.cast::<u8>();
8686
let dst = NonNull::new(dst).ok_or(AllocError)?;
8787

8888
if flags.contains(__GFP_ZERO) {

rust/kernel/alloc/kvec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ where
288288
// - `self.len` is smaller than `self.capacity` by the type invariant and hence, the
289289
// resulting pointer is guaranteed to be part of the same allocated object.
290290
// - `self.len` can not overflow `isize`.
291-
let ptr = unsafe { self.as_mut_ptr().add(self.len) } as *mut MaybeUninit<T>;
291+
let ptr = unsafe { self.as_mut_ptr().add(self.len) }.cast::<MaybeUninit<T>>();
292292

293293
// SAFETY: The memory between `self.len` and `self.capacity` is guaranteed to be allocated
294294
// and valid, but uninitialized.
@@ -847,7 +847,7 @@ where
847847
// - `ptr` points to memory with at least a size of `size_of::<T>() * len`,
848848
// - all elements within `b` are initialized values of `T`,
849849
// - `len` does not exceed `isize::MAX`.
850-
unsafe { Vec::from_raw_parts(ptr as _, len, len) }
850+
unsafe { Vec::from_raw_parts(ptr.cast(), len, len) }
851851
}
852852
}
853853

rust/kernel/configfs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ where
561561
let data: &Data = unsafe { get_group_data(c_group) };
562562

563563
// SAFETY: By function safety requirements, `page` is writable for `PAGE_SIZE`.
564-
let ret = O::show(data, unsafe { &mut *(page as *mut [u8; PAGE_SIZE]) });
564+
let ret = O::show(data, unsafe { &mut *(page.cast::<[u8; PAGE_SIZE]>()) });
565565

566566
match ret {
567567
Ok(size) => size as isize,

rust/kernel/cpufreq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ impl Policy {
649649
fn set_data<T: ForeignOwnable>(&mut self, data: T) -> Result {
650650
if self.as_ref().driver_data.is_null() {
651651
// Transfer the ownership of the data to the foreign interface.
652-
self.as_mut_ref().driver_data = <T as ForeignOwnable>::into_foreign(data) as _;
652+
self.as_mut_ref().driver_data = <T as ForeignOwnable>::into_foreign(data).cast();
653653
Ok(())
654654
} else {
655655
Err(EBUSY)

rust/kernel/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ impl<Ctx: DeviceContext> Device<Ctx> {
195195
#[cfg(CONFIG_PRINTK)]
196196
unsafe {
197197
bindings::_dev_printk(
198-
klevel as *const _ as *const crate::ffi::c_char,
198+
klevel.as_ptr().cast::<crate::ffi::c_char>(),
199199
self.as_raw(),
200200
c_str!("%pA").as_char_ptr(),
201-
&msg as *const _ as *const crate::ffi::c_void,
201+
core::ptr::from_ref(&msg).cast::<crate::ffi::c_void>(),
202202
)
203203
};
204204
}

rust/kernel/devres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<T> DevresInner<T> {
159159

160160
#[allow(clippy::missing_safety_doc)]
161161
unsafe extern "C" fn devres_callback(ptr: *mut kernel::ffi::c_void) {
162-
let ptr = ptr as *mut DevresInner<T>;
162+
let ptr = ptr.cast::<DevresInner<T>>();
163163
// Devres owned this memory; now that we received the callback, drop the `Arc` and hence the
164164
// reference.
165165
// SAFETY: Safe, since we leaked an `Arc` reference to devm_add_action() in

rust/kernel/dma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<T: AsBytes + FromBytes> CoherentAllocation<T> {
186186
dev: dev.into(),
187187
dma_handle,
188188
count,
189-
cpu_addr: ret as *mut T,
189+
cpu_addr: ret.cast::<T>(),
190190
dma_attrs,
191191
})
192192
}
@@ -293,7 +293,7 @@ impl<T: AsBytes + FromBytes> Drop for CoherentAllocation<T> {
293293
bindings::dma_free_attrs(
294294
self.dev.as_raw(),
295295
size,
296-
self.cpu_addr as _,
296+
self.cpu_addr.cast(),
297297
self.dma_handle,
298298
self.dma_attrs.as_raw(),
299299
)

rust/kernel/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl Error {
153153
/// Returns the error encoded as a pointer.
154154
pub fn to_ptr<T>(self) -> *mut T {
155155
// SAFETY: `self.0` is a valid error due to its invariant.
156-
unsafe { bindings::ERR_PTR(self.0.get() as _) as *mut _ }
156+
unsafe { bindings::ERR_PTR(self.0.get() as _).cast() }
157157
}
158158

159159
/// Returns a string representing the error, if one exists.

0 commit comments

Comments
 (0)