-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new lint: trait_method_unsafe_removed (#612)
* Add new lint: trait_method_unsafe_removed * Create tests for lint trait_method_unsafe_removed * Implement lint trait_method_unsafe_removed
- Loading branch information
1 parent
27e3c9c
commit 58711ac
Showing
9 changed files
with
182 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
SemverQuery( | ||
id: "trait_method_unsafe_removed", | ||
human_readable_name: "pub trait method became safe", | ||
description: "A method in a public trait became safe", | ||
required_update: Major, | ||
reference_link: Some("https://doc.rust-lang.org/stable/reference/unsafe-keyword.html#unsafe-functions-unsafe-fn"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
baseline { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
importable_path { | ||
path @output @tag | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
method { | ||
unsafe @filter(op: "=", value: ["$true"]) | ||
public_api_eligible @filter(op: "=", value: ["$true"]) | ||
method_name: name @output @tag | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
method { | ||
unsafe @filter(op: "!=", value: ["$true"]) | ||
public_api_eligible @filter(op: "=", value: ["$true"]) | ||
name @filter(op: "=", value: ["%method_name"]) | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"true": true, | ||
"public": "public", | ||
}, | ||
error_message: "A publicly-visible trait method is no longer `unsafe`, so implementations must remove the `unsafe` qualifier.", | ||
per_result_error_template: Some("trait method <{{join \"::\" path}}>::{{method_name}} in file {{span_filename}}:{{span_begin_line}}"), | ||
) |
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,7 @@ | ||
[package] | ||
publish = false | ||
name = "trait_method_unsafe_removed" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,30 @@ | ||
// Method of public trait becomes safe, should get reported. | ||
pub trait PubTrait { | ||
fn becomes_safe(); | ||
fn stays_safe(); | ||
unsafe fn stays_unsafe(); | ||
} | ||
|
||
// Method of trait that was not publicly-visible becomes safe, shouldn't get | ||
// reported. | ||
pub trait TraitBecomesPublic { | ||
fn becomes_safe(); | ||
} | ||
|
||
// Method of trait that is publicly-visible becomes safe while trait becomes | ||
// private. The trait becoming private should be the only violation that's | ||
// reported. | ||
trait TraitBecomesPrivate { | ||
fn becomes_safe(); | ||
} | ||
|
||
// Method of trait becomes safe while trait also becomes safe. Method should get | ||
// reported along with trait. | ||
pub trait TraitBecomesSafe { | ||
fn becomes_safe(); | ||
} | ||
|
||
// Private trait's method becomes safe, shouldn't get reported. | ||
trait PrivateTrait { | ||
fn becomes_safe(); | ||
} |
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,7 @@ | ||
[package] | ||
publish = false | ||
name = "trait_method_unsafe_removed" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,30 @@ | ||
// Method of public trait becomes safe, should get reported. | ||
pub trait PubTrait { | ||
unsafe fn becomes_safe(); | ||
fn stays_safe(); | ||
unsafe fn stays_unsafe(); | ||
} | ||
|
||
// Method of trait that was not publicly-visible becomes safe, shouldn't get | ||
// reported. | ||
trait TraitBecomesPublic { | ||
unsafe fn becomes_safe(); | ||
} | ||
|
||
// Method of trait that is publicly-visible becomes safe while trait becomes | ||
// private. The trait becoming private should be the only violation that's | ||
// reported. | ||
pub trait TraitBecomesPrivate { | ||
unsafe fn becomes_safe(); | ||
} | ||
|
||
// Method of trait becomes safe while trait also becomes safe. Method should get | ||
// reported along with trait. | ||
pub unsafe trait TraitBecomesSafe { | ||
unsafe fn becomes_safe(); | ||
} | ||
|
||
// Private trait's method becomes safe, shouldn't get reported. | ||
trait PrivateTrait { | ||
unsafe fn becomes_safe(); | ||
} |
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,24 @@ | ||
{ | ||
"./test_crates/trait_method_unsafe_removed/": [ | ||
{ | ||
"method_name": String("becomes_safe"), | ||
"path": List([ | ||
String("trait_method_unsafe_removed"), | ||
String("PubTrait"), | ||
]), | ||
"span_begin_line": Uint64(3), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"method_name": String("becomes_safe"), | ||
"path": List([ | ||
String("trait_method_unsafe_removed"), | ||
String("TraitBecomesSafe"), | ||
]), | ||
"span_begin_line": Uint64(24), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
} | ||
], | ||
} |
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