-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
8 changed files
with
539 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)] | ||
#![allow(clippy::let_unit_value, clippy::missing_safety_doc)] | ||
|
||
mod unsafe_items_invalid_comment { | ||
// SAFETY: | ||
const CONST: u32 = 0; | ||
// SAFETY: | ||
static STATIC: u32 = 0; | ||
// SAFETY: | ||
struct Struct; | ||
// SAFETY: | ||
enum Enum {} | ||
// SAFETY: | ||
mod module {} | ||
} | ||
|
||
mod unnecessary_from_macro { | ||
trait T {} | ||
|
||
macro_rules! no_safety_comment { | ||
($t:ty) => { | ||
impl T for $t {} | ||
}; | ||
} | ||
|
||
// FIXME: This is not caught | ||
// Safety: unnecessary | ||
no_safety_comment!(()); | ||
|
||
macro_rules! with_safety_comment { | ||
($t:ty) => { | ||
// Safety: unnecessary | ||
impl T for $t {} | ||
}; | ||
} | ||
|
||
with_safety_comment!(i32); | ||
} | ||
|
||
fn unnecessary_on_stmt_and_expr() -> u32 { | ||
// SAFETY: unnecessary | ||
let num = 42; | ||
|
||
// SAFETY: unnecessary | ||
if num > 24 {} | ||
|
||
// SAFETY: unnecessary | ||
24 | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
error: constant item has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:6:5 | ||
| | ||
LL | const CONST: u32 = 0; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:5:5 | ||
| | ||
LL | // SAFETY: | ||
| ^^^^^^^^^^ | ||
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings` | ||
|
||
error: static item has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:8:5 | ||
| | ||
LL | static STATIC: u32 = 0; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:7:5 | ||
| | ||
LL | // SAFETY: | ||
| ^^^^^^^^^^ | ||
|
||
error: struct has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:10:5 | ||
| | ||
LL | struct Struct; | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:9:5 | ||
| | ||
LL | // SAFETY: | ||
| ^^^^^^^^^^ | ||
|
||
error: enum has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:12:5 | ||
| | ||
LL | enum Enum {} | ||
| ^^^^^^^^^^^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:11:5 | ||
| | ||
LL | // SAFETY: | ||
| ^^^^^^^^^^ | ||
|
||
error: module has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:14:5 | ||
| | ||
LL | mod module {} | ||
| ^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:13:5 | ||
| | ||
LL | // SAFETY: | ||
| ^^^^^^^^^^ | ||
|
||
error: impl has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:33:13 | ||
| | ||
LL | impl T for $t {} | ||
| ^^^^^^^^^^^^^^^^ | ||
... | ||
LL | with_safety_comment!(i32); | ||
| ------------------------- in this macro invocation | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:32:13 | ||
| | ||
LL | // Safety: unnecessary | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: expression has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:48:5 | ||
| | ||
LL | 24 | ||
| ^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:47:5 | ||
| | ||
LL | // SAFETY: unnecessary | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: statement has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:42:5 | ||
| | ||
LL | let num = 42; | ||
| ^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:41:5 | ||
| | ||
LL | // SAFETY: unnecessary | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: statement has unnecessary safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:45:5 | ||
| | ||
LL | if num > 24 {} | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
help: consider removing the safety comment | ||
--> $DIR/unnecessary_safety_comment.rs:44:5 | ||
| | ||
LL | // SAFETY: unnecessary | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 9 previous errors | ||
|