Add a couple of examples to undocumented_unsafe_blocks

This commit is contained in:
Jason Newcomb
2022-04-02 00:46:45 -04:00
parent 30b333645d
commit 17c8bee95a
3 changed files with 51 additions and 20 deletions

View File

@@ -15,6 +15,24 @@ declare_clippy_lint! {
/// explaining why the unsafe operations performed inside /// explaining why the unsafe operations performed inside
/// the block are safe. /// the block are safe.
/// ///
/// Note the comment must appear on the line(s) preceding the unsafe block
/// with nothing appearing in between. The following is ok:
/// ```ignore
/// foo(
/// // SAFETY:
/// // This is a valid safety comment
/// unsafe { *x }
/// )
/// ```
/// But neither of these are:
/// ```ignore
/// // SAFETY:
/// // This is not a valid safety comment
/// foo(
/// /* SAFETY: Neither is this */ unsafe { *x },
/// );
/// ```
///
/// ### Why is this bad? /// ### Why is this bad?
/// Undocumented unsafe blocks can make it difficult to /// Undocumented unsafe blocks can make it difficult to
/// read and maintain code, as well as uncover unsoundness /// read and maintain code, as well as uncover unsoundness

View File

@@ -251,6 +251,11 @@ fn from_proc_macro() {
// Invalid comments // Invalid comments
#[rustfmt::skip]
fn inline_block_comment() {
/* Safety: */ unsafe {}
}
fn no_comment() { fn no_comment() {
unsafe {} unsafe {}
} }

View File

@@ -1,14 +1,22 @@
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:255:5 --> $DIR/undocumented_unsafe_blocks.rs:256:19
| |
LL | unsafe {} LL | /* Safety: */ unsafe {}
| ^^^^^^^^^ | ^^^^^^^^^
| |
= note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings` = note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings`
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:259:14 --> $DIR/undocumented_unsafe_blocks.rs:260:5
|
LL | unsafe {}
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:264:14
| |
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }]; LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
@@ -16,7 +24,7 @@ LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:259:29 --> $DIR/undocumented_unsafe_blocks.rs:264:29
| |
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }]; LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
@@ -24,7 +32,7 @@ LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:259:48 --> $DIR/undocumented_unsafe_blocks.rs:264:48
| |
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }]; LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
@@ -32,7 +40,7 @@ LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:263:18 --> $DIR/undocumented_unsafe_blocks.rs:268:18
| |
LL | let _ = (42, unsafe {}, "test", unsafe {}); LL | let _ = (42, unsafe {}, "test", unsafe {});
| ^^^^^^^^^ | ^^^^^^^^^
@@ -40,7 +48,7 @@ LL | let _ = (42, unsafe {}, "test", unsafe {});
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:263:37 --> $DIR/undocumented_unsafe_blocks.rs:268:37
| |
LL | let _ = (42, unsafe {}, "test", unsafe {}); LL | let _ = (42, unsafe {}, "test", unsafe {});
| ^^^^^^^^^ | ^^^^^^^^^
@@ -48,7 +56,7 @@ LL | let _ = (42, unsafe {}, "test", unsafe {});
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:267:14 --> $DIR/undocumented_unsafe_blocks.rs:272:14
| |
LL | let _ = *unsafe { &42 }; LL | let _ = *unsafe { &42 };
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@@ -56,7 +64,7 @@ LL | let _ = *unsafe { &42 };
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:272:19 --> $DIR/undocumented_unsafe_blocks.rs:277:19
| |
LL | let _ = match unsafe {} { LL | let _ = match unsafe {} {
| ^^^^^^^^^ | ^^^^^^^^^
@@ -64,7 +72,7 @@ LL | let _ = match unsafe {} {
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:278:14 --> $DIR/undocumented_unsafe_blocks.rs:283:14
| |
LL | let _ = &unsafe {}; LL | let _ = &unsafe {};
| ^^^^^^^^^ | ^^^^^^^^^
@@ -72,7 +80,7 @@ LL | let _ = &unsafe {};
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:282:14 --> $DIR/undocumented_unsafe_blocks.rs:287:14
| |
LL | let _ = [unsafe {}; 5]; LL | let _ = [unsafe {}; 5];
| ^^^^^^^^^ | ^^^^^^^^^
@@ -80,7 +88,7 @@ LL | let _ = [unsafe {}; 5];
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:286:13 --> $DIR/undocumented_unsafe_blocks.rs:291:13
| |
LL | let _ = unsafe {}; LL | let _ = unsafe {};
| ^^^^^^^^^ | ^^^^^^^^^
@@ -88,7 +96,7 @@ LL | let _ = unsafe {};
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:296:8 --> $DIR/undocumented_unsafe_blocks.rs:301:8
| |
LL | t!(unsafe {}); LL | t!(unsafe {});
| ^^^^^^^^^ | ^^^^^^^^^
@@ -96,7 +104,7 @@ LL | t!(unsafe {});
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:302:13 --> $DIR/undocumented_unsafe_blocks.rs:307:13
| |
LL | unsafe {} LL | unsafe {}
| ^^^^^^^^^ | ^^^^^^^^^
@@ -108,7 +116,7 @@ LL | t!();
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:310:5 --> $DIR/undocumented_unsafe_blocks.rs:315:5
| |
LL | unsafe {} // SAFETY: LL | unsafe {} // SAFETY:
| ^^^^^^^^^ | ^^^^^^^^^
@@ -116,7 +124,7 @@ LL | unsafe {} // SAFETY:
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:314:5 --> $DIR/undocumented_unsafe_blocks.rs:319:5
| |
LL | unsafe { LL | unsafe {
| ^^^^^^^^ | ^^^^^^^^
@@ -124,7 +132,7 @@ LL | unsafe {
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:324:5 --> $DIR/undocumented_unsafe_blocks.rs:329:5
| |
LL | unsafe {}; LL | unsafe {};
| ^^^^^^^^^ | ^^^^^^^^^
@@ -132,12 +140,12 @@ LL | unsafe {};
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:328:20 --> $DIR/undocumented_unsafe_blocks.rs:333:20
| |
LL | println!("{}", unsafe { String::from_utf8_unchecked(vec![]) }); LL | println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= help: consider adding a safety comment on the preceding line = help: consider adding a safety comment on the preceding line
error: aborting due to 17 previous errors error: aborting due to 18 previous errors