Skip to content

Commit 07dc32a

Browse files
committed
Auto merge of #11170 - tgross35:undocumented-unsafe-blocks-defaults, r=Centri3
Change defaults of `accept-comment-above-statement` and `accept-comment-above-attributes` 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. This was originally discussed in #11162 changelog: [`undocumented_unsafe_blocks`]: set `accept-comment-above-statement` and `accept-comment-above-attributes` to `true` by default.
2 parents add2722 + 90c37e1 commit 07dc32a

File tree

7 files changed

+177
-96
lines changed

7 files changed

+177
-96
lines changed

book/src/lint_configuration.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ Minimum chars an ident can have, anything below or equal to this will be linted.
703703
## `accept-comment-above-statement`
704704
Whether to accept a safety comment to be placed above the statement containing the `unsafe` block
705705

706-
**Default Value:** `false` (`bool`)
706+
**Default Value:** `true` (`bool`)
707707

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

716-
**Default Value:** `false` (`bool`)
716+
**Default Value:** `true` (`bool`)
717717

718718
---
719719
**Affected lints:**
@@ -750,4 +750,3 @@ Which crates to allow absolute paths from
750750
**Affected lints:**
751751
* [`absolute_paths`](https://rust-lang.github.io/rust-clippy/master/index.html#absolute_paths)
752752

753-

clippy_lints/src/utils/conf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,11 @@ define_Conf! {
542542
/// Lint: UNDOCUMENTED_UNSAFE_BLOCKS.
543543
///
544544
/// Whether to accept a safety comment to be placed above the statement containing the `unsafe` block
545-
(accept_comment_above_statement: bool = false),
545+
(accept_comment_above_statement: bool = true),
546546
/// Lint: UNDOCUMENTED_UNSAFE_BLOCKS.
547547
///
548548
/// Whether to accept a safety comment to be placed above the attributes for the `unsafe` block
549-
(accept_comment_above_attributes: bool = false),
549+
(accept_comment_above_attributes: bool = true),
550550
/// Lint: UNNECESSARY_RAW_STRING_HASHES.
551551
///
552552
/// Whether to allow `r#""#` when `r""` can be used
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
accept-comment-above-statement = true
2-
accept-comment-above-attributes = true
1+
accept-comment-above-statement = false
2+
accept-comment-above-attributes = false

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ unsafe impl CrateRoot for () {}
491491
// SAFETY: ok
492492
unsafe impl CrateRoot for (i32) {}
493493

494-
fn issue_9142() {
494+
fn nested_block_separation_issue_9142() {
495495
// SAFETY: ok
496496
let _ =
497497
// we need this comment to avoid rustfmt putting
@@ -518,49 +518,49 @@ pub const unsafe fn a_const_function_with_a_very_long_name_to_break_the_line() -
518518
2
519519
}
520520

521-
fn issue_10832() {
522-
// Safety: A safety comment
521+
fn separate_line_from_let_issue_10832() {
522+
// Safety: A separate safety comment that will warn
523523
let _some_variable_with_a_very_long_name_to_break_the_line =
524524
unsafe { a_function_with_a_very_long_name_to_break_the_line() };
525525

526-
// Safety: Another safety comment
526+
// Safety: Another separate safety comment that will warn
527527
const _SOME_CONST_WITH_A_VERY_LONG_NAME_TO_BREAK_THE_LINE: u32 =
528528
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
529529

530-
// Safety: Yet another safety comment
530+
// Safety: Yet another separate safety comment that will warn
531531
static _SOME_STATIC_WITH_A_VERY_LONG_NAME_TO_BREAK_THE_LINE: u32 =
532532
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
533533
}
534534

535-
fn issue_8679<T: Copy>() {
536-
// SAFETY:
535+
fn above_expr_attribute_issue_8679<T: Copy>() {
536+
// SAFETY: this should fail, above attribute
537537
#[allow(unsafe_code)]
538538
unsafe {}
539539

540-
// SAFETY:
540+
// SAFETY: this should fail, above attribute
541541
#[expect(unsafe_code, reason = "totally safe")]
542542
unsafe {
543543
*std::ptr::null::<T>()
544544
};
545545

546-
// Safety: A safety comment
546+
// Safety: A safety comment that should fail
547547
#[allow(unsafe_code)]
548548
let _some_variable_with_a_very_long_name_to_break_the_line =
549549
unsafe { a_function_with_a_very_long_name_to_break_the_line() };
550550

551-
// Safety: Another safety comment
551+
// Safety: Another safety comment that should fail
552552
#[allow(unsafe_code)]
553553
const _SOME_CONST_WITH_A_VERY_LONG_NAME_TO_BREAK_THE_LINE: u32 =
554554
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
555555

556-
// Safety: Yet another safety comment
556+
// Safety: Yet another safety comment that should fail
557557
#[allow(unsafe_code)]
558558
static _SOME_STATIC_WITH_A_VERY_LONG_NAME_TO_BREAK_THE_LINE: u32 =
559559
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
560560

561561
// SAFETY:
562562
#[allow(unsafe_code)]
563-
// This also works I guess
563+
// This shouldn't work either
564564
unsafe {}
565565
}
566566

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.stderr

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,14 @@ LL | unsafe impl CrateRoot for () {}
276276
|
277277
= help: consider adding a safety comment on the preceding line
278278

279+
error: unsafe block missing a safety comment
280+
--> $DIR/undocumented_unsafe_blocks.rs:499:9
281+
|
282+
LL | unsafe {};
283+
| ^^^^^^^^^
284+
|
285+
= help: consider adding a safety comment on the preceding line
286+
279287
error: statement has unnecessary safety comment
280288
--> $DIR/undocumented_unsafe_blocks.rs:502:5
281289
|
@@ -310,5 +318,77 @@ LL | let bar = unsafe {};
310318
|
311319
= help: consider adding a safety comment on the preceding line
312320

313-
error: aborting due to 35 previous errors
321+
error: unsafe block missing a safety comment
322+
--> $DIR/undocumented_unsafe_blocks.rs:524:9
323+
|
324+
LL | unsafe { a_function_with_a_very_long_name_to_break_the_line() };
325+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
326+
|
327+
= help: consider adding a safety comment on the preceding line
328+
329+
error: unsafe block missing a safety comment
330+
--> $DIR/undocumented_unsafe_blocks.rs:528:9
331+
|
332+
LL | unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
333+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
334+
|
335+
= help: consider adding a safety comment on the preceding line
336+
337+
error: unsafe block missing a safety comment
338+
--> $DIR/undocumented_unsafe_blocks.rs:532:9
339+
|
340+
LL | unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
341+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
342+
|
343+
= help: consider adding a safety comment on the preceding line
344+
345+
error: unsafe block missing a safety comment
346+
--> $DIR/undocumented_unsafe_blocks.rs:538:5
347+
|
348+
LL | unsafe {}
349+
| ^^^^^^^^^
350+
|
351+
= help: consider adding a safety comment on the preceding line
352+
353+
error: unsafe block missing a safety comment
354+
--> $DIR/undocumented_unsafe_blocks.rs:542:5
355+
|
356+
LL | unsafe {
357+
| ^^^^^^^^
358+
|
359+
= help: consider adding a safety comment on the preceding line
360+
361+
error: unsafe block missing a safety comment
362+
--> $DIR/undocumented_unsafe_blocks.rs:549:9
363+
|
364+
LL | unsafe { a_function_with_a_very_long_name_to_break_the_line() };
365+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
366+
|
367+
= help: consider adding a safety comment on the preceding line
368+
369+
error: unsafe block missing a safety comment
370+
--> $DIR/undocumented_unsafe_blocks.rs:554:9
371+
|
372+
LL | unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
373+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
374+
|
375+
= help: consider adding a safety comment on the preceding line
376+
377+
error: unsafe block missing a safety comment
378+
--> $DIR/undocumented_unsafe_blocks.rs:559:9
379+
|
380+
LL | unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
381+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
382+
|
383+
= help: consider adding a safety comment on the preceding line
384+
385+
error: unsafe block missing a safety comment
386+
--> $DIR/undocumented_unsafe_blocks.rs:564:5
387+
|
388+
LL | unsafe {}
389+
| ^^^^^^^^^
390+
|
391+
= help: consider adding a safety comment on the preceding line
392+
393+
error: aborting due to 45 previous errors
314394

tests/ui/undocumented_unsafe_blocks.rs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//@aux-build:proc_macro_unsafe.rs:proc-macro
22

33
#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)]
4-
#![allow(clippy::let_unit_value, clippy::missing_safety_doc)]
4+
#![allow(deref_nullptr, clippy::let_unit_value, clippy::missing_safety_doc)]
5+
#![feature(lint_reasons)]
56

67
extern crate proc_macro_unsafe;
78

@@ -490,7 +491,7 @@ unsafe impl CrateRoot for () {}
490491
// SAFETY: ok
491492
unsafe impl CrateRoot for (i32) {}
492493

493-
fn issue_9142() {
494+
fn nested_block_separation_issue_9142() {
494495
// SAFETY: ok
495496
let _ =
496497
// we need this comment to avoid rustfmt putting
@@ -517,18 +518,51 @@ pub const unsafe fn a_const_function_with_a_very_long_name_to_break_the_line() -
517518
2
518519
}
519520

520-
fn issue_10832() {
521-
// Safety: A safety comment. But it will warn anyways
521+
fn separate_line_from_let_issue_10832() {
522+
// SAFETY:
522523
let _some_variable_with_a_very_long_name_to_break_the_line =
523524
unsafe { a_function_with_a_very_long_name_to_break_the_line() };
524525

525-
// Safety: Another safety comment. But it will warn anyways
526+
// SAFETY:
526527
const _SOME_CONST_WITH_A_VERY_LONG_NAME_TO_BREAK_THE_LINE: u32 =
527528
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
528529

529-
// Safety: Yet another safety comment. But it will warn anyways
530+
// SAFETY:
530531
static _SOME_STATIC_WITH_A_VERY_LONG_NAME_TO_BREAK_THE_LINE: u32 =
531532
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
532533
}
533534

535+
fn above_expr_attribute_issue_8679<T: Copy>() {
536+
// SAFETY:
537+
#[allow(unsafe_code)]
538+
unsafe {}
539+
540+
// SAFETY:
541+
#[expect(unsafe_code, reason = "totally safe")]
542+
unsafe {
543+
*std::ptr::null::<T>()
544+
};
545+
546+
// Safety: A safety comment
547+
#[allow(unsafe_code)]
548+
let _some_variable_with_a_very_long_name_to_break_the_line =
549+
unsafe { a_function_with_a_very_long_name_to_break_the_line() };
550+
551+
// Safety: Another safety comment
552+
#[allow(unsafe_code)]
553+
const _SOME_CONST_WITH_A_VERY_LONG_NAME_TO_BREAK_THE_LINE: u32 =
554+
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
555+
556+
// Safety: Yet another safety comment with multiple attributes
557+
#[allow(unsafe_code)]
558+
#[allow(non_upper_case_globals)]
559+
static _some_static_with_a_very_long_name_to_break_the_line: u32 =
560+
unsafe { a_const_function_with_a_very_long_name_to_break_the_line() };
561+
562+
// SAFETY:
563+
#[allow(unsafe_code)]
564+
// This also works I guess
565+
unsafe {}
566+
}
567+
534568
fn main() {}

0 commit comments

Comments
 (0)