-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement trait_newly_sealed new lint #876
Merged
obi1kenobi
merged 2 commits into
obi1kenobi:main
from
dmatos2012:add-trait-newly-sealed
Aug 20, 2024
+146
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,52 @@ | ||
SemverQuery( | ||
id: "trait_newly_sealed", | ||
human_readable_name: "pub trait became sealed", | ||
description: "A public trait became sealed, so users of this trait can not implement it anymore", | ||
required_update: Major, | ||
lint_level: Deny, | ||
reference_link: Some("https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
baseline { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
sealed @filter(op: "!=", value: ["$true"]) | ||
|
||
importable_path { | ||
path @output @tag | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
sealed @filter(op: "=", value: ["$true"]) | ||
name @output | ||
|
||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
|
||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"true": true, | ||
}, | ||
error_message: "A publicly-visible trait became sealed, so downstream crates are no longer able to implement it", | ||
per_result_error_template: Some("trait {{join \"::\" path}} 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_newly_sealed" | ||
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,24 @@ | ||
mod private { | ||
pub trait Sealed {} | ||
pub struct Token; | ||
} | ||
|
||
// Public trait becomes sealed, should be reported | ||
pub trait TraitBecomesSealed: private::Sealed {} | ||
|
||
// Trait that was not publicly-visible becomes sealed, shouldnt get reported as its still private | ||
trait TraitRemainsPrivateButSealed: private::Sealed {} | ||
|
||
// Method on public trait becomes sealed, should be reported | ||
pub trait TraitMethodBecomesSealed { | ||
fn method(&self, _: private::Token); | ||
} | ||
|
||
// Trait that was not publicly-visible becomes public and sealed, shouldnt be | ||
// reported | ||
pub trait TraitBecomesPublicAndSealed: private::Sealed {} | ||
|
||
// Trait becomes private and sealed. The fact that it's private dominates, | ||
// and should be the only violation that's reported: | ||
// Thus being newly sealed is not the main problem | ||
trait TraitBecomesPrivateAndSealed: private::Sealed {} |
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_newly_sealed" | ||
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,18 @@ | ||
// Public trait becomes sealed, should be reported | ||
pub trait TraitBecomesSealed {} | ||
|
||
// Trait that was not publicly-visible becomes sealed, shouldnt get reported as its still private | ||
trait TraitRemainsPrivateButSealed {} | ||
|
||
// Method on public trait becomes sealed, should be reported | ||
pub trait TraitMethodBecomesSealed { | ||
fn method(&self); | ||
} | ||
// Trait that was not publicly-visible becomes public and sealed, shouldnt be | ||
// reported | ||
trait TraitBecomesPublicAndSealed {} | ||
|
||
// Trait becomes private and sealed. The fact that it's private dominates, | ||
// and should be the only violation that's reported: | ||
// Thus being newly sealed is not the main problem | ||
pub trait TraitBecomesPrivateAndSealed {} |
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,25 @@ | ||
{ | ||
"./test_crates/trait_newly_sealed/": [ | ||
{ | ||
"name": String("TraitBecomesSealed"), | ||
"path": List([ | ||
String("trait_newly_sealed"), | ||
String("TraitBecomesSealed"), | ||
]), | ||
"span_begin_line": Uint64(7), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"name": String("TraitMethodBecomesSealed"), | ||
"path": List([ | ||
String("trait_newly_sealed"), | ||
String("TraitMethodBecomesSealed"), | ||
]), | ||
"span_begin_line": Uint64(13), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
], | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great reference link!