Re: [PATCH 4/6] rust: binder: enable `clippy::ref_as_ptr` lint
From: Alice Ryhl
Date: Tue May 26 2026 - 08:48:20 EST
On Fri, May 22, 2026 at 07:12:49PM +0200, Tamir Duberstein wrote:
> In Rust 1.78.0, Clippy introduced the `ref_as_ptr` lint [1]:
>
> > Using `as` casts may result in silently changing mutability or type.
>
> While this does not eliminate unchecked `as` conversions, it makes such
> conversions easier to scrutinize. It also has the slight benefit of
> removing a degree of freedom on which to bikeshed. Thus apply the changes
> and enable the lint in the Binder Rust driver -- no functional change
> intended.
>
> Link: https://rust-lang.github.io/rust-clippy/master/index.html#ref_as_ptr [1]
> Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxxx>
> ---
> drivers/android/binder/node.rs | 2 +-
> drivers/android/binder/page_range.rs | 4 ++--
> drivers/android/binder/rust_binder_main.rs | 2 +-
> drivers/android/binder/trace.rs | 2 +-
> 4 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/android/binder/node.rs b/drivers/android/binder/node.rs
> index d710940c3c8f..a4a3ddda6ac9 100644
> --- a/drivers/android/binder/node.rs
> +++ b/drivers/android/binder/node.rs
> @@ -321,7 +321,7 @@ pub(crate) unsafe fn remove_node_info(
> /// An id that is unique across all binder nodes on the system. Used as the key in the
> /// `by_node` map.
> pub(crate) fn global_id(&self) -> usize {
> - (self as *const Node).addr()
> + core::ptr::from_ref(self).addr()
Can we please import core::ptr?
> }
>
> pub(crate) fn get_id(&self) -> (u64, u64) {
> diff --git a/drivers/android/binder/page_range.rs b/drivers/android/binder/page_range.rs
> index 927b0802e80d..93e79c08b89a 100644
> --- a/drivers/android/binder/page_range.rs
> +++ b/drivers/android/binder/page_range.rs
> @@ -312,7 +312,7 @@ pub(crate) fn register_with_vma(&self, vma: &virt::VmaNew) -> Result<usize> {
>
> // SAFETY: This just initializes the pages array.
> unsafe {
> - let self_ptr = self as *const ShrinkablePageRange;
> + let self_ptr = core::ptr::from_ref(self);
> for i in 0..num_pages {
> let info = pages.as_mut_ptr().add(i);
> (&raw mut (*info).range).write(self_ptr);
> @@ -593,7 +593,7 @@ pub(crate) unsafe fn write<T: ?Sized>(&self, offset: usize, obj: &T) -> Result {
> unsafe {
> self.iterate(offset, size_of_val(obj), |page, offset, to_copy| {
> // SAFETY: The sum of `offset` and `to_copy` is bounded by the size of T.
> - let obj_ptr = (obj as *const T).cast::<u8>().add(obj_offset);
> + let obj_ptr = core::ptr::from_ref(obj).cast::<u8>().add(obj_offset);
> // SAFETY: We have a reference to the object, so the pointer is valid.
> page.write_raw(obj_ptr, offset, to_copy)?;
> obj_offset += to_copy;
> diff --git a/drivers/android/binder/rust_binder_main.rs b/drivers/android/binder/rust_binder_main.rs
> index fa28697982d3..88da29413e16 100644
> --- a/drivers/android/binder/rust_binder_main.rs
> +++ b/drivers/android/binder/rust_binder_main.rs
> @@ -6,7 +6,7 @@
>
> #![crate_name = "rust_binder"]
> #![recursion_limit = "256"]
> -#![allow(clippy::as_underscore, clippy::ref_as_ptr, clippy::cast_lossless)]
> +#![allow(clippy::as_underscore, clippy::cast_lossless)]
>
> use kernel::{
> bindings::{self, seq_file},
> diff --git a/drivers/android/binder/trace.rs b/drivers/android/binder/trace.rs
> index 5539672d7285..b6cae57801fc 100644
> --- a/drivers/android/binder/trace.rs
> +++ b/drivers/android/binder/trace.rs
> @@ -26,7 +26,7 @@
>
> #[inline]
> fn raw_transaction(t: &Transaction) -> rust_binder_transaction {
> - t as *const Transaction as rust_binder_transaction
> + core::ptr::from_ref(t).cast_mut().cast()
This just removes the type annotations I had. I'm not sure it's
cleaner.
Alice
> }
>
> #[inline]
>
> --
> 2.54.0
>