Skip to content

Commit

Permalink
Add a couple of examples to undocumented_unsafe_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Apr 2, 2022
1 parent 30b3336 commit 17c8bee
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
18 changes: 18 additions & 0 deletions clippy_lints/src/undocumented_unsafe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ declare_clippy_lint! {
/// explaining why the unsafe operations performed inside
/// 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?
/// Undocumented unsafe blocks can make it difficult to
/// read and maintain code, as well as uncover unsoundness
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/undocumented_unsafe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ fn from_proc_macro() {

// Invalid comments

#[rustfmt::skip]
fn inline_block_comment() {
/* Safety: */ unsafe {}
}

fn no_comment() {
unsafe {}
}
Expand Down
46 changes: 27 additions & 19 deletions tests/ui/undocumented_unsafe_blocks.stderr
Original file line number Diff line number Diff line change
@@ -1,102 +1,110 @@
error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:255:5
--> $DIR/undocumented_unsafe_blocks.rs:256:19
|
LL | /* Safety: */ unsafe {}
| ^^^^^^^^^
|
= note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings`
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:260:5
|
LL | unsafe {}
| ^^^^^^^^^
|
= note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings`
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:259:14
--> $DIR/undocumented_unsafe_blocks.rs:264:14
|
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
| ^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

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 }];
| ^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

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 }];
| ^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

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 {});
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

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 {});
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

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 };
| ^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

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 {} {
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:278:14
--> $DIR/undocumented_unsafe_blocks.rs:283:14
|
LL | let _ = &unsafe {};
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

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];
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:286:13
--> $DIR/undocumented_unsafe_blocks.rs:291:13
|
LL | let _ = unsafe {};
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:296:8
--> $DIR/undocumented_unsafe_blocks.rs:301:8
|
LL | t!(unsafe {});
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:302:13
--> $DIR/undocumented_unsafe_blocks.rs:307:13
|
LL | unsafe {}
| ^^^^^^^^^
Expand All @@ -108,36 +116,36 @@ LL | t!();
= 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
--> $DIR/undocumented_unsafe_blocks.rs:310:5
--> $DIR/undocumented_unsafe_blocks.rs:315:5
|
LL | unsafe {} // SAFETY:
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:314:5
--> $DIR/undocumented_unsafe_blocks.rs:319: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:324:5
--> $DIR/undocumented_unsafe_blocks.rs:329: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:328:20
--> $DIR/undocumented_unsafe_blocks.rs:333:20
|
LL | println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: aborting due to 17 previous errors
error: aborting due to 18 previous errors

0 comments on commit 17c8bee

Please sign in to comment.