From 7914906ba72fb019e74d4d111273d065e9797eae Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Tue, 10 Jan 2023 21:47:59 +0100 Subject: [PATCH 01/11] New lint: inherent_method_must_use_added --- src/lints/inherent_method_must_use_added.ron | 78 +++++++++++++++++++ src/query.rs | 1 + .../new/Cargo.toml | 6 ++ .../new/src/lib.rs | 74 ++++++++++++++++++ .../old/Cargo.toml | 6 ++ .../old/src/lib.rs | 58 ++++++++++++++ .../inherent_method_must_use_added.output.ron | 30 +++++++ 7 files changed, 253 insertions(+) create mode 100644 src/lints/inherent_method_must_use_added.ron create mode 100644 test_crates/inherent_method_must_use_added/new/Cargo.toml create mode 100644 test_crates/inherent_method_must_use_added/new/src/lib.rs create mode 100644 test_crates/inherent_method_must_use_added/old/Cargo.toml create mode 100644 test_crates/inherent_method_must_use_added/old/src/lib.rs create mode 100644 test_outputs/inherent_method_must_use_added.output.ron diff --git a/src/lints/inherent_method_must_use_added.ron b/src/lints/inherent_method_must_use_added.ron new file mode 100644 index 00000000..84549831 --- /dev/null +++ b/src/lints/inherent_method_must_use_added.ron @@ -0,0 +1,78 @@ +SemverQuery( + id: "inherent_method_must_use_added", + human_readable_name: "inherent method #[must_use] added", + description: "An inherent method 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 ImplOwner { + visibility_limit @filter(op: "=", value: ["$public"]) @output + name @tag @output + + importable_path { + path @tag @output + } + + inherent_impl { + method { + method_visibility: visibility_limit @filter(op: "=", value: ["$public"]) @output + method_name: name @tag @output + + attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { + content { + base @filter(op: "=", value: ["$must_use"]) + } + } + } + } + } + } + } + current { + item { + ... on ImplOwner { + visibility_limit @filter(op: "=", value: ["$public"]) + name @filter(op: "=", value: ["%name"]) + + importable_path { + path @filter(op: "=", value: ["%path"]) + } + + inherent_impl { + method { + visibility_limit @filter(op: "=", value: ["$public"]) + name @filter(op: "=", value: ["%method_name"]) + + 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 inherent method is now #[must_use]. Downstream crates that did not use its return value will get a compiler lint.", + per_result_error_template: Some("inherent method {{join \"::\" path}}::{{method_name}} in {{span_filename}}:{{span_begin_line}}"), +) diff --git a/src/query.rs b/src/query.rs index 9816aeee..4c9adda2 100644 --- a/src/query.rs +++ b/src/query.rs @@ -422,6 +422,7 @@ add_lints!( function_unsafe_added, inherent_method_const_removed, inherent_method_missing, + inherent_method_must_use_added inherent_method_unsafe_added, method_parameter_count_changed, sized_impl_removed, diff --git a/test_crates/inherent_method_must_use_added/new/Cargo.toml b/test_crates/inherent_method_must_use_added/new/Cargo.toml new file mode 100644 index 00000000..6c98db59 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/new/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "inherent_method_must_use_added" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/inherent_method_must_use_added/new/src/lib.rs b/test_crates/inherent_method_must_use_added/new/src/lib.rs new file mode 100644 index 00000000..9d004790 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/new/src/lib.rs @@ -0,0 +1,74 @@ +pub struct StructWithMustUseMethods {} + +impl StructWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes of + // the attribute, including deletion, should not be reported. + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public struct's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub struct StructWithPrivateMustUseMethods {} + +impl StructWithPrivateMustUseMethods { + + #[must_use] + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This struct is private and adding #[must_use] to its inherent methods +// should NOT be reported. + +struct PrivateStructWithMustUseMethods {} + +impl PrivateStructWithMustUseMethods { + + #[must_use] + fn PrivateStructMethodToPrivateStructMustUseMethod(&self) {} +} + + +// This struct and its inherent method were added in the new version of the +// crate, together with the method's 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. + +pub struct NewStructWithMustUseMethods {} + +impl NewStructWithMustUseMethods { + + #[must_use] + pub fn NewStructMustUseMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/old/Cargo.toml b/test_crates/inherent_method_must_use_added/old/Cargo.toml new file mode 100644 index 00000000..6c98db59 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/old/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "inherent_method_must_use_added" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/inherent_method_must_use_added/old/src/lib.rs b/test_crates/inherent_method_must_use_added/old/src/lib.rs new file mode 100644 index 00000000..27757717 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/old/src/lib.rs @@ -0,0 +1,58 @@ +pub struct StructWithMustUseMethods {} + +impl StructWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes + // of the attribute, including deletion, should not be reported. + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public struct's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub struct StructWithPrivateMustUseMethods {} + +impl StructWithPrivateMustUseMethods { + + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This struct is private and adding #[must_use] to its inherent methods +// should NOT be reported. + +struct PrivateStructWithMustUseMethods {} + +impl PrivateStructWithMustUseMethods { + + fn PrivateStructMethodToPrivateStructMustUseMethod(&self) {} +} diff --git a/test_outputs/inherent_method_must_use_added.output.ron b/test_outputs/inherent_method_must_use_added.output.ron new file mode 100644 index 00000000..86931109 --- /dev/null +++ b/test_outputs/inherent_method_must_use_added.output.ron @@ -0,0 +1,30 @@ +{ + "./test_crates/inherent_method_must_use_added/": [ + { + "method_name": String("MethodToMustUseMethod"), + "method_visibility": String("public"), + "name": String("StructWithMustUseMethods"), + "new_attr": String("#[must_use]"), + "path": List([ + String("inherent_method_must_use_added"), + String("StructWithMustUseMethods"), + ]), + "span_begin_line": Uint64(9), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMustUseMessageMethod"), + "method_visibility": String("public"), + "name": String("StructWithMustUseMethods"), + "new_attr": String("#[must_use = \"Foo\"]"), + "path": List([ + String("inherent_method_must_use_added"), + String("StructWithMustUseMethods"), + ]), + "span_begin_line": Uint64(12), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + ], +} From 7d5a7316b12d6a687c75d8090709ec9287328379 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Tue, 10 Jan 2023 21:55:37 +0100 Subject: [PATCH 02/11] Self-reviewed some comments. --- test_crates/inherent_method_must_use_added/new/src/lib.rs | 2 +- test_crates/inherent_method_must_use_added/old/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test_crates/inherent_method_must_use_added/new/src/lib.rs b/test_crates/inherent_method_must_use_added/new/src/lib.rs index 9d004790..cad907de 100644 --- a/test_crates/inherent_method_must_use_added/new/src/lib.rs +++ b/test_crates/inherent_method_must_use_added/new/src/lib.rs @@ -48,7 +48,7 @@ impl StructWithPrivateMustUseMethods { } -// This struct is private and adding #[must_use] to its inherent methods +// This struct is private and adding #[must_use] to its inherent method // should NOT be reported. struct PrivateStructWithMustUseMethods {} diff --git a/test_crates/inherent_method_must_use_added/old/src/lib.rs b/test_crates/inherent_method_must_use_added/old/src/lib.rs index 27757717..c0bd7dd9 100644 --- a/test_crates/inherent_method_must_use_added/old/src/lib.rs +++ b/test_crates/inherent_method_must_use_added/old/src/lib.rs @@ -47,7 +47,7 @@ impl StructWithPrivateMustUseMethods { } -// This struct is private and adding #[must_use] to its inherent methods +// This struct is private and adding #[must_use] to its inherent method // should NOT be reported. struct PrivateStructWithMustUseMethods {} From b96cb6e664fbe4ff7745c2db6761d7a14e4c54ce Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Tue, 10 Jan 2023 21:57:46 +0100 Subject: [PATCH 03/11] Missing comma in query.rs --- src/query.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query.rs b/src/query.rs index 4c9adda2..95ffeb04 100644 --- a/src/query.rs +++ b/src/query.rs @@ -422,7 +422,7 @@ add_lints!( function_unsafe_added, inherent_method_const_removed, inherent_method_missing, - inherent_method_must_use_added + inherent_method_must_use_added, inherent_method_unsafe_added, method_parameter_count_changed, sized_impl_removed, From fbf3a8c00306db0fcd97598a5753c346f08e543f Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Sat, 14 Jan 2023 16:24:58 +0100 Subject: [PATCH 04/11] Additional tests for enum, union & item type changed. --- .../enum_inherent_method_must_use_added.rs | 82 +++++++++ ..._changed_inherent_method_must_use_added.rs | 163 ++++++++++++++++++ .../new/src/lib.rs | 76 +------- .../struct_inherent_method_must_use_added.rs | 74 ++++++++ .../union_inherent_method_must_use_added.rs | 82 +++++++++ .../enum_inherent_method_must_use_added.rs | 64 +++++++ ..._changed_inherent_method_must_use_added.rs | 163 ++++++++++++++++++ .../old/src/lib.rs | 60 +------ .../struct_inherent_method_must_use_added.rs | 58 +++++++ .../union_inherent_method_must_use_added.rs | 64 +++++++ 10 files changed, 762 insertions(+), 124 deletions(-) create mode 100644 test_crates/inherent_method_must_use_added/new/src/enum_inherent_method_must_use_added.rs create mode 100644 test_crates/inherent_method_must_use_added/new/src/item_type_changed_inherent_method_must_use_added.rs create mode 100644 test_crates/inherent_method_must_use_added/new/src/struct_inherent_method_must_use_added.rs create mode 100644 test_crates/inherent_method_must_use_added/new/src/union_inherent_method_must_use_added.rs create mode 100644 test_crates/inherent_method_must_use_added/old/src/enum_inherent_method_must_use_added.rs create mode 100644 test_crates/inherent_method_must_use_added/old/src/item_type_changed_inherent_method_must_use_added.rs create mode 100644 test_crates/inherent_method_must_use_added/old/src/struct_inherent_method_must_use_added.rs create mode 100644 test_crates/inherent_method_must_use_added/old/src/union_inherent_method_must_use_added.rs diff --git a/test_crates/inherent_method_must_use_added/new/src/enum_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/new/src/enum_inherent_method_must_use_added.rs new file mode 100644 index 00000000..d1f87bd2 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/new/src/enum_inherent_method_must_use_added.rs @@ -0,0 +1,82 @@ +pub enum EnumWithMustUseMethods { + Bar, +} + +impl EnumWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes of + // the attribute, including deletion, should not be reported. + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public enum's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub enum EnumWithPrivateMustUseMethods { + Bar, +} + +impl EnumWithPrivateMustUseMethods { + + #[must_use] + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This enum is private and adding #[must_use] to its inherent method +// should NOT be reported. + +enum PrivateEnumWithMustUseMethods { + Bar, +} + +impl PrivateEnumWithMustUseMethods { + + #[must_use] + fn PrivateEnumMethodToPrivateEnumMustUseMethod(&self) {} +} + + +// This enum and its inherent method were added in the new version of the +// crate, together with the method's 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. + +pub enum NewEnumWithMustUseMethods { + Bar, +} + +impl NewEnumWithMustUseMethods { + + #[must_use] + pub fn NewEnumMustUseMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/new/src/item_type_changed_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/new/src/item_type_changed_inherent_method_must_use_added.rs new file mode 100644 index 00000000..112ab098 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/new/src/item_type_changed_inherent_method_must_use_added.rs @@ -0,0 +1,163 @@ +// This test file pair contains test cases against items changing the type +// between the old and new versions while their names and inherent methods +// remain unchanged. +// This can result in false-positives by reporting an addition of #[must_use] +// to an inherent method while the ImplOwner of the method has changed, making +// the new version method no more related to the old version method. + +pub struct EnumToStructWithMustUseMethods {} + +impl EnumToStructWithMustUseMethods { + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub union EnumToUnionWithMustUseMethods { + bar: usize, +} + +impl EnumToUnionWithMustUseMethods { + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub enum StructToEnumWithMustUseMethods { + Bar, +} + +impl StructToEnumWithMustUseMethods { + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub union StructToUnionWithMustUseMethods { + bar: usize, +} + +impl StructToUnionWithMustUseMethods { + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub enum UnionToEnumWithMustUseMethods { + Bar, +} + +impl UnionToEnumWithMustUseMethods { + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub struct UnionToStructWithMustUseMethods {} + +impl UnionToStructWithMustUseMethods { + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/new/src/lib.rs b/test_crates/inherent_method_must_use_added/new/src/lib.rs index cad907de..0a151612 100644 --- a/test_crates/inherent_method_must_use_added/new/src/lib.rs +++ b/test_crates/inherent_method_must_use_added/new/src/lib.rs @@ -1,74 +1,10 @@ -pub struct StructWithMustUseMethods {} +// This crate's test cases were separated into smaller files for easier +// management and debugging. -impl StructWithMustUseMethods { +pub mod enum_inherent_method_must_use_added; - // These methods did not have the #[must_use] attribute in the old version. - // Addition of the attribute should be reported. +pub mod struct_inherent_method_must_use_added; - #[must_use] - pub fn MethodToMustUseMethod(&self) {} +pub mod union_inherent_method_must_use_added; - #[must_use = "Foo"] - pub fn MethodToMustUseMessageMethod(&self) {} - - - // These methods had the #[must_use] attribute in the old version. Changes of - // the attribute, including deletion, should not be reported. - - pub fn MustUseMethodToMethod(&self) {} - - #[must_use = "Foo"] - pub fn MustUseMethodToMustUseMessageMethod(&self) {} - - - // These methods 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. - - pub fn MustUseMessageMethodToMethod(&self) {} - - #[must_use] - pub fn MustUseMessageMethodToMustUseMethod(&self) {} - - #[must_use = "Baz"] - pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} -} - - -// This public struct's inherent method did not have the #[must_use] attribute -// in the old version. Because the method is private, adding the attribute -// should NOT be reported. - -pub struct StructWithPrivateMustUseMethods {} - -impl StructWithPrivateMustUseMethods { - - #[must_use] - fn PrivateMethodToPrivateMustUseMethod(&self) {} -} - - -// This struct is private and adding #[must_use] to its inherent method -// should NOT be reported. - -struct PrivateStructWithMustUseMethods {} - -impl PrivateStructWithMustUseMethods { - - #[must_use] - fn PrivateStructMethodToPrivateStructMustUseMethod(&self) {} -} - - -// This struct and its inherent method were added in the new version of the -// crate, together with the method's 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. - -pub struct NewStructWithMustUseMethods {} - -impl NewStructWithMustUseMethods { - - #[must_use] - pub fn NewStructMustUseMethod(&self) {} -} +pub mod item_type_changed_inherent_method_must_use_added; diff --git a/test_crates/inherent_method_must_use_added/new/src/struct_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/new/src/struct_inherent_method_must_use_added.rs new file mode 100644 index 00000000..cad907de --- /dev/null +++ b/test_crates/inherent_method_must_use_added/new/src/struct_inherent_method_must_use_added.rs @@ -0,0 +1,74 @@ +pub struct StructWithMustUseMethods {} + +impl StructWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes of + // the attribute, including deletion, should not be reported. + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public struct's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub struct StructWithPrivateMustUseMethods {} + +impl StructWithPrivateMustUseMethods { + + #[must_use] + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This struct is private and adding #[must_use] to its inherent method +// should NOT be reported. + +struct PrivateStructWithMustUseMethods {} + +impl PrivateStructWithMustUseMethods { + + #[must_use] + fn PrivateStructMethodToPrivateStructMustUseMethod(&self) {} +} + + +// This struct and its inherent method were added in the new version of the +// crate, together with the method's 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. + +pub struct NewStructWithMustUseMethods {} + +impl NewStructWithMustUseMethods { + + #[must_use] + pub fn NewStructMustUseMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/new/src/union_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/new/src/union_inherent_method_must_use_added.rs new file mode 100644 index 00000000..94083ac0 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/new/src/union_inherent_method_must_use_added.rs @@ -0,0 +1,82 @@ +pub union UnionWithMustUseMethods { + bar: usize, +} + +impl UnionWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + #[must_use] + pub fn MethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes of + // the attribute, including deletion, should not be reported. + + pub fn MustUseMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Baz"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public union's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub union UnionWithPrivateMustUseMethods { + bar: usize, +} + +impl UnionWithPrivateMustUseMethods { + + #[must_use] + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This union is private and adding #[must_use] to its inherent method +// should NOT be reported. + +union PrivateUnionWithMustUseMethods { + bar: usize, +} + +impl PrivateUnionWithMustUseMethods { + + #[must_use] + fn PrivateUnionMethodToPrivateUnionMustUseMethod(&self) {} +} + + +// This union and its inherent method were added in the new version of the +// crate, together with the method's 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. + +pub union NewUnionWithMustUseMethods { + bar: usize, +} + +impl NewUnionWithMustUseMethods { + + #[must_use] + pub fn NewUnionMustUseMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/old/src/enum_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/old/src/enum_inherent_method_must_use_added.rs new file mode 100644 index 00000000..64c7386f --- /dev/null +++ b/test_crates/inherent_method_must_use_added/old/src/enum_inherent_method_must_use_added.rs @@ -0,0 +1,64 @@ +pub enum EnumWithMustUseMethods { + Bar, +} + +impl EnumWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes + // of the attribute, including deletion, should not be reported. + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public enum's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub enum EnumWithPrivateMustUseMethods { + Bar, +} + +impl EnumWithPrivateMustUseMethods { + + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This enum is private and adding #[must_use] to its inherent method +// should NOT be reported. + +enum PrivateEnumWithMustUseMethods { + Bar, +} + +impl PrivateEnumWithMustUseMethods { + + fn PrivateEnumMethodToPrivateEnumMustUseMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/old/src/item_type_changed_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/old/src/item_type_changed_inherent_method_must_use_added.rs new file mode 100644 index 00000000..6122d9ec --- /dev/null +++ b/test_crates/inherent_method_must_use_added/old/src/item_type_changed_inherent_method_must_use_added.rs @@ -0,0 +1,163 @@ +// This test file pair contains test cases against items changing the type +// between the old and new versions while their names and inherent methods +// remain unchanged. +// This can result in false-positives by reporting an addition of #[must_use] +// to an inherent method while the ImplOwner of the method has changed, making +// the new version method no more related to the old version method. + +pub enum EnumToStructWithMustUseMethods { + Bar, +} + +impl EnumToStructWithMustUseMethods { + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub enum EnumToUnionWithMustUseMethods { + Bar, +} + +impl EnumToUnionWithMustUseMethods { + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub struct StructToEnumWithMustUseMethods {} + +impl StructToEnumWithMustUseMethods { + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub struct StructToUnionWithMustUseMethods {} + +impl StructToUnionWithMustUseMethods { + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub union UnionToEnumWithMustUseMethods { + bar: usize, +} + +impl UnionToEnumWithMustUseMethods { + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +pub union UnionToStructWithMustUseMethods { + bar: usize, +} + +impl UnionToStructWithMustUseMethods { + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/old/src/lib.rs b/test_crates/inherent_method_must_use_added/old/src/lib.rs index c0bd7dd9..0a151612 100644 --- a/test_crates/inherent_method_must_use_added/old/src/lib.rs +++ b/test_crates/inherent_method_must_use_added/old/src/lib.rs @@ -1,58 +1,10 @@ -pub struct StructWithMustUseMethods {} +// This crate's test cases were separated into smaller files for easier +// management and debugging. -impl StructWithMustUseMethods { +pub mod enum_inherent_method_must_use_added; - // These methods did not have the #[must_use] attribute in the old version. - // Addition of the attribute should be reported. +pub mod struct_inherent_method_must_use_added; - pub fn MethodToMustUseMethod(&self) {} +pub mod union_inherent_method_must_use_added; - pub fn MethodToMustUseMessageMethod(&self) {} - - - // These methods had the #[must_use] attribute in the old version. Changes - // of the attribute, including deletion, should not be reported. - - #[must_use] - pub fn MustUseMethodToMethod(&self) {} - - #[must_use] - pub fn MustUseMethodToMustUseMessageMethod(&self) {} - - - // These methods 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. - - #[must_use = "Foo"] - pub fn MustUseMessageMethodToMethod(&self) {} - - #[must_use = "Foo"] - pub fn MustUseMessageMethodToMustUseMethod(&self) {} - - #[must_use = "Foo"] - pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} -} - - -// This public struct's inherent method did not have the #[must_use] attribute -// in the old version. Because the method is private, adding the attribute -// should NOT be reported. - -pub struct StructWithPrivateMustUseMethods {} - -impl StructWithPrivateMustUseMethods { - - fn PrivateMethodToPrivateMustUseMethod(&self) {} -} - - -// This struct is private and adding #[must_use] to its inherent method -// should NOT be reported. - -struct PrivateStructWithMustUseMethods {} - -impl PrivateStructWithMustUseMethods { - - fn PrivateStructMethodToPrivateStructMustUseMethod(&self) {} -} +pub mod item_type_changed_inherent_method_must_use_added; diff --git a/test_crates/inherent_method_must_use_added/old/src/struct_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/old/src/struct_inherent_method_must_use_added.rs new file mode 100644 index 00000000..c0bd7dd9 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/old/src/struct_inherent_method_must_use_added.rs @@ -0,0 +1,58 @@ +pub struct StructWithMustUseMethods {} + +impl StructWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes + // of the attribute, including deletion, should not be reported. + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public struct's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub struct StructWithPrivateMustUseMethods {} + +impl StructWithPrivateMustUseMethods { + + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This struct is private and adding #[must_use] to its inherent method +// should NOT be reported. + +struct PrivateStructWithMustUseMethods {} + +impl PrivateStructWithMustUseMethods { + + fn PrivateStructMethodToPrivateStructMustUseMethod(&self) {} +} diff --git a/test_crates/inherent_method_must_use_added/old/src/union_inherent_method_must_use_added.rs b/test_crates/inherent_method_must_use_added/old/src/union_inherent_method_must_use_added.rs new file mode 100644 index 00000000..987c7514 --- /dev/null +++ b/test_crates/inherent_method_must_use_added/old/src/union_inherent_method_must_use_added.rs @@ -0,0 +1,64 @@ +pub union UnionWithMustUseMethods { + bar: usize, +} + +impl UnionWithMustUseMethods { + + // These methods did not have the #[must_use] attribute in the old version. + // Addition of the attribute should be reported. + + pub fn MethodToMustUseMethod(&self) {} + + pub fn MethodToMustUseMessageMethod(&self) {} + + + // These methods had the #[must_use] attribute in the old version. Changes + // of the attribute, including deletion, should not be reported. + + #[must_use] + pub fn MustUseMethodToMethod(&self) {} + + #[must_use] + pub fn MustUseMethodToMustUseMessageMethod(&self) {} + + + // These methods 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. + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMethod(&self) {} + + #[must_use = "Foo"] + pub fn MustUseMessageMethodToMustUseMessageMethod(&self) {} +} + + +// This public union's inherent method did not have the #[must_use] attribute +// in the old version. Because the method is private, adding the attribute +// should NOT be reported. + +pub union UnionWithPrivateMustUseMethods { + bar: usize, +} + +impl UnionWithPrivateMustUseMethods { + + fn PrivateMethodToPrivateMustUseMethod(&self) {} +} + + +// This union is private and adding #[must_use] to its inherent method +// should NOT be reported. + +union PrivateUnionWithMustUseMethods { + bar: usize, +} + +impl PrivateUnionWithMustUseMethods { + + fn PrivateUnionMethodToPrivateUnionMustUseMethod(&self) {} +} From 9954b81b01b2ae4c1f195f18807e878e1ff11573 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Sat, 14 Jan 2023 16:29:22 +0100 Subject: [PATCH 05/11] Update test outputs for enum_missing & struct_missing --- test_outputs/enum_missing.output.ron | 24 ++++++++++++++++++++++++ test_outputs/struct_missing.output.ron | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/test_outputs/enum_missing.output.ron b/test_outputs/enum_missing.output.ron index 5a94813c..c9c795e2 100644 --- a/test_outputs/enum_missing.output.ron +++ b/test_outputs/enum_missing.output.ron @@ -33,4 +33,28 @@ "visibility_limit": String("public"), }, ], + "./test_crates/inherent_method_must_use_added/": [ + { + "name": String("EnumToStructWithMustUseMethods"), + "path": List([ + String("inherent_method_must_use_added"), + String("item_type_changed_inherent_method_must_use_added"), + String("EnumToStructWithMustUseMethods"), + ]), + "span_begin_line": Uint64(8), + "span_filename": String("src/item_type_changed_inherent_method_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "name": String("EnumToUnionWithMustUseMethods"), + "path": List([ + String("inherent_method_must_use_added"), + String("item_type_changed_inherent_method_must_use_added"), + String("EnumToUnionWithMustUseMethods"), + ]), + "span_begin_line": Uint64(35), + "span_filename": String("src/item_type_changed_inherent_method_must_use_added.rs"), + "visibility_limit": String("public"), + }, + ], } diff --git a/test_outputs/struct_missing.output.ron b/test_outputs/struct_missing.output.ron index 6e7e9a18..8a380cdb 100644 --- a/test_outputs/struct_missing.output.ron +++ b/test_outputs/struct_missing.output.ron @@ -1,4 +1,30 @@ { + "./test_crates/inherent_method_must_use_added/": [ + { + "name": String("StructToEnumWithMustUseMethods"), + "path": List([ + String("inherent_method_must_use_added"), + String("item_type_changed_inherent_method_must_use_added"), + String("StructToEnumWithMustUseMethods"), + ]), + "span_begin_line": Uint64(62), + "span_filename": String("src/item_type_changed_inherent_method_must_use_added.rs"), + "struct_type": String("plain"), + "visibility_limit": String("public"), + }, + { + "name": String("StructToUnionWithMustUseMethods"), + "path": List([ + String("inherent_method_must_use_added"), + String("item_type_changed_inherent_method_must_use_added"), + String("StructToUnionWithMustUseMethods"), + ]), + "span_begin_line": Uint64(87), + "span_filename": String("src/item_type_changed_inherent_method_must_use_added.rs"), + "struct_type": String("plain"), + "visibility_limit": String("public"), + }, + ], "./test_crates/struct_missing/": [ { "name": String("WillBeRemovedStruct"), From ce93ca05c18a5699f8dbd2cbbabe3729df90f8d1 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Sat, 14 Jan 2023 17:04:58 +0100 Subject: [PATCH 06/11] Update output for inherent_method_must_use_added --- .../inherent_method_must_use_added.output.ron | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/test_outputs/inherent_method_must_use_added.output.ron b/test_outputs/inherent_method_must_use_added.output.ron index 86931109..957ffe7c 100644 --- a/test_outputs/inherent_method_must_use_added.output.ron +++ b/test_outputs/inherent_method_must_use_added.output.ron @@ -1,5 +1,33 @@ { "./test_crates/inherent_method_must_use_added/": [ + { + "method_name": String("MethodToMustUseMethod"), + "method_visibility": String("public"), + "name": String("EnumWithMustUseMethods"), + "new_attr": String("#[must_use]"), + "path": List([ + String("inherent_method_must_use_added"), + String("enum_inherent_method_must_use_added"), + String("EnumWithMustUseMethods"), + ]), + "span_begin_line": Uint64(11), + "span_filename": String("src/enum_inherent_method_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMustUseMessageMethod"), + "method_visibility": String("public"), + "name": String("EnumWithMustUseMethods"), + "new_attr": String("#[must_use = \"Foo\"]"), + "path": List([ + String("inherent_method_must_use_added"), + String("enum_inherent_method_must_use_added"), + String("EnumWithMustUseMethods"), + ]), + "span_begin_line": Uint64(14), + "span_filename": String("src/enum_inherent_method_must_use_added.rs"), + "visibility_limit": String("public"), + }, { "method_name": String("MethodToMustUseMethod"), "method_visibility": String("public"), @@ -7,10 +35,11 @@ "new_attr": String("#[must_use]"), "path": List([ String("inherent_method_must_use_added"), + String("struct_inherent_method_must_use_added"), String("StructWithMustUseMethods"), ]), "span_begin_line": Uint64(9), - "span_filename": String("src/lib.rs"), + "span_filename": String("src/struct_inherent_method_must_use_added.rs"), "visibility_limit": String("public"), }, { @@ -20,10 +49,39 @@ "new_attr": String("#[must_use = \"Foo\"]"), "path": List([ String("inherent_method_must_use_added"), + String("struct_inherent_method_must_use_added"), String("StructWithMustUseMethods"), ]), "span_begin_line": Uint64(12), - "span_filename": String("src/lib.rs"), + "span_filename": String("src/struct_inherent_method_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMustUseMethod"), + "method_visibility": String("public"), + "name": String("UnionWithMustUseMethods"), + "new_attr": String("#[must_use]"), + "path": List([ + String("inherent_method_must_use_added"), + String("union_inherent_method_must_use_added"), + String("UnionWithMustUseMethods"), + ]), + "span_begin_line": Uint64(11), + "span_filename": String("src/union_inherent_method_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMustUseMessageMethod"), + "method_visibility": String("public"), + "name": String("UnionWithMustUseMethods"), + "new_attr": String("#[must_use = \"Foo\"]"), + "path": List([ + String("inherent_method_must_use_added"), + String("union_inherent_method_must_use_added"), + String("UnionWithMustUseMethods"), + ]), + "span_begin_line": Uint64(14), + "span_filename": String("src/union_inherent_method_must_use_added.rs"), "visibility_limit": String("public"), }, ], From 8c39124cb15d64b3685b0f09720aba650d5b5e0a Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Sat, 14 Jan 2023 20:07:02 +0100 Subject: [PATCH 07/11] Add `publish = false` to Cargo.toml --- test_crates/inherent_method_must_use_added/new/Cargo.toml | 1 + test_crates/inherent_method_must_use_added/old/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/test_crates/inherent_method_must_use_added/new/Cargo.toml b/test_crates/inherent_method_must_use_added/new/Cargo.toml index 6c98db59..2119cdf6 100644 --- a/test_crates/inherent_method_must_use_added/new/Cargo.toml +++ b/test_crates/inherent_method_must_use_added/new/Cargo.toml @@ -1,4 +1,5 @@ [package] +publish = false name = "inherent_method_must_use_added" version = "0.1.0" edition = "2021" diff --git a/test_crates/inherent_method_must_use_added/old/Cargo.toml b/test_crates/inherent_method_must_use_added/old/Cargo.toml index 6c98db59..2119cdf6 100644 --- a/test_crates/inherent_method_must_use_added/old/Cargo.toml +++ b/test_crates/inherent_method_must_use_added/old/Cargo.toml @@ -1,4 +1,5 @@ [package] +publish = false name = "inherent_method_must_use_added" version = "0.1.0" edition = "2021" From 826c1250914a8361e4f4fc9f12d158e3dc960a27 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Sat, 14 Jan 2023 22:38:21 +0100 Subject: [PATCH 08/11] Lint & update of output file. --- src/lints/inherent_method_must_use_added.ron | 24 ++++++++-------- .../inherent_method_must_use_added.output.ron | 28 ------------------- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/src/lints/inherent_method_must_use_added.ron b/src/lints/inherent_method_must_use_added.ron index 84549831..eebae326 100644 --- a/src/lints/inherent_method_must_use_added.ron +++ b/src/lints/inherent_method_must_use_added.ron @@ -10,11 +10,12 @@ SemverQuery( query: r#" { CrateDiff { - baseline { + current { item { ... on ImplOwner { visibility_limit @filter(op: "=", value: ["$public"]) @output name @tag @output + owner_type: __typename @tag importable_path { path @tag @output @@ -25,42 +26,43 @@ SemverQuery( method_visibility: visibility_limit @filter(op: "=", value: ["$public"]) @output method_name: name @tag @output - attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { + attribute { + new_attr: raw_attribute @output content { base @filter(op: "=", value: ["$must_use"]) } } + + span_: span @optional { + filename @output + begin_line @output + } } } } } } - current { + baseline { item { ... on ImplOwner { visibility_limit @filter(op: "=", value: ["$public"]) name @filter(op: "=", value: ["%name"]) + __typename @filter(op: "=", value: ["%owner_type"]) importable_path { path @filter(op: "=", value: ["%path"]) - } + } inherent_impl { method { visibility_limit @filter(op: "=", value: ["$public"]) name @filter(op: "=", value: ["%method_name"]) - attribute { - new_attr: raw_attribute @output + attribute @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { content { base @filter(op: "=", value: ["$must_use"]) } } - - span_: span @optional { - filename @output - begin_line @output - } } } } diff --git a/test_outputs/inherent_method_must_use_added.output.ron b/test_outputs/inherent_method_must_use_added.output.ron index 957ffe7c..baf4e969 100644 --- a/test_outputs/inherent_method_must_use_added.output.ron +++ b/test_outputs/inherent_method_must_use_added.output.ron @@ -55,34 +55,6 @@ "span_begin_line": Uint64(12), "span_filename": String("src/struct_inherent_method_must_use_added.rs"), "visibility_limit": String("public"), - }, - { - "method_name": String("MethodToMustUseMethod"), - "method_visibility": String("public"), - "name": String("UnionWithMustUseMethods"), - "new_attr": String("#[must_use]"), - "path": List([ - String("inherent_method_must_use_added"), - String("union_inherent_method_must_use_added"), - String("UnionWithMustUseMethods"), - ]), - "span_begin_line": Uint64(11), - "span_filename": String("src/union_inherent_method_must_use_added.rs"), - "visibility_limit": String("public"), - }, - { - "method_name": String("MethodToMustUseMessageMethod"), - "method_visibility": String("public"), - "name": String("UnionWithMustUseMethods"), - "new_attr": String("#[must_use = \"Foo\"]"), - "path": List([ - String("inherent_method_must_use_added"), - String("union_inherent_method_must_use_added"), - String("UnionWithMustUseMethods"), - ]), - "span_begin_line": Uint64(14), - "span_filename": String("src/union_inherent_method_must_use_added.rs"), - "visibility_limit": String("public"), }, ], } From e3d7e03253f96e855edea51d834670aa45041dc3 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk <92160712+SmolSir@users.noreply.github.com> Date: Sat, 14 Jan 2023 22:55:00 +0100 Subject: [PATCH 09/11] Update src/lints/inherent_method_must_use_added.ron Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/lints/inherent_method_must_use_added.ron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lints/inherent_method_must_use_added.ron b/src/lints/inherent_method_must_use_added.ron index eebae326..2189520c 100644 --- a/src/lints/inherent_method_must_use_added.ron +++ b/src/lints/inherent_method_must_use_added.ron @@ -51,7 +51,7 @@ SemverQuery( importable_path { path @filter(op: "=", value: ["%path"]) - } + } inherent_impl { method { From cdf677525368f1475dafab2dc03d39084992897b Mon Sep 17 00:00:00 2001 From: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> Date: Sun, 15 Jan 2023 12:48:18 -0500 Subject: [PATCH 10/11] Apply suggestions from code review --- src/lints/inherent_method_must_use_added.ron | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lints/inherent_method_must_use_added.ron b/src/lints/inherent_method_must_use_added.ron index 2189520c..aa4bdf88 100644 --- a/src/lints/inherent_method_must_use_added.ron +++ b/src/lints/inherent_method_must_use_added.ron @@ -15,7 +15,7 @@ SemverQuery( ... on ImplOwner { visibility_limit @filter(op: "=", value: ["$public"]) @output name @tag @output - owner_type: __typename @tag + owner_type: __typename @tag @output importable_path { path @tag @output @@ -76,5 +76,5 @@ SemverQuery( "zero": 0, }, error_message: "An inherent method is now #[must_use]. Downstream crates that did not use its return value will get a compiler lint.", - per_result_error_template: Some("inherent method {{join \"::\" path}}::{{method_name}} in {{span_filename}}:{{span_begin_line}}"), + per_result_error_template: Some("method {{join \"::\" path}}::{{method_name}} in {{span_filename}}:{{span_begin_line}}"), ) From 51a4555a86697417850d96d856325b8c6358a997 Mon Sep 17 00:00:00 2001 From: Bartosz Smolarczyk Date: Sun, 15 Jan 2023 23:03:36 +0100 Subject: [PATCH 11/11] Update `inherent_method_must_use_added.output.ron` --- test_outputs/inherent_method_must_use_added.output.ron | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test_outputs/inherent_method_must_use_added.output.ron b/test_outputs/inherent_method_must_use_added.output.ron index baf4e969..3bdeda72 100644 --- a/test_outputs/inherent_method_must_use_added.output.ron +++ b/test_outputs/inherent_method_must_use_added.output.ron @@ -5,6 +5,7 @@ "method_visibility": String("public"), "name": String("EnumWithMustUseMethods"), "new_attr": String("#[must_use]"), + "owner_type": String("Enum"), "path": List([ String("inherent_method_must_use_added"), String("enum_inherent_method_must_use_added"), @@ -19,6 +20,7 @@ "method_visibility": String("public"), "name": String("EnumWithMustUseMethods"), "new_attr": String("#[must_use = \"Foo\"]"), + "owner_type": String("Enum"), "path": List([ String("inherent_method_must_use_added"), String("enum_inherent_method_must_use_added"), @@ -33,6 +35,7 @@ "method_visibility": String("public"), "name": String("StructWithMustUseMethods"), "new_attr": String("#[must_use]"), + "owner_type": String("Struct"), "path": List([ String("inherent_method_must_use_added"), String("struct_inherent_method_must_use_added"), @@ -47,6 +50,7 @@ "method_visibility": String("public"), "name": String("StructWithMustUseMethods"), "new_attr": String("#[must_use = \"Foo\"]"), + "owner_type": String("Struct"), "path": List([ String("inherent_method_must_use_added"), String("struct_inherent_method_must_use_added"),