]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
rust: enable `clippy::ptr_as_ptr` lint
authorTamir Duberstein <tamird@gmail.com>
Sun, 15 Jun 2025 20:55:05 +0000 (16:55 -0400)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 22 Jun 2025 21:08:42 +0000 (23:08 +0200)
commitfcad9bbf9e1a7de6c53908954ba1b1a1ab11ef1e
tree2f1128914944f96a1cbd32afee3fd1345fbeafe4
parent86731a2a651e58953fc949573895f2fa6d456841
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
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: Viresh Kumar <viresh.kumar@linaro.org>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/r/20250615-ptr-as-ptr-v12-1-f43b024581e8@gmail.com
[ Added `.cast()` for `opp`. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
25 files changed:
Makefile
rust/bindings/lib.rs
rust/kernel/alloc/allocator_test.rs
rust/kernel/alloc/kvec.rs
rust/kernel/configfs.rs
rust/kernel/cpufreq.rs
rust/kernel/device.rs
rust/kernel/devres.rs
rust/kernel/dma.rs
rust/kernel/error.rs
rust/kernel/firmware.rs
rust/kernel/fs/file.rs
rust/kernel/kunit.rs
rust/kernel/list/impl_list_item_mod.rs
rust/kernel/opp.rs
rust/kernel/pci.rs
rust/kernel/platform.rs
rust/kernel/print.rs
rust/kernel/seq_file.rs
rust/kernel/str.rs
rust/kernel/sync/poll.rs
rust/kernel/time/hrtimer/pin.rs
rust/kernel/time/hrtimer/pin_mut.rs
rust/kernel/workqueue.rs
rust/uapi/lib.rs