Skip to content

Commit 17c8bee

Browse files
committed
Add a couple of examples to undocumented_unsafe_blocks
1 parent 30b3336 commit 17c8bee

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ declare_clippy_lint! {
1515
/// explaining why the unsafe operations performed inside
1616
/// the block are safe.
1717
///
18+
/// Note the comment must appear on the line(s) preceding the unsafe block
19+
/// with nothing appearing in between. The following is ok:
20+
/// ```ignore
21+
/// foo(
22+
/// // SAFETY:
23+
/// // This is a valid safety comment
24+
/// unsafe { *x }
25+
/// )
26+
/// ```
27+
/// But neither of these are:
28+
/// ```ignore
29+
/// // SAFETY:
30+
/// // This is not a valid safety comment
31+
/// foo(
32+
/// /* SAFETY: Neither is this */ unsafe { *x },
33+
/// );
34+
/// ```
35+
///
1836
/// ### Why is this bad?
1937
/// Undocumented unsafe blocks can make it difficult to
2038
/// read and maintain code, as well as uncover unsoundness

tests/ui/undocumented_unsafe_blocks.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ fn from_proc_macro() {
251251

252252
// Invalid comments
253253

254+
#[rustfmt::skip]
255+
fn inline_block_comment() {
256+
/* Safety: */ unsafe {}
257+
}
258+
254259
fn no_comment() {
255260
unsafe {}
256261
}
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,110 @@
11
error: unsafe block missing a safety comment
2-
--> $DIR/undocumented_unsafe_blocks.rs:255:5
2+
--> $DIR/undocumented_unsafe_blocks.rs:256:19
3+
|
4+
LL | /* Safety: */ unsafe {}
5+
| ^^^^^^^^^
6+
|
7+
= note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings`
8+
= help: consider adding a safety comment on the preceding line
9+
10+
error: unsafe block missing a safety comment
11+
--> $DIR/undocumented_unsafe_blocks.rs:260:5
312
|
413
LL | unsafe {}
514
| ^^^^^^^^^
615
|
7-
= note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings`
816
= help: consider adding a safety comment on the preceding line
917

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

1826
error: unsafe block missing a safety comment
19-
--> $DIR/undocumented_unsafe_blocks.rs:259:29
27+
--> $DIR/undocumented_unsafe_blocks.rs:264:29
2028
|
2129
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
2230
| ^^^^^^^^^^^^^
2331
|
2432
= help: consider adding a safety comment on the preceding line
2533

2634
error: unsafe block missing a safety comment
27-
--> $DIR/undocumented_unsafe_blocks.rs:259:48
35+
--> $DIR/undocumented_unsafe_blocks.rs:264:48
2836
|
2937
LL | let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
3038
| ^^^^^^^^^^^^^
3139
|
3240
= help: consider adding a safety comment on the preceding line
3341

3442
error: unsafe block missing a safety comment
35-
--> $DIR/undocumented_unsafe_blocks.rs:263:18
43+
--> $DIR/undocumented_unsafe_blocks.rs:268:18
3644
|
3745
LL | let _ = (42, unsafe {}, "test", unsafe {});
3846
| ^^^^^^^^^
3947
|
4048
= help: consider adding a safety comment on the preceding line
4149

4250
error: unsafe block missing a safety comment
43-
--> $DIR/undocumented_unsafe_blocks.rs:263:37
51+
--> $DIR/undocumented_unsafe_blocks.rs:268:37
4452
|
4553
LL | let _ = (42, unsafe {}, "test", unsafe {});
4654
| ^^^^^^^^^
4755
|
4856
= help: consider adding a safety comment on the preceding line
4957

5058
error: unsafe block missing a safety comment
51-
--> $DIR/undocumented_unsafe_blocks.rs:267:14
59+
--> $DIR/undocumented_unsafe_blocks.rs:272:14
5260
|
5361
LL | let _ = *unsafe { &42 };
5462
| ^^^^^^^^^^^^^^
5563
|
5664
= help: consider adding a safety comment on the preceding line
5765

5866
error: unsafe block missing a safety comment
59-
--> $DIR/undocumented_unsafe_blocks.rs:272:19
67+
--> $DIR/undocumented_unsafe_blocks.rs:277:19
6068
|
6169
LL | let _ = match unsafe {} {
6270
| ^^^^^^^^^
6371
|
6472
= help: consider adding a safety comment on the preceding line
6573

6674
error: unsafe block missing a safety comment
67-
--> $DIR/undocumented_unsafe_blocks.rs:278:14
75+
--> $DIR/undocumented_unsafe_blocks.rs:283:14
6876
|
6977
LL | let _ = &unsafe {};
7078
| ^^^^^^^^^
7179
|
7280
= help: consider adding a safety comment on the preceding line
7381

7482
error: unsafe block missing a safety comment
75-
--> $DIR/undocumented_unsafe_blocks.rs:282:14
83+
--> $DIR/undocumented_unsafe_blocks.rs:287:14
7684
|
7785
LL | let _ = [unsafe {}; 5];
7886
| ^^^^^^^^^
7987
|
8088
= help: consider adding a safety comment on the preceding line
8189

8290
error: unsafe block missing a safety comment
83-
--> $DIR/undocumented_unsafe_blocks.rs:286:13
91+
--> $DIR/undocumented_unsafe_blocks.rs:291:13
8492
|
8593
LL | let _ = unsafe {};
8694
| ^^^^^^^^^
8795
|
8896
= help: consider adding a safety comment on the preceding line
8997

9098
error: unsafe block missing a safety comment
91-
--> $DIR/undocumented_unsafe_blocks.rs:296:8
99+
--> $DIR/undocumented_unsafe_blocks.rs:301:8
92100
|
93101
LL | t!(unsafe {});
94102
| ^^^^^^^^^
95103
|
96104
= help: consider adding a safety comment on the preceding line
97105

98106
error: unsafe block missing a safety comment
99-
--> $DIR/undocumented_unsafe_blocks.rs:302:13
107+
--> $DIR/undocumented_unsafe_blocks.rs:307:13
100108
|
101109
LL | unsafe {}
102110
| ^^^^^^^^^
@@ -108,36 +116,36 @@ LL | t!();
108116
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)
109117

110118
error: unsafe block missing a safety comment
111-
--> $DIR/undocumented_unsafe_blocks.rs:310:5
119+
--> $DIR/undocumented_unsafe_blocks.rs:315:5
112120
|
113121
LL | unsafe {} // SAFETY:
114122
| ^^^^^^^^^
115123
|
116124
= help: consider adding a safety comment on the preceding line
117125

118126
error: unsafe block missing a safety comment
119-
--> $DIR/undocumented_unsafe_blocks.rs:314:5
127+
--> $DIR/undocumented_unsafe_blocks.rs:319:5
120128
|
121129
LL | unsafe {
122130
| ^^^^^^^^
123131
|
124132
= help: consider adding a safety comment on the preceding line
125133

126134
error: unsafe block missing a safety comment
127-
--> $DIR/undocumented_unsafe_blocks.rs:324:5
135+
--> $DIR/undocumented_unsafe_blocks.rs:329:5
128136
|
129137
LL | unsafe {};
130138
| ^^^^^^^^^
131139
|
132140
= help: consider adding a safety comment on the preceding line
133141

134142
error: unsafe block missing a safety comment
135-
--> $DIR/undocumented_unsafe_blocks.rs:328:20
143+
--> $DIR/undocumented_unsafe_blocks.rs:333:20
136144
|
137145
LL | println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
138146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139147
|
140148
= help: consider adding a safety comment on the preceding line
141149

142-
error: aborting due to 17 previous errors
150+
error: aborting due to 18 previous errors
143151

0 commit comments

Comments
 (0)