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

Adding #[must_use] on an item #268

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
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].
SmolSir marked this conversation as resolved.
Show resolved Hide resolved
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.",
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 @@ -436,4 +436,5 @@ add_lints!(
tuple_struct_to_plain_struct,
unit_struct_changed_kind,
variant_marked_non_exhaustive,
enum_must_use_added,
);
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]
43 changes: 43 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,43 @@
// 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,
}


// 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,
}
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]
44 changes: 44 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,44 @@
// 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,
}
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(9),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}