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/sync (2 of 16 files). Show all

Unified · Split

2 files
  1. rust/kernel/sync/condvar.rs +1 −1
  2. rust/kernel/sync/lock.rs +3 −3
rust/kernel/sync/condvar.rs+1−1
@@ -92,8 +92,8 @@ pub struct CondVar { · 91 unmodified lines
9292 _pin: PhantomPinned,
9393 }
9494
95−// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on any thread.
9695 #[allow(clippy::non_send_fields_in_send_ty)]
96+// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on any thread.
9797 unsafe impl Send for CondVar {}
9898
9999 // SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on multiple threads
rust/kernel/sync/lock.rs+3−3
@@ -150,9 +150,9 @@ impl<T: ?Sized, B: Backend> Guard<'_, T, B> { · 149 unmodified lines
150150 // SAFETY: The caller owns the lock, so it is safe to unlock it.
151151 unsafe { B::unlock(self.lock.state.get(), &self.state) };
152152
153− // SAFETY: The lock was just unlocked above and is being relocked now.
154− let _relock =
155− ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) });
153+ let _relock = ScopeGuard::new(||
154+ // SAFETY: The lock was just unlocked above and is being relocked now.
155+ unsafe { B::relock(self.lock.state.get(), &mut self.state) });
156156
157157 cb()
158158 }