Skip to content

Commit

Permalink
Change default configuration of undocumented_unsafe_blocks
Browse files Browse the repository at this point in the history
This patch sets the two configuration options for
`undocumented_unsafe_blocks` to `true` by default: these are
`accept-comment-above-statement` and `accept-comment-above-attributes`.
Having these values `false` by default prevents what many users would
consider clean code, e.g. placing the `// SAFETY:` comment above a
single-line functino call, rather than directly next to the argument.

changelog: [`undocumented_unsafe_blocks`]: set
`accept-comment-above-statement` and `accept-comment-above-attributes`
to `true` by default.
  • Loading branch information
tgross35 committed Sep 20, 2023
1 parent f464149 commit 1b3e5dd
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 632 deletions.
4 changes: 2 additions & 2 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ Minimum chars an ident can have, anything below or equal to this will be linted.
## `accept-comment-above-statement`
Whether to accept a safety comment to be placed above the statement containing the `unsafe` block

**Default Value:** `false` (`bool`)
**Default Value:** `true` (`bool`)

---
**Affected lints:**
Expand All @@ -713,7 +713,7 @@ Whether to accept a safety comment to be placed above the statement containing t
## `accept-comment-above-attributes`
Whether to accept a safety comment to be placed above the attributes for the `unsafe` block

**Default Value:** `false` (`bool`)
**Default Value:** `true` (`bool`)

---
**Affected lints:**
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,11 @@ define_Conf! {
/// Lint: UNDOCUMENTED_UNSAFE_BLOCKS.
///
/// Whether to accept a safety comment to be placed above the statement containing the `unsafe` block
(accept_comment_above_statement: bool = false),
(accept_comment_above_statement: bool = true),
/// Lint: UNDOCUMENTED_UNSAFE_BLOCKS.
///
/// Whether to accept a safety comment to be placed above the attributes for the `unsafe` block
(accept_comment_above_attributes: bool = false),
(accept_comment_above_attributes: bool = true),
/// Lint: UNNECESSARY_RAW_STRING_HASHES.
///
/// Whether to allow `r#""#` when `r""` can be used
Expand Down
2 changes: 0 additions & 2 deletions tests/ui-toml/undocumented_unsafe_blocks/clippy.toml

This file was deleted.

2 changes: 2 additions & 0 deletions tests/ui-toml/undocumented_unsafe_blocks/default/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# default configuration has `accept-comment-above-statement` and
# `accept-comment-above-attributes` true
3 changes: 3 additions & 0 deletions tests/ui-toml/undocumented_unsafe_blocks/disabled/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# test with these options disabled
accept-comment-above-statement = false
accept-comment-above-attributes = false
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:263:19
--> $DIR/undocumented_unsafe_blocks.rs:266:19
|
LL | /* Safety: */ unsafe {}
| ^^^^^^^^^
Expand All @@ -9,103 +9,103 @@ LL | /* Safety: */ unsafe {}
= help: to override `-D warnings` add `#[allow(clippy::undocumented_unsafe_blocks)]`

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:267:5
--> $DIR/undocumented_unsafe_blocks.rs:270: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:271:14
--> $DIR/undocumented_unsafe_blocks.rs:274: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:271:29
--> $DIR/undocumented_unsafe_blocks.rs:274: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:271:48
--> $DIR/undocumented_unsafe_blocks.rs:274: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:275:18
--> $DIR/undocumented_unsafe_blocks.rs:278: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:275:37
--> $DIR/undocumented_unsafe_blocks.rs:278: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:279:14
--> $DIR/undocumented_unsafe_blocks.rs:282: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:284:19
--> $DIR/undocumented_unsafe_blocks.rs:287: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:290:14
--> $DIR/undocumented_unsafe_blocks.rs:293: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:294:14
--> $DIR/undocumented_unsafe_blocks.rs:297: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:298:13
--> $DIR/undocumented_unsafe_blocks.rs:301: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:308:8
--> $DIR/undocumented_unsafe_blocks.rs:311: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:314:13
--> $DIR/undocumented_unsafe_blocks.rs:317:13
|
LL | unsafe {}
| ^^^^^^^^^
Expand All @@ -117,55 +117,55 @@ 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:322:5
--> $DIR/undocumented_unsafe_blocks.rs:325: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:326: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:336:5
--> $DIR/undocumented_unsafe_blocks.rs:339: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:340:20
--> $DIR/undocumented_unsafe_blocks.rs:343:20
|
LL | println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:347:5
--> $DIR/undocumented_unsafe_blocks.rs:350:5
|
LL | unsafe impl A for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:354:9
--> $DIR/undocumented_unsafe_blocks.rs:357:9
|
LL | unsafe impl B for (u32) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:375:13
--> $DIR/undocumented_unsafe_blocks.rs:378:13
|
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -177,7 +177,7 @@ LL | no_safety_comment!(());
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:400:13
--> $DIR/undocumented_unsafe_blocks.rs:403:13
|
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -189,15 +189,15 @@ LL | no_safety_comment!(());
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:408:5
--> $DIR/undocumented_unsafe_blocks.rs:411:5
|
LL | unsafe impl T for (i32) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:400:13
--> $DIR/undocumented_unsafe_blocks.rs:403:13
|
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -209,77 +209,77 @@ LL | no_safety_comment!(u32);
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:414:5
--> $DIR/undocumented_unsafe_blocks.rs:417:5
|
LL | unsafe impl T for (bool) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:460:5
--> $DIR/undocumented_unsafe_blocks.rs:463:5
|
LL | unsafe impl NoComment for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:464:19
--> $DIR/undocumented_unsafe_blocks.rs:467:19
|
LL | /* SAFETY: */ unsafe impl InlineComment for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:468:5
--> $DIR/undocumented_unsafe_blocks.rs:471:5
|
LL | unsafe impl TrailingComment for () {} // SAFETY:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: constant item has unnecessary safety comment
--> $DIR/undocumented_unsafe_blocks.rs:472:5
--> $DIR/undocumented_unsafe_blocks.rs:475:5
|
LL | const BIG_NUMBER: i32 = 1000000;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider removing the safety comment
--> $DIR/undocumented_unsafe_blocks.rs:471:5
--> $DIR/undocumented_unsafe_blocks.rs:474:5
|
LL | // SAFETY:
| ^^^^^^^^^^
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_safety_comment)]`

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:473:5
--> $DIR/undocumented_unsafe_blocks.rs:476:5
|
LL | unsafe impl Interference for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:480:5
--> $DIR/undocumented_unsafe_blocks.rs:483:5
|
LL | unsafe impl ImplInFn for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:489:1
--> $DIR/undocumented_unsafe_blocks.rs:492:1
|
LL | unsafe impl CrateRoot for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: statement has unnecessary safety comment
--> $DIR/undocumented_unsafe_blocks.rs:502:5
--> $DIR/undocumented_unsafe_blocks.rs:505:5
|
LL | / let _ = {
LL | | if unsafe { true } {
Expand All @@ -291,21 +291,21 @@ LL | | };
| |______^
|
help: consider removing the safety comment
--> $DIR/undocumented_unsafe_blocks.rs:501:5
--> $DIR/undocumented_unsafe_blocks.rs:504:5
|
LL | // SAFETY: this is more than one level away, so it should warn
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:503:12
--> $DIR/undocumented_unsafe_blocks.rs:506:12
|
LL | if unsafe { true } {
| ^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line

error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:506:23
--> $DIR/undocumented_unsafe_blocks.rs:509:23
|
LL | let bar = unsafe {};
| ^^^^^^^^^
Expand Down
Loading

0 comments on commit 1b3e5dd

Please sign in to comment.