History

rust: enable `clippy::undocumented_unsafe_blocks` lint

Checking that we are not missing any `// SAFETY` comments in our `unsafe` blocks is something we have wanted to do for a long time, as well as cleaning up the remaining cases that were not documented [1]. Back when Rust for Linux started, this was something that could have been done via a script, like Rust's `tidy`. Soon after, in Rust 1.58.0, Clippy implemented the `undocumented_unsafe_blocks` lint [2]. Even though the lint has a few false positives, e.g. in some cases where attributes appear between the comment and the `unsafe` block [3], there are workarounds and the lint seems quite usable already. Thus enable the lint now. We still have a few cases to clean up, so just allow those for the moment by writing a `TODO` comment -- some of those may be good candidates for new contributors.

Miguel Ojeda committed 2 years ago

Commit db4f72c1 parent 567cdff16 files · +47 −10Browse files

Showing the changes to rust/kernel/init (2 of 16 files). Show all

Unified · Split

2 files
  1. rust/kernel/init/__internal.rs +2 −0
  2. rust/kernel/init/macros.rs +9 −0
rust/kernel/init/__internal.rs+2−0
@@ -112,10 +112,12 @@ impl<T: ?Sized> Clone for AllData<T> { · 111 unmodified lines
112112
113113 impl<T: ?Sized> Copy for AllData<T> {}
114114
115+// SAFETY: TODO.
115116 unsafe impl<T: ?Sized> InitData for AllData<T> {
116117 type Datee = T;
117118 }
118119
120+// SAFETY: TODO.
119121 unsafe impl<T: ?Sized> HasInitData for T {
120122 type InitData = AllData<T>;
121123
rust/kernel/init/macros.rs+9−0
@@ -513,6 +513,7 @@ macro_rules! __pinned_drop { · 512 unmodified lines
513513 }
514514 ),
515515 ) => {
516+ // SAFETY: TODO.
516517 unsafe $($impl_sig)* {
517518 // Inherit all attributes and the type/ident tokens for the signature.
518519 $(#[$($attr)*])*
@@ -872,6 +873,7 @@ macro_rules! __pin_data { · 353 unmodified lines
872873 }
873874 }
874875
876+ // SAFETY: TODO.
875877 unsafe impl<$($impl_generics)*>
876878 $crate::init::__internal::PinData for __ThePinData<$($ty_generics)*>
877879 where $($whr)*
@@ -997,6 +999,7 @@ macro_rules! __pin_data { · 119 unmodified lines
997999 slot: *mut $p_type,
9981000 init: impl $crate::init::PinInit<$p_type, E>,
9991001 ) -> ::core::result::Result<(), E> {
1002+ // SAFETY: TODO.
10001003 unsafe { $crate::init::PinInit::__pinned_init(init, slot) }
10011004 }
10021005 )*
@@ -1007,6 +1010,7 @@ macro_rules! __pin_data { · 4 unmodified lines
10071010 slot: *mut $type,
10081011 init: impl $crate::init::Init<$type, E>,
10091012 ) -> ::core::result::Result<(), E> {
1013+ // SAFETY: TODO.
10101014 unsafe { $crate::init::Init::__init(init, slot) }
10111015 }
10121016 )*
@@ -1121,6 +1125,8 @@ macro_rules! __init_internal { · 108 unmodified lines
11211125 // no possibility of returning without `unsafe`.
11221126 struct __InitOk;
11231127 // Get the data about fields from the supplied type.
1128+ //
1129+ // SAFETY: TODO.
11241130 let data = unsafe {
11251131 use $crate::init::__internal::$has_data;
11261132 // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
@@ -1176,6 +1182,7 @@ macro_rules! __init_internal { · 49 unmodified lines
11761182 let init = move |slot| -> ::core::result::Result<(), $err> {
11771183 init(slot).map(|__InitOk| ())
11781184 };
1185+ // SAFETY: TODO.
11791186 let init = unsafe { $crate::init::$construct_closure::<_, $err>(init) };
11801187 init
11811188 }};
@@ -1324,6 +1331,8 @@ macro_rules! __init_internal { · 142 unmodified lines
13241331 // Endpoint, nothing more to munch, create the initializer.
13251332 // Since we are in the closure that is never called, this will never get executed.
13261333 // We abuse `slot` to get the correct type inference here:
1334+ //
1335+ // SAFETY: TODO.
13271336 unsafe {
13281337 // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
13291338 // information that is associated to already parsed fragments, so a path fragment