Skip to content

Commit 5cfe7be

Browse files
ojeday86-dev
authored andcommitted
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. Link: Rust-for-Linux/linux#351 [1] Link: https://rust-lang.github.io/rust-clippy/master/#/undocumented_unsafe_blocks [2] Link: rust-lang/rust-clippy#13189 [3] Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Trevor Gross <[email protected]> Tested-by: Gary Guo <[email protected]> Reviewed-by: Gary Guo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> (cherry picked from commit db4f72c904cb116e2bf56afdd67fc5167a607a7b) Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 10fee40 commit 5cfe7be

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ needless_bitwise_bool = "deny"
4949
needless_continue = "deny"
5050
needless_lifetimes = "deny"
5151
no_mangle_with_rust_abi = "deny"
52+
undocumented_unsafe_blocks = "deny"
5253
unnecessary_safety_comment = "deny"
5354
unnecessary_safety_doc = "deny"

src/__internal.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ impl<T: ?Sized> Clone for AllData<T> {
120120

121121
impl<T: ?Sized> Copy for AllData<T> {}
122122

123+
// SAFETY: TODO.
123124
unsafe impl<T: ?Sized> InitData for AllData<T> {
124125
type Datee = T;
125126
}
126127

128+
// SAFETY: TODO.
127129
unsafe impl<T: ?Sized> HasInitData for T {
128130
type InitData = AllData<T>;
129131

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ macro_rules! stack_try_pin_init {
583583
///
584584
/// let init = pin_init!(&this in Buf {
585585
/// buf: [0; 64],
586+
/// // SAFETY: TODO.
586587
/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
587588
/// pin: PhantomPinned,
588589
/// });
@@ -1153,6 +1154,7 @@ where
11531154
// SAFETY: Every type can be initialized by-value.
11541155
unsafe impl<T, E> Init<T, E> for T {
11551156
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
1157+
// SAFETY: TODO.
11561158
unsafe { slot.write(self) };
11571159
Ok(())
11581160
}
@@ -1161,6 +1163,7 @@ unsafe impl<T, E> Init<T, E> for T {
11611163
// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
11621164
unsafe impl<T, E> PinInit<T, E> for T {
11631165
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
1166+
// SAFETY: TODO.
11641167
unsafe { self.__init(slot) }
11651168
}
11661169
}
@@ -1387,6 +1390,7 @@ macro_rules! impl_zeroable {
13871390
($($(#[$attr:meta])*$({$($generics:tt)*})? $t:ty, )*) => {
13881391
$(
13891392
$(#[$attr])*
1393+
// SAFETY: Safety comments written in the macro invocation.
13901394
unsafe impl$($($generics)*)? Zeroable for $t {}
13911395
)*
13921396
};

src/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ macro_rules! __pinned_drop {
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 {
872873
}
873874
}
874875

876+
// SAFETY: TODO.
875877
unsafe impl<$($impl_generics)*>
876878
$crate::__internal::PinData for __ThePinData<$($ty_generics)*>
877879
where $($whr)*
@@ -996,6 +998,7 @@ macro_rules! __pin_data {
996998
slot: *mut $p_type,
997999
init: impl $crate::PinInit<$p_type, E>,
9981000
) -> ::core::result::Result<(), E> {
1001+
// SAFETY: TODO.
9991002
unsafe { $crate::PinInit::__pinned_init(init, slot) }
10001003
}
10011004
)*
@@ -1005,6 +1008,7 @@ macro_rules! __pin_data {
10051008
slot: *mut $type,
10061009
init: impl $crate::Init<$type, E>,
10071010
) -> ::core::result::Result<(), E> {
1011+
// SAFETY: TODO.
10081012
unsafe { $crate::Init::__init(init, slot) }
10091013
}
10101014
)*
@@ -1121,6 +1125,8 @@ macro_rules! __init_internal {
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::__internal::$has_data;
11261132
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
@@ -1176,6 +1182,7 @@ macro_rules! __init_internal {
11761182
let init = move |slot| -> ::core::result::Result<(), $err> {
11771183
init(slot).map(|__InitOk| ())
11781184
};
1185+
// SAFETY: TODO.
11791186
let init = unsafe { $crate::$construct_closure::<_, $err>(init) };
11801187
init
11811188
}};
@@ -1324,6 +1331,8 @@ macro_rules! __init_internal {
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

0 commit comments

Comments
 (0)