Skip to contentSkip to conversation

rust: init: fix `clippy::undocumented_unsafe_blocks` warnings

PATCH In review Miguel Ojeda 2 participants 2 messages Apr 26 – Apr 27

Subsystems (2)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 rust: init: fix `clippy::undocumented_unsafe_blocks` warnings

The stable backport in commit acc105db0826 ("rust: pin-init:
add references to previously initialized fields") introduced some clippy::undocumented_unsafe_blocks warnings [1], e.g.

error: unsafe block missing a safety comment
--> rust/kernel/init/macros.rs:1015:25

As well as:

    --> rust/kernel/init/macros.rs:1243:45
    --> rust/kernel/init/macros.rs:1286:22
    --> rust/kernel/init/macros.rs:1374:45

After discussing it with Benno and Gary, we decided to clean the build log by doing a minimal targeted stable commit.

Thus, depending on the case:

- Reorder the attributes so that the existing // SAFETY: comments may be seen by Clippy.

- Add a placeholder // SAFETY: TODO. comment.

Cc Benno Lossin <[email protected]>
Cc Gary Guo <[email protected]>
Fixes acc105db0826 ("rust: pin-init: add references to previously initialized fields")

---
Greg/Sasha: please let Benno & Gary Acked-by the patch before picking it up -- thanks!

 rust/kernel/init/macros.rs | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
rust/kernel/init/macros.rs +4 -3
index e477e4de817b..d6e27c522115 100644
@@ -1012,6 +1012,7 @@ impl<$($impl_generics)*> $pin_data<$($ty_generics)*>
                         self,
                         slot: &'__slot mut $p_type,
                     ) -> ::core::pin::Pin<&'__slot mut $p_type> {
+                        // SAFETY: TODO.
                         unsafe { ::core::pin::Pin::new_unchecked(slot) }
                     }
                 )*
@@ -1235,11 +1236,11 @@ fn assert_zeroable<T: $crate::init::Zeroable>(_: *mut T) {}
         // Unaligned fields will cause the compiler to emit E0793. We do not support
         // unaligned fields since `Init::__init` requires an aligned pointer; the call to
         // `ptr::write` below has the same requirement.
+        #[allow(unused_variables, unused_assignments)]
         // SAFETY:
         // - the project function does the correct field projection,
         // - the field has been initialized,
         // - the reference is only valid until the end of the initializer.
-        #[allow(unused_variables, unused_assignments)]
         let $field = $crate::macros::paste!(unsafe { $data.[< __project_ $field >](&mut (*$slot).$field) });
 
         // Create the drop guard:
@@ -1278,11 +1279,11 @@ fn assert_zeroable<T: $crate::init::Zeroable>(_: *mut T) {}
         // Unaligned fields will cause the compiler to emit E0793. We do not support
         // unaligned fields since `Init::__init` requires an aligned pointer; the call to
         // `ptr::write` below has the same requirement.
+        #[allow(unused_variables, unused_assignments)]
         // SAFETY:
         // - the field is not structurally pinned, since the line above must compile,
         // - the field has been initialized,
         // - the reference is only valid until the end of the initializer.
-        #[allow(unused_variables, unused_assignments)]
         let $field = unsafe { &mut (*$slot).$field };
 
         // Create the drop guard:
@@ -1366,11 +1367,11 @@ fn assert_zeroable<T: $crate::init::Zeroable>(_: *mut T) {}
         // Unaligned fields will cause the compiler to emit E0793. We do not support
         // unaligned fields since `Init::__init` requires an aligned pointer; the call to
         // `ptr::write` below has the same requirement.
+        #[allow(unused_variables, unused_assignments)]
         // SAFETY:
         // - the project function does the correct field projection,
         // - the field has been initialized,
         // - the reference is only valid until the end of the initializer.
-        #[allow(unused_variables, unused_assignments)]
         let $field = $crate::macros::paste!(unsafe { $data.[< __project_ $field >](&mut (*$slot).$field) });
 
         // Create the drop guard:

base-commit: 59f8529e78a2fc581c35fbed58169f5e8c79b8d7 --
2.53.0

reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 21 lines · Miguel Ojeda

The stable backport in commit acc105db0826 ("rust: pin-init:
add references to previously initialized fields") introduced some clippy::undocumented_unsafe_blocks warnings [1], e.g.

error: unsafe block missing a safety comment
--> rust/kernel/init/macros.rs:1015:25

As well as:

    --> rust/kernel/init/macros.rs:1243:45
    --> rust/kernel/init/macros.rs:1286:22
    --> rust/kernel/init/macros.rs:1374:45

After discussing it with Benno and Gary, we decided to clean the build log by doing a minimal targeted stable commit.

Thus, depending on the case:

- Reorder the attributes so that the existing // SAFETY: comments may be seen by Clippy.

Quoted 7 lines

- Add a placeholder // SAFETY: TODO. comment.

Cc Benno Lossin <[email protected]>
Cc Gary Guo <[email protected]>
Fixes acc105db0826 ("rust: pin-init: add references to previously initialized fields")
Quoted 6 lines

---
Greg/Sasha: please let Benno & Gary Acked-by the patch before picking it up -- thanks!

 rust/kernel/init/macros.rs | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)