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

New lint: Enum #[must_use] added #278

Merged
merged 4 commits into from
Jan 11, 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
64 changes: 64 additions & 0 deletions src/lints/enum_must_use_added.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
SemverQuery(
id: "enum_must_use_added",
human_readable_name: "enum #[must_use] added",
description: "An enum has been marked with #[must_use].",
required_update: Minor,

// TODO: Change the reference link to point to the cargo semver reference
// once it has a section on attribute #[must_use].
reference_link: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"),
query: r#"
{
CrateDiff {
baseline {
item {
... on Enum {
visibility_limit @filter(op: "=", value: ["$public"]) @output
name @tag @output

importable_path {
path @tag @output
}

attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
content {
base @filter(op: "=", value: ["$must_use"])
}
}
}
}
}
current {
item {
... on Enum {
visibility_limit @filter(op: "=", value: ["$public"])
name @filter(op: "=", value: ["%name"])

importable_path {
path @filter(op: "=", value: ["%path"])
}

attribute {
new_attr: raw_attribute @output
content {
base @filter(op: "=", value: ["$must_use"])
}
}

span_: span @optional {
filename @output
begin_line @output
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"must_use": "must_use",
"zero": 0,
},
error_message: "An enum has been marked with #[must_use]. This can cause downstream crates that did not use this enum's value to get a compiler lint.",
SmolSir marked this conversation as resolved.
Show resolved Hide resolved
per_result_error_template: Some("enum {{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 @@ -409,6 +409,7 @@ add_lints!(
derive_trait_impl_removed,
enum_marked_non_exhaustive,
enum_missing,
enum_must_use_added,
enum_repr_c_removed,
enum_repr_int_changed,
enum_repr_int_removed,
Expand Down
6 changes: 6 additions & 0 deletions test_crates/enum_must_use_added/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "enum_must_use_added"
version = "0.1.0"
edition = "2021"

[dependencies]
62 changes: 62 additions & 0 deletions test_crates/enum_must_use_added/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// These enums did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported by this rule.

#[must_use]
pub enum EnumToMustUseEnum {
Bar,
}

#[must_use = "Foo"]
pub enum EnumToMustUseMessageEnum {
Bar,
}


// These enums had the #[must_use] attribute in the old version. Changes of
// the attribute, including deletion, should NOT be reported by this rule.

pub enum MustUseEnumToEnum {
Bar,
}

#[must_use = "Foo"]
pub enum MustUseEnumToMustUseMessageEnum {
Bar,
}
SmolSir marked this conversation as resolved.
Show resolved Hide resolved


// These enums had the #[must_use] 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 enum MustUseMessageEnumToEnum {
Bar,
}

#[must_use]
pub enum MustUseMessageEnumToMustUseEnum {
Bar,
}

#[must_use = "Baz"]
pub enum MustUseMessageEnumToMustUseMessageEnum {
Bar,
}


// This enum is private and should NOT be reported by this rule.

#[must_use]
enum MustUsePrivateEnum {
Bar,
}


// This enum was added in the new version of the crate with it's attribute.
SmolSir marked this conversation as resolved.
Show resolved Hide resolved
// It should NOT be reported by this rule because adding a new enum is not
// a breaking change.
SmolSir marked this conversation as resolved.
Show resolved Hide resolved

#[must_use]
pub enum MustUseNewEnum {
Bar,
}
6 changes: 6 additions & 0 deletions test_crates/enum_must_use_added/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "enum_must_use_added"
version = "0.1.0"
edition = "2021"

[dependencies]
51 changes: 51 additions & 0 deletions test_crates/enum_must_use_added/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// These enums did not have the #[must_use] attribute in the old version.
// Addition of the attribute should be reported by this rule.

pub enum EnumToMustUseEnum {
Bar,
}

pub enum EnumToMustUseMessageEnum {
Bar,
}


// These enums had the #[must_use] attribute in the old version. Changes of
// the attribute, including deletion, should NOT be reported by this rule.

#[must_use]
pub enum MustUseEnumToEnum {
Bar,
}

#[must_use]
pub enum MustUseEnumToMustUseMessageEnum {
Bar,
}


// These enums had the #[must_use] 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.

#[must_use = "Foo"]
pub enum MustUseMessageEnumToEnum {
Bar,
}

#[must_use = "Foo"]
pub enum MustUseMessageEnumToMustUseEnum {
Bar,
}

#[must_use = "Foo"]
pub enum MustUseMessageEnumToMustUseMessageEnum {
Bar,
}


// This enum is private and should NOT be reported by this rule.

enum MustUsePrivateEnum {
Bar,
}
26 changes: 26 additions & 0 deletions test_outputs/enum_must_use_added.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"./test_crates/enum_must_use_added/": [
{
"name": String("EnumToMustUseEnum"),
"new_attr": String("#[must_use]"),
"path": List([
String("enum_must_use_added"),
String("EnumToMustUseEnum"),
]),
"span_begin_line": Uint64(5),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
{
"name": String("EnumToMustUseMessageEnum"),
"new_attr": String("#[must_use = \"Foo\"]"),
"path": List([
String("enum_must_use_added"),
String("EnumToMustUseMessageEnum"),
]),
"span_begin_line": Uint64(10),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}