Skip to content

Commit efadb55

Browse files
committed
Auto merge of #9851 - Veykril:unnecessary-safety-comment, r=giraffate
Lint unnecessary safety comments changelog: [`unnecessary_safety_comment`]: Add unnecessary safety comment lint Addresses #7954 This does not necessarily catch all occurences, as doing so would require checking all expressions in the entire source which seems rather expensive. Instead what the lint does is it checks items, statements and the tail expression of blocks for safety comments, then checks if those comments are necessary or not, then linting for the unnecessary ones. I kept the tests in one file to check that the lints do not clash with each other.
2 parents 1a96571 + f96dd38 commit efadb55

8 files changed

+539
-105
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4451,6 +4451,7 @@ Released 2018-09-13
44514451
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
44524452
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
44534453
[`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings
4454+
[`unnecessary_safety_comment`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_comment
44544455
[`unnecessary_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_doc
44554456
[`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports
44564457
[`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
584584
crate::types::TYPE_COMPLEXITY_INFO,
585585
crate::types::VEC_BOX_INFO,
586586
crate::undocumented_unsafe_blocks::UNDOCUMENTED_UNSAFE_BLOCKS_INFO,
587+
crate::undocumented_unsafe_blocks::UNNECESSARY_SAFETY_COMMENT_INFO,
587588
crate::unicode::INVISIBLE_CHARACTERS_INFO,
588589
crate::unicode::NON_ASCII_LITERAL_INFO,
589590
crate::unicode::UNICODE_NOT_NFC_INFO,

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 332 additions & 97 deletions
Large diffs are not rendered by default.

clippy_utils/src/visitors.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,36 +170,36 @@ where
170170
cb: F,
171171
}
172172

173-
struct WithStmtGuarg<'a, F> {
173+
struct WithStmtGuard<'a, F> {
174174
val: &'a mut RetFinder<F>,
175175
prev_in_stmt: bool,
176176
}
177177

178178
impl<F> RetFinder<F> {
179-
fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuarg<'_, F> {
179+
fn inside_stmt(&mut self, in_stmt: bool) -> WithStmtGuard<'_, F> {
180180
let prev_in_stmt = std::mem::replace(&mut self.in_stmt, in_stmt);
181-
WithStmtGuarg {
181+
WithStmtGuard {
182182
val: self,
183183
prev_in_stmt,
184184
}
185185
}
186186
}
187187

188-
impl<F> std::ops::Deref for WithStmtGuarg<'_, F> {
188+
impl<F> std::ops::Deref for WithStmtGuard<'_, F> {
189189
type Target = RetFinder<F>;
190190

191191
fn deref(&self) -> &Self::Target {
192192
self.val
193193
}
194194
}
195195

196-
impl<F> std::ops::DerefMut for WithStmtGuarg<'_, F> {
196+
impl<F> std::ops::DerefMut for WithStmtGuard<'_, F> {
197197
fn deref_mut(&mut self) -> &mut Self::Target {
198198
self.val
199199
}
200200
}
201201

202-
impl<F> Drop for WithStmtGuarg<'_, F> {
202+
impl<F> Drop for WithStmtGuard<'_, F> {
203203
fn drop(&mut self) {
204204
self.val.in_stmt = self.prev_in_stmt;
205205
}

tests/ui/undocumented_unsafe_blocks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// aux-build:proc_macro_unsafe.rs
22

3-
#![warn(clippy::undocumented_unsafe_blocks)]
3+
#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)]
44
#![allow(clippy::let_unit_value, clippy::missing_safety_doc)]
55

66
extern crate proc_macro_unsafe;

tests/ui/undocumented_unsafe_blocks.stderr

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@ LL | unsafe impl TrailingComment for () {} // SAFETY:
239239
|
240240
= help: consider adding a safety comment on the preceding line
241241

242+
error: constant item has unnecessary safety comment
243+
--> $DIR/undocumented_unsafe_blocks.rs:471:5
244+
|
245+
LL | const BIG_NUMBER: i32 = 1000000;
246+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
247+
|
248+
help: consider removing the safety comment
249+
--> $DIR/undocumented_unsafe_blocks.rs:470:5
250+
|
251+
LL | // SAFETY:
252+
| ^^^^^^^^^^
253+
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings`
254+
242255
error: unsafe impl missing a safety comment
243256
--> $DIR/undocumented_unsafe_blocks.rs:472:5
244257
|
@@ -271,6 +284,24 @@ LL | unsafe {};
271284
|
272285
= help: consider adding a safety comment on the preceding line
273286

287+
error: statement has unnecessary safety comment
288+
--> $DIR/undocumented_unsafe_blocks.rs:501:5
289+
|
290+
LL | / let _ = {
291+
LL | | if unsafe { true } {
292+
LL | | todo!();
293+
LL | | } else {
294+
... |
295+
LL | | }
296+
LL | | };
297+
| |______^
298+
|
299+
help: consider removing the safety comment
300+
--> $DIR/undocumented_unsafe_blocks.rs:500:5
301+
|
302+
LL | // SAFETY: this is more than one level away, so it should warn
303+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304+
274305
error: unsafe block missing a safety comment
275306
--> $DIR/undocumented_unsafe_blocks.rs:502:12
276307
|
@@ -287,5 +318,5 @@ LL | let bar = unsafe {};
287318
|
288319
= help: consider adding a safety comment on the preceding line
289320

290-
error: aborting due to 34 previous errors
321+
error: aborting due to 36 previous errors
291322

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)]
2+
#![allow(clippy::let_unit_value, clippy::missing_safety_doc)]
3+
4+
mod unsafe_items_invalid_comment {
5+
// SAFETY:
6+
const CONST: u32 = 0;
7+
// SAFETY:
8+
static STATIC: u32 = 0;
9+
// SAFETY:
10+
struct Struct;
11+
// SAFETY:
12+
enum Enum {}
13+
// SAFETY:
14+
mod module {}
15+
}
16+
17+
mod unnecessary_from_macro {
18+
trait T {}
19+
20+
macro_rules! no_safety_comment {
21+
($t:ty) => {
22+
impl T for $t {}
23+
};
24+
}
25+
26+
// FIXME: This is not caught
27+
// Safety: unnecessary
28+
no_safety_comment!(());
29+
30+
macro_rules! with_safety_comment {
31+
($t:ty) => {
32+
// Safety: unnecessary
33+
impl T for $t {}
34+
};
35+
}
36+
37+
with_safety_comment!(i32);
38+
}
39+
40+
fn unnecessary_on_stmt_and_expr() -> u32 {
41+
// SAFETY: unnecessary
42+
let num = 42;
43+
44+
// SAFETY: unnecessary
45+
if num > 24 {}
46+
47+
// SAFETY: unnecessary
48+
24
49+
}
50+
51+
fn main() {}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
error: constant item has unnecessary safety comment
2+
--> $DIR/unnecessary_safety_comment.rs:6:5
3+
|
4+
LL | const CONST: u32 = 0;
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
help: consider removing the safety comment
8+
--> $DIR/unnecessary_safety_comment.rs:5:5
9+
|
10+
LL | // SAFETY:
11+
| ^^^^^^^^^^
12+
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings`
13+
14+
error: static item has unnecessary safety comment
15+
--> $DIR/unnecessary_safety_comment.rs:8:5
16+
|
17+
LL | static STATIC: u32 = 0;
18+
| ^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
help: consider removing the safety comment
21+
--> $DIR/unnecessary_safety_comment.rs:7:5
22+
|
23+
LL | // SAFETY:
24+
| ^^^^^^^^^^
25+
26+
error: struct has unnecessary safety comment
27+
--> $DIR/unnecessary_safety_comment.rs:10:5
28+
|
29+
LL | struct Struct;
30+
| ^^^^^^^^^^^^^^
31+
|
32+
help: consider removing the safety comment
33+
--> $DIR/unnecessary_safety_comment.rs:9:5
34+
|
35+
LL | // SAFETY:
36+
| ^^^^^^^^^^
37+
38+
error: enum has unnecessary safety comment
39+
--> $DIR/unnecessary_safety_comment.rs:12:5
40+
|
41+
LL | enum Enum {}
42+
| ^^^^^^^^^^^^
43+
|
44+
help: consider removing the safety comment
45+
--> $DIR/unnecessary_safety_comment.rs:11:5
46+
|
47+
LL | // SAFETY:
48+
| ^^^^^^^^^^
49+
50+
error: module has unnecessary safety comment
51+
--> $DIR/unnecessary_safety_comment.rs:14:5
52+
|
53+
LL | mod module {}
54+
| ^^^^^^^^^^^^^
55+
|
56+
help: consider removing the safety comment
57+
--> $DIR/unnecessary_safety_comment.rs:13:5
58+
|
59+
LL | // SAFETY:
60+
| ^^^^^^^^^^
61+
62+
error: impl has unnecessary safety comment
63+
--> $DIR/unnecessary_safety_comment.rs:33:13
64+
|
65+
LL | impl T for $t {}
66+
| ^^^^^^^^^^^^^^^^
67+
...
68+
LL | with_safety_comment!(i32);
69+
| ------------------------- in this macro invocation
70+
|
71+
help: consider removing the safety comment
72+
--> $DIR/unnecessary_safety_comment.rs:32:13
73+
|
74+
LL | // Safety: unnecessary
75+
| ^^^^^^^^^^^^^^^^^^^^^^
76+
= note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)
77+
78+
error: expression has unnecessary safety comment
79+
--> $DIR/unnecessary_safety_comment.rs:48:5
80+
|
81+
LL | 24
82+
| ^^
83+
|
84+
help: consider removing the safety comment
85+
--> $DIR/unnecessary_safety_comment.rs:47:5
86+
|
87+
LL | // SAFETY: unnecessary
88+
| ^^^^^^^^^^^^^^^^^^^^^^
89+
90+
error: statement has unnecessary safety comment
91+
--> $DIR/unnecessary_safety_comment.rs:42:5
92+
|
93+
LL | let num = 42;
94+
| ^^^^^^^^^^^^^
95+
|
96+
help: consider removing the safety comment
97+
--> $DIR/unnecessary_safety_comment.rs:41:5
98+
|
99+
LL | // SAFETY: unnecessary
100+
| ^^^^^^^^^^^^^^^^^^^^^^
101+
102+
error: statement has unnecessary safety comment
103+
--> $DIR/unnecessary_safety_comment.rs:45:5
104+
|
105+
LL | if num > 24 {}
106+
| ^^^^^^^^^^^^^^
107+
|
108+
help: consider removing the safety comment
109+
--> $DIR/unnecessary_safety_comment.rs:44:5
110+
|
111+
LL | // SAFETY: unnecessary
112+
| ^^^^^^^^^^^^^^^^^^^^^^
113+
114+
error: aborting due to 9 previous errors
115+

0 commit comments

Comments
 (0)