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.
Showing the changes to rust/kernel/sync (2 of 16 files). Show all
Unified · Split
| @@ -92,8 +92,8 @@ pub struct CondVar { · 91 unmodified lines | ||
| 92 | 92 | _pin: PhantomPinned, |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | −// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on any thread. | |
| 96 | 95 | #[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. | |
| 97 | 97 | unsafe impl Send for CondVar {} |
| 98 | 98 | |
| 99 | 99 | // SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe to use on multiple threads |
| @@ -150,9 +150,9 @@ impl<T: ?Sized, B: Backend> Guard<'_, T, B> { · 149 unmodified lines | ||
| 150 | 150 | // SAFETY: The caller owns the lock, so it is safe to unlock it. |
| 151 | 151 | unsafe { B::unlock(self.lock.state.get(), &self.state) }; |
| 152 | 152 | |
| 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) }); | |
| 156 | 156 | |
| 157 | 157 | cb() |
| 158 | 158 | } |