8000 rust: enable `clippy::ref_as_ptr` lint · Rust-for-Linux/linux@dc35ddc · GitHub
[go: up one dir, main page]

Skip to content

Commit dc35ddc

Browse files
tamirdojeda
authored andcommitted
rust: enable clippy::ref_as_ptr lint
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 doesn't 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 -- no functional change intended. Link: https://rust-lang.github.io/rust-clippy/master/index.html#ref_as_ptr [1] Suggested-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/all/D8PGG7NTWB6U.3SS3A5LN4XWMN@proton.me/ 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: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250615-ptr-as-ptr-v12-6-f43b024581e8@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent b7c8d7a commit dc35ddc

File tree

8 files changed

+15
-20
lines changed

8 files changed

+15
-20
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ export rust_common_flags := --edition=2021 \
489489
-Wclippy::no_mangle_with_rust_abi \
490490
-Wclippy::ptr_as_ptr \
491491
-Wclippy::ptr_cast_constness \
492+
-Wclippy::ref_as_ptr \
492493
-Wclippy::undocumented_unsafe_blocks \
493494
-Wclippy::unnecessary_safety_comment \
494495
-Wclippy::unnecessary_safety_doc \

rust/bindings/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#[allow(dead_code)]
2828
#[allow(clippy::cast_lossless)]
2929
#[allow(clippy::ptr_as_ptr)]
30+
#[allow(clippy::ref_as_ptr)]
3031
#[allow(clippy::undocumented_unsafe_blocks)]
3132
#[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))]
3233
mod bindings_raw {

rust/kernel/configfs.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ where
426426
};
427427

428428
const fn vtable_ptr() -> *const bindings::configfs_group_operations {
429-
&Self::VTABLE as *const bindings::configfs_group_operations
429+
&Self::VTABLE
430430
}
431431
}
432432

@@ -464,7 +464,7 @@ where
464464
};
465465

466466
const fn vtable_ptr() -> *const bindings::configfs_item_operations {
467-
&Self::VTABLE as *const bindings::configfs_item_operations
467+
&Self::VTABLE
468468
}
469469
}
470470

@@ -476,7 +476,7 @@ impl<Data> ItemOperationsVTable<Subsystem<Data>, Data> {
476476
};
477477

478478
const fn vtable_ptr() -> *const bindings::configfs_item_operations {
479-
&Self::VTABLE as *const bindings::configfs_item_operations
479+
&Self::VTABLE
480480
}
481481
}
482482

@@ -717,11 +717,7 @@ impl<const N: usize, Data> AttributeList<N, Data> {
717717

718718
// SAFETY: By function safety requirements, we have exclusive access to
719719
// `self` and the reference created below will be exclusive.
720-
unsafe {
721-
(&mut *self.0.get())[I] = (attribute as *const Attribute<ID, O, Data>)
722-
.cast_mut()
723-
.cast()
724-
};
720+
unsafe { (&mut *self.0.get())[I] = core::ptr::from_ref(attribute).cast_mut().cast() };
725721
}
726722
}
727723

@@ -761,9 +757,7 @@ macro_rules! impl_item_type {
761757
ct_owner: owner.as_ptr(),
762758
ct_group_ops: GroupOperationsVTable::<Data, Child>::vtable_ptr().cast_mut(),
763759
ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr().cast_mut(),
764-
ct_attrs: (attributes as *const AttributeList<N, Data>)
765-
.cast_mut()
766-
.cast(),
760+
ct_attrs: core::ptr::from_ref(attributes).cast_mut().cast(),
767761
ct_bin_attrs: core::ptr::null_mut(),
768762
}),
769763
_p: PhantomData,
@@ -780,9 +774,7 @@ macro_rules! impl_item_type {
780774
ct_owner: owner.as_ptr(),
781775
ct_group_ops: core::ptr::null_mut(),
782776
ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr().cast_mut(),
783-
ct_attrs: (attributes as *const AttributeList<N, Data>)
784-
.cast_mut()
785-
.cast(),
777+
ct_attrs: core::ptr::from_ref(attributes).cast_mut().cast(),
786778
ct_bin_attrs: core::ptr::null_mut(),
787779
}),
788780
_p: PhantomData,

rust/kernel/device_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<T: RawDeviceId, U, const N: usize> IdTable<T, U> for IdArray<T, U, N> {
136136
fn as_ptr(&self) -> *const T::RawType {
137137
// This cannot be `self.ids.as_ptr()`, as the return pointer must have correct provenance
138138
// to access the sentinel.
139-
(self as *const Self).cast()
139+
core::ptr::from_ref(self).cast()
140140
}
141141

142142
fn id(&self, index: usize) -> &T::RawType {

rust/kernel/fs/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl core::ops::Deref for File {
366366
//
367367
// By the type invariants, there are no `fdget_pos` calls that did not take the
368368
// `f_pos_lock` mutex.
369-
unsafe { LocalFile::from_raw_file((self as *const Self).cast()) }
369+
unsafe { LocalFile::from_raw_file(core::ptr::from_ref(self).cast()) }
370370
}
371371
}
372372

rust/kernel/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl BStr {
2929
#[inline]
3030
pub const fn from_bytes(bytes: &[u8]) -> &Self {
3131
// SAFETY: `BStr` is transparent to `[u8]`.
32-
unsafe { &*(bytes as *const [u8] as *const BStr) }
32+
unsafe { &*(core::ptr::from_ref(bytes) as *const BStr) }
3333
}
3434

3535
/// Strip a prefix from `self`. Delegates to [`slice::strip_prefix`].
@@ -290,7 +290,7 @@ impl CStr {
290290
#[inline]
291291
pub unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr {
292292
// SAFETY: Properties of `bytes` guaranteed by the safety precondition.
293-
unsafe { &mut *(bytes as *mut [u8] as *mut CStr) }
293+
unsafe { &mut *(core::ptr::from_mut(bytes) as *mut CStr) }
294294
}
295295

296296
/// Returns a C pointer to the string.

rust/kernel/uaccess.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl UserSliceReader {
240240
pub fn read_slice(&mut self, out: &mut [u8]) -> Result {
241241
// SAFETY: The types are compatible and `read_raw` doesn't write uninitialized bytes to
242242
// `out`.
243-
let out = unsafe { &mut *(out as *mut [u8] as *mut [MaybeUninit<u8>]) };
243+
let out = unsafe { &mut *(core::ptr::from_mut(out) as *mut [MaybeUninit<u8>]) };
244244
self.read_raw(out)
245245
}
246246

@@ -355,7 +355,7 @@ impl UserSliceWriter {
355355
let res = unsafe {
356356
bindings::_copy_to_user(
357357
self.ptr as *mut c_void,
358-
(value as *const T).cast::<c_void>(),
358+
core::ptr::from_ref(value).cast::<c_void>(),
359359
len,
360360
)
361361
};

rust/uapi/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
clippy::all,
1717
clippy::cast_lossless,
1818
clippy::ptr_as_ptr,
19+
clippy::ref_as_ptr,
1920
clippy::undocumented_unsafe_blocks,
2021
dead_code,
2122
missing_docs,

0 commit comments

Comments
 (0)
0