-
-
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
Add lint for "deprecated" attribute on structs and enums #365
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a26518c
Add lint for "deprecated" attribute on structs
Daaiid 5bf166c
Generalize lint for "deprecated" struct attribute
Daaiid d618049
Rename struct_deprecated_added lint to its now more general use case
Daaiid a502c0d
Reduce comments, use more meaningful strings
Daaiid c4d5272
Add tests for deprecated enums
Daaiid a31e1b9
Add tests for deprecated unions
Daaiid cbc4aa6
Simplify deprecated lint name
Daaiid b967393
Reorganize deprecated tests
Daaiid cec04b9
Apply suggestions from code review
obi1kenobi 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: "struct_deprecated_added", | ||
human_readable_name: "struct #[deprecated] added", | ||
description: "A struct has been newly marked with #[deprecated].", | ||
required_update: Minor, | ||
reference_link: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
current { | ||
item { | ||
... on Struct { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @output | ||
|
||
importable_path { | ||
path @tag @output | ||
} | ||
|
||
attribute { | ||
new_attr: raw_attribute @output | ||
content { | ||
base @filter(op: "=", value: ["$deprecated"]) | ||
} | ||
} | ||
|
||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
baseline { | ||
item { | ||
... on Struct { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
|
||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
} | ||
|
||
attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { | ||
content { | ||
base @filter(op: "=", value: ["$deprecated"]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"deprecated": "deprecated", | ||
"zero": 0, | ||
}, | ||
error_message: "A struct is now #[deprecated]. Downstream crates will get a compiler warning.", | ||
per_result_error_template: Some("struct {{name}} in {{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 = "struct_deprecated_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,59 @@ | ||
// These structs did not have the #[deprecated] attribute in the old version. | ||
// Addition of the attribute should be reported by this rule. | ||
|
||
#[deprecated] | ||
pub struct StructToDeprecatedStruct { | ||
bar: u64, | ||
} | ||
|
||
#[deprecated = "Foo"] | ||
pub struct StructToDeprecatedMessageStruct { | ||
bar: u64, | ||
} | ||
|
||
// These structs had the #[deprecated] attribute in the old version. Changes of | ||
// the attribute, including deletion, should NOT be reported by this rule. | ||
|
||
pub struct DeprecatedStructToStruct { | ||
bar: u64, | ||
} | ||
|
||
#[deprecated = "Foo"] | ||
pub struct DeprecatedStructToDeprecatedMessageStruct { | ||
bar: u64, | ||
} | ||
|
||
// These structs had the #[deprecated] attribute in the old version. | ||
// They also included the user-defined warning message. Changes of | ||
// the attribute, including deletion, should NOT be reported by this rule. | ||
|
||
pub struct DeprecatedMessageStructToStruct { | ||
bar: u64, | ||
} | ||
|
||
#[deprecated] | ||
pub struct DeprecatedMessageStructToDeprecatedStruct { | ||
bar: u64, | ||
} | ||
|
||
#[deprecated = "Baz"] | ||
pub struct DeprecatedMessageStructToDeprecatedMessageStruct { | ||
bar: u64, | ||
} | ||
|
||
// This struct is private and should NOT be reported by this rule. | ||
|
||
#[deprecated] | ||
struct DeprecatedPrivateStruct { | ||
bar: u64, | ||
} | ||
|
||
// This struct was added in the new version of the crate with its attribute. | ||
// It should NOT be reported by this rule to avoid duplicate lints. | ||
// It should be reported as a new pub type that is part of the crate's API. | ||
// This might seem like a nonsensical test but is a valid edge case. | ||
|
||
#[deprecated] | ||
pub struct DeprecatedNewStruct { | ||
bar: u64, | ||
} |
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 = "struct_deprecated_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,48 @@ | ||
// These structs did not have the #[deprecated] attribute in the old version. | ||
// Addition of the attribute should be reported by this rule. | ||
|
||
pub struct StructToDeprecatedStruct { | ||
bar: u64, | ||
} | ||
|
||
pub struct StructToDeprecatedMessageStruct { | ||
bar: u64, | ||
} | ||
|
||
// These structs had the #[deprecated] attribute in the old version. Changes of | ||
// the attribute, including deletion, should NOT be reported by this rule. | ||
|
||
#[deprecated] | ||
pub struct DeprecatedStructToStruct { | ||
bar: u64, | ||
} | ||
|
||
#[deprecated] | ||
pub struct DeprecatedStructToDeprecatedMessageStruct { | ||
bar: u64, | ||
} | ||
|
||
// These structs had the #[deprecated] attribute in the old version. | ||
// They also included the user-defined warning message. Changes of | ||
// the attribute, including deletion, should NOT be reported by this rule. | ||
|
||
#[deprecated = "Foo"] | ||
pub struct DeprecatedMessageStructToStruct { | ||
bar: u64, | ||
} | ||
|
||
#[deprecated = "Foo"] | ||
pub struct DeprecatedMessageStructToDeprecatedStruct { | ||
bar: u64, | ||
} | ||
|
||
#[deprecated = "Foo"] | ||
pub struct DeprecatedMessageStructToDeprecatedMessageStruct { | ||
bar: u64, | ||
} | ||
|
||
// This struct is private and should NOT be reported by this rule. | ||
|
||
struct DeprecatedPrivateStruct { | ||
bar: u64, | ||
} |
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,26 @@ | ||
{ | ||
"./test_crates/struct_deprecated_added/": [ | ||
{ | ||
"name": String("StructToDeprecatedStruct"), | ||
"new_attr": String("#[deprecated]"), | ||
"path": List([ | ||
String("struct_deprecated_added"), | ||
String("StructToDeprecatedStruct"), | ||
]), | ||
"span_begin_line": Uint64(5), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"name": String("StructToDeprecatedMessageStruct"), | ||
"new_attr": String("#[deprecated = \"Foo\"]"), | ||
"path": List([ | ||
String("struct_deprecated_added"), | ||
String("StructToDeprecatedMessageStruct"), | ||
]), | ||
"span_begin_line": Uint64(10), | ||
"span_filename": String("src/lib.rs"), | ||
"visibility_limit": String("public"), | ||
}, | ||
] | ||
} |
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.
As is, this query is perfect and does what it sets out completely correctly.
There's an opportunity to make this same query apply not just for structs, but also for enums and (in the future, when we expose them in the query API) unions as well. To do that, instead of only selecting structs here, you could use
ImplOwner
, which is a common supertype ofstruct/enum/union
, as the items that are allowed to have an inherentimpl
block.Then we'd just need to make sure that the "current" and the "baseline" arms of the query are also talking about the same kind of item in addition to having the same
importable_path
value.You can see an example here: https://github.com/obi1kenobi/cargo-semver-checks/blob/main/src/lints/inherent_method_must_use_added.ron#L15-L18