From 1b5e035827d41f1274ef05109d576210c0552064 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Tue, 10 Jan 2023 16:51:01 +0100 Subject: [PATCH 1/4] New lint: enum_must_use_added --- src/lints/enum_must_use_added.ron | 64 +++++++++++++++++++ src/query.rs | 1 + .../enum_must_use_added/new/Cargo.toml | 6 ++ .../enum_must_use_added/new/src/lib.rs | 62 ++++++++++++++++++ .../enum_must_use_added/old/Cargo.toml | 6 ++ .../enum_must_use_added/old/src/lib.rs | 51 +++++++++++++++ test_outputs/enum_must_use_added.output.ron | 26 ++++++++ 7 files changed, 216 insertions(+) create mode 100644 src/lints/enum_must_use_added.ron create mode 100644 test_crates/enum_must_use_added/new/Cargo.toml create mode 100644 test_crates/enum_must_use_added/new/src/lib.rs create mode 100644 test_crates/enum_must_use_added/old/Cargo.toml create mode 100644 test_crates/enum_must_use_added/old/src/lib.rs create mode 100644 test_outputs/enum_must_use_added.output.ron diff --git a/src/lints/enum_must_use_added.ron b/src/lints/enum_must_use_added.ron new file mode 100644 index 00000000..49a54063 --- /dev/null +++ b/src/lints/enum_must_use_added.ron @@ -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.", + per_result_error_template: Some("enum {{name}} in {{span_filename}}:{{span_begin_line}}"), +) diff --git a/src/query.rs b/src/query.rs index 9816aeee..1931eb5b 100644 --- a/src/query.rs +++ b/src/query.rs @@ -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, diff --git a/test_crates/enum_must_use_added/new/Cargo.toml b/test_crates/enum_must_use_added/new/Cargo.toml new file mode 100644 index 00000000..6a362d66 --- /dev/null +++ b/test_crates/enum_must_use_added/new/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enum_must_use_added" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/enum_must_use_added/new/src/lib.rs b/test_crates/enum_must_use_added/new/src/lib.rs new file mode 100644 index 00000000..48b99b76 --- /dev/null +++ b/test_crates/enum_must_use_added/new/src/lib.rs @@ -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, +} + + +// 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. +// It should NOT be reported by this rule because adding a new enum is not +// a breaking change. + +#[must_use] +pub enum MustUseNewEnum { + Bar, +} diff --git a/test_crates/enum_must_use_added/old/Cargo.toml b/test_crates/enum_must_use_added/old/Cargo.toml new file mode 100644 index 00000000..6a362d66 --- /dev/null +++ b/test_crates/enum_must_use_added/old/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "enum_must_use_added" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/enum_must_use_added/old/src/lib.rs b/test_crates/enum_must_use_added/old/src/lib.rs new file mode 100644 index 00000000..c27a2965 --- /dev/null +++ b/test_crates/enum_must_use_added/old/src/lib.rs @@ -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, +} diff --git a/test_outputs/enum_must_use_added.output.ron b/test_outputs/enum_must_use_added.output.ron new file mode 100644 index 00000000..5a0d3ab2 --- /dev/null +++ b/test_outputs/enum_must_use_added.output.ron @@ -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"), + }, + ], +} From ab5dd486908c0c17835b200367d3dddfc1855b6e Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk <92160712+SmolSir@users.noreply.github.com> Date: Tue, 10 Jan 2023 21:51:21 +0100 Subject: [PATCH 2/4] Update src/lints/enum_must_use_added.ron Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/lints/enum_must_use_added.ron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lints/enum_must_use_added.ron b/src/lints/enum_must_use_added.ron index 49a54063..8b98a96f 100644 --- a/src/lints/enum_must_use_added.ron +++ b/src/lints/enum_must_use_added.ron @@ -59,6 +59,6 @@ SemverQuery( "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.", + error_message: "An enum is now #[must_use]. Downstream crates that did not use its value will get a compiler lint.", per_result_error_template: Some("enum {{name}} in {{span_filename}}:{{span_begin_line}}"), ) From 855c75cca1d6c2b38239c28a119860c7b1906cd2 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk <92160712+SmolSir@users.noreply.github.com> Date: Tue, 10 Jan 2023 21:51:30 +0100 Subject: [PATCH 3/4] Update test_crates/enum_must_use_added/new/src/lib.rs Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- test_crates/enum_must_use_added/new/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_crates/enum_must_use_added/new/src/lib.rs b/test_crates/enum_must_use_added/new/src/lib.rs index 48b99b76..29d3467b 100644 --- a/test_crates/enum_must_use_added/new/src/lib.rs +++ b/test_crates/enum_must_use_added/new/src/lib.rs @@ -52,7 +52,7 @@ enum MustUsePrivateEnum { } -// This enum was added in the new version of the crate with it's attribute. +// This enum was added in the new version of the crate with its attribute. // It should NOT be reported by this rule because adding a new enum is not // a breaking change. From ab48204f25fcc9513cc06469b0e99bf8c227ae83 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk <92160712+SmolSir@users.noreply.github.com> Date: Tue, 10 Jan 2023 21:51:49 +0100 Subject: [PATCH 4/4] Update test_crates/enum_must_use_added/new/src/lib.rs Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- test_crates/enum_must_use_added/new/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_crates/enum_must_use_added/new/src/lib.rs b/test_crates/enum_must_use_added/new/src/lib.rs index 29d3467b..3423bf1b 100644 --- a/test_crates/enum_must_use_added/new/src/lib.rs +++ b/test_crates/enum_must_use_added/new/src/lib.rs @@ -53,8 +53,8 @@ enum MustUsePrivateEnum { // This enum was added in the new version of the crate with its attribute. -// It should NOT be reported by this rule because adding a new enum is not -// a breaking change. +// 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. #[must_use] pub enum MustUseNewEnum {