Skip to content
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 9 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/lints/struct_deprecated_added.ron
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 {
Copy link
Owner

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 of struct/enum/union, as the items that are allowed to have an inherent impl 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

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}}"),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,5 @@ add_lints!(
tuple_struct_to_plain_struct,
unit_struct_changed_kind,
variant_marked_non_exhaustive,
struct_deprecated_added,
);
7 changes: 7 additions & 0 deletions test_crates/struct_deprecated_added/new/Cargo.toml
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]
59 changes: 59 additions & 0 deletions test_crates/struct_deprecated_added/new/src/lib.rs
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,
}
7 changes: 7 additions & 0 deletions test_crates/struct_deprecated_added/old/Cargo.toml
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]
48 changes: 48 additions & 0 deletions test_crates/struct_deprecated_added/old/src/lib.rs
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,
}
26 changes: 26 additions & 0 deletions test_outputs/struct_deprecated_added.output.ron
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"),
},
]
}