[PATCH v6 03/18] rust: task: remove use of `addr_of!` macro
From: Antonio Hickey
Date: Thu Apr 17 2025 - 21:42:44 EST
The use of `addr_of!` here is unnecessary since its immediately
dereferenced. The main benefit of `addr_of!` is to avoid intermediate
field loads without immediate dereferencing, so there's no benefit in
using it here.
We can achieve the same behavior by directly accessing the
`group_leader` and `pid` fields, which is more idiomatic.
Suggested-by: Benno Lossin <benno.lossin@xxxxxxxxx>
Link: https://github.com/Rust-for-Linux/linux/issues/1148
Signed-off-by: Antonio Hickey <contact@xxxxxxxxxxxxxxxxx>
---
rust/kernel/task.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 9e6f6854948d..554b42c609af 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -259,7 +259,7 @@ pub fn as_ptr(&self) -> *mut bindings::task_struct {
pub fn group_leader(&self) -> &Task {
// SAFETY: The group leader of a task never changes after initialization, so reading this
// field is not a data race.
- let ptr = unsafe { *ptr::addr_of!((*self.as_ptr()).group_leader) };
+ let ptr = unsafe { (*self.as_ptr()).group_leader };
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
// and given that a task has a reference to its group leader, we know it must be valid for
@@ -271,7 +271,7 @@ pub fn group_leader(&self) -> &Task {
pub fn pid(&self) -> Pid {
// SAFETY: The pid of a task never changes after initialization, so reading this field is
// not a data race.
- unsafe { *ptr::addr_of!((*self.as_ptr()).pid) }
+ unsafe { (*self.as_ptr()).pid }
}
/// Returns the UID of the given task.
--
2.48.1