Re: [PATCH v2 2/5] rust: enable `clippy::ptr_as_ptr` lint

Benno Lossin
2025-03-12T14:40:09Z

On Sun Mar 9, 2025 at 5:00 PM CET, Tamir Duberstein wrote:
> 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 `let x: *const _ = &x;` and `.cast()` or `.cast::<T>()`

Similarly to the other patch, this could be `let x = &raw x;`. (but it's
fine to leave it as-is for now, we can also make that a
good-first-issue.)

> according to the previous rules. The intermediate `let` binding is
> required because `(x as *const _).cast::<T>()` results in inference
> failure.
> - Native literal C strings are replaced with `c_str!().as_char_ptr()`.
>
> 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]
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>

Reviewed-by: Benno Lossin <benno.lossin@proton.me>

---
Cheers,
Benno

> ---
> Makefile | 1 +
> rust/bindings/lib.rs | 1 +
> rust/kernel/alloc/allocator_test.rs | 2 +-
> rust/kernel/alloc/kvec.rs | 4 ++--
> rust/kernel/device.rs | 5 +++--
> rust/kernel/devres.rs | 2 +-
> rust/kernel/error.rs | 2 +-
> rust/kernel/fs/file.rs | 2 +-
> rust/kernel/kunit.rs | 15 +++++++--------
> rust/kernel/list/impl_list_item_mod.rs | 2 +-
> rust/kernel/pci.rs | 2 +-
> rust/kernel/platform.rs | 4 +++-
> rust/kernel/print.rs | 11 +++++------
> rust/kernel/seq_file.rs | 3 ++-
> rust/kernel/str.rs | 2 +-
> rust/kernel/sync/poll.rs | 2 +-
> rust/kernel/workqueue.rs | 10 +++++-----
> rust/uapi/lib.rs | 1 +
> 18 files changed, 38 insertions(+), 33 deletions(-)