-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
adds new trait-method-added lint #891
Merged
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,60 @@ | ||
SemverQuery( | ||
id: "trait_method_added", | ||
human_readable_name: "pub trait method added", | ||
description: "A non-sealed public trait added a new method without a default implementation", | ||
required_update: Major, | ||
lint_level: Deny, | ||
reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-item-no-default"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
current { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
name @output | ||
|
||
importable_path { | ||
path @output @tag | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
|
||
method { | ||
method_name: name @output @tag | ||
has_body @filter(op: "!=", value: ["$true"]) | ||
|
||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
baseline { | ||
item { | ||
... on Trait { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
sealed @filter(op: "!=", value: ["$true"]) | ||
|
||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
|
||
method @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { | ||
name @filter(op: "=", value: ["%method_name"]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"true": true, | ||
"public": "public", | ||
"zero": 0, | ||
}, | ||
error_message: "A non-sealed public trait added a new method without a default implementation, which breaks downstream implementations of the trait", | ||
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_added" | ||
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,49 @@ | ||
mod sealed { | ||
pub(crate) trait Sealed {} | ||
} | ||
|
||
// ---- Should be reported ---- | ||
pub trait WillGainMethodWithoutDefault { | ||
fn one_method(self); | ||
} | ||
|
||
pub trait WillGainAnotherMethodWithoutDefault { | ||
fn one_method(self); | ||
fn two_method(self); | ||
} | ||
|
||
pub trait WillGainMultipleMethodsWithoutDefault { | ||
fn one_method(self); | ||
fn two_method(self); | ||
} | ||
|
||
pub trait WillGainMethodWithoutDefaultAndSeal: sealed::Sealed { | ||
fn one_method(self); | ||
} | ||
|
||
pub trait WIllGainDocHiddenMethodWithoutDefault { | ||
#[doc(hidden)] | ||
fn one_method(self); | ||
} | ||
|
||
// ---- Should not be reported ---- | ||
pub trait WillGainMethodWithDefault { | ||
fn one_method(self) {} | ||
} | ||
|
||
pub trait WillGainAnotherMethodWithDefault { | ||
fn one_method(self); | ||
fn two_method(self) {} | ||
} | ||
|
||
pub trait WillGainMethodWithoutDefaultSealed: sealed::Sealed { | ||
fn one_method(self); | ||
} | ||
|
||
pub trait WillGainMethodWithoutDefaultAndLoseSeal { | ||
fn one_method(self); | ||
} | ||
|
||
pub trait WillKeepAMethodWithoutDefault { | ||
fn one_method(self); | ||
} |
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_added" | ||
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,31 @@ | ||
mod sealed { | ||
pub(crate) trait Sealed {} | ||
} | ||
|
||
// ---- Should be reported ---- | ||
pub trait WillGainMethodWithoutDefault {} | ||
|
||
pub trait WillGainAnotherMethodWithoutDefault { | ||
fn one_method(self); | ||
} | ||
|
||
pub trait WillGainMultipleMethodsWithoutDefault {} | ||
|
||
pub trait WillGainMethodWithoutDefaultAndSeal {} | ||
|
||
pub trait WIllGainDocHiddenMethodWithoutDefault {} | ||
|
||
// ---- Should not be reported ---- | ||
pub trait WillGainMethodWithDefault {} | ||
|
||
pub trait WillGainAnotherMethodWithDefault { | ||
fn one_method(self); | ||
} | ||
|
||
pub trait WillGainMethodWithoutDefaultSealed: sealed::Sealed {} | ||
|
||
pub trait WillGainMethodWithoutDefaultAndLoseSeal: sealed::Sealed {} | ||
|
||
pub trait WillKeepAMethodWithoutDefault { | ||
fn one_method(self); | ||
} |
Oops, something went wrong.
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 portion is correctly implemented, but would be lovely if we had a test case for one more situation that users might find confusing.
Users might think that marking the new method
#[doc(hidden)]
may resolve this lint. It shouldn't, and it'd be great to have a test for it.For bonus points, if you're interested, you could take a look at our other "trait item added without default" lints and make sure they all have a "
#[doc(hidden)]
on the new item" test case as well. If not, more PRs are welcome!