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
112 112
113impl<T: ?Sized> Copy for AllData<T> {}113impl<T: ?Sized> Copy for AllData<T> {}
114 114
115// SAFETY: TODO.
115unsafe impl<T: ?Sized> InitData for AllData<T> {116unsafe impl<T: ?Sized> InitData for AllData<T> {
116 type Datee = T;117 type Datee = T;
117}118}
118 119
120// SAFETY: TODO.
119unsafe impl<T: ?Sized> HasInitData for T {121unsafe impl<T: ?Sized> HasInitData for T {
120 type InitData = AllData<T>;122 type InitData = AllData<T>;
121 123
rust/kernel/init/macros.rs+9−0
@@ -513,6 +513,7 @@ macro_rules! __pinned_drop { · 512 unmodified lines
513 }513 }
514 ),514 ),
515 ) => {515 ) => {
516 // SAFETY: TODO.
516 unsafe $($impl_sig)* {517 unsafe $($impl_sig)* {
517 // Inherit all attributes and the type/ident tokens for the signature.518 // Inherit all attributes and the type/ident tokens for the signature.
518 $(#[$($attr)*])*519 $(#[$($attr)*])*
@@ -872,6 +873,7 @@ macro_rules! __pin_data { · 353 unmodified lines
872 }873 }
873 }874 }
874 875
876 // SAFETY: TODO.
875 unsafe impl<$($impl_generics)*>877 unsafe impl<$($impl_generics)*>
876 $crate::init::__internal::PinData for __ThePinData<$($ty_generics)*>878 $crate::init::__internal::PinData for __ThePinData<$($ty_generics)*>
877 where $($whr)*879 where $($whr)*
@@ -997,6 +999,7 @@ macro_rules! __pin_data { · 119 unmodified lines
997 slot: *mut $p_type,999 slot: *mut $p_type,
998 init: impl $crate::init::PinInit<$p_type, E>,1000 init: impl $crate::init::PinInit<$p_type, E>,
999 ) -> ::core::result::Result<(), E> {1001 ) -> ::core::result::Result<(), E> {
1002 // SAFETY: TODO.
1000 unsafe { $crate::init::PinInit::__pinned_init(init, slot) }1003 unsafe { $crate::init::PinInit::__pinned_init(init, slot) }
1001 }1004 }
1002 )*1005 )*
@@ -1007,6 +1010,7 @@ macro_rules! __pin_data { · 4 unmodified lines
1007 slot: *mut $type,1010 slot: *mut $type,
1008 init: impl $crate::init::Init<$type, E>,1011 init: impl $crate::init::Init<$type, E>,
1009 ) -> ::core::result::Result<(), E> {1012 ) -> ::core::result::Result<(), E> {
1013 // SAFETY: TODO.
1010 unsafe { $crate::init::Init::__init(init, slot) }1014 unsafe { $crate::init::Init::__init(init, slot) }
1011 }1015 }
1012 )*1016 )*
@@ -1121,6 +1125,8 @@ macro_rules! __init_internal { · 108 unmodified lines
1121 // no possibility of returning without `unsafe`.1125 // no possibility of returning without `unsafe`.
1122 struct __InitOk;1126 struct __InitOk;
1123 // Get the data about fields from the supplied type.1127 // Get the data about fields from the supplied type.
1128 //
1129 // SAFETY: TODO.
1124 let data = unsafe {1130 let data = unsafe {
1125 use $crate::init::__internal::$has_data;1131 use $crate::init::__internal::$has_data;
1126 // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal1132 // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
@@ -1176,6 +1182,7 @@ macro_rules! __init_internal { · 49 unmodified lines
1176 let init = move |slot| -> ::core::result::Result<(), $err> {1182 let init = move |slot| -> ::core::result::Result<(), $err> {
1177 init(slot).map(|__InitOk| ())1183 init(slot).map(|__InitOk| ())
1178 };1184 };
1185 // SAFETY: TODO.
1179 let init = unsafe { $crate::init::$construct_closure::<_, $err>(init) };1186 let init = unsafe { $crate::init::$construct_closure::<_, $err>(init) };
1180 init1187 init
1181 }};1188 }};
@@ -1324,6 +1331,8 @@ macro_rules! __init_internal { · 142 unmodified lines
1324 // Endpoint, nothing more to munch, create the initializer.1331 // Endpoint, nothing more to munch, create the initializer.
1325 // Since we are in the closure that is never called, this will never get executed.1332 // Since we are in the closure that is never called, this will never get executed.
1326 // We abuse `slot` to get the correct type inference here:1333 // We abuse `slot` to get the correct type inference here:
1334 //
1335 // SAFETY: TODO.
1327 unsafe {1336 unsafe {
1328 // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal1337 // Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
1329 // information that is associated to already parsed fragments, so a path fragment1338 // information that is associated to already parsed fragments, so a path fragment