From 99006922342ee6923277052240704255b455dd51 Mon Sep 17 00:00:00 2001 From: PedroTurik Date: Thu, 29 Aug 2024 00:08:10 -0300 Subject: [PATCH] adds new trait-method-added lint (#891) * adds new trait-method-added lint * corrects lint to account for traits that have also become sealed, and adds tests for it --- src/lints/trait_method_added.ron | 60 +++ src/query.rs | 1 + test_crates/trait_method_added/new/Cargo.toml | 7 + test_crates/trait_method_added/new/src/lib.rs | 49 +++ test_crates/trait_method_added/old/Cargo.toml | 7 + test_crates/trait_method_added/old/src/lib.rs | 31 ++ test_outputs/trait_method_added.output.ron | 360 ++++++++++++++++++ test_outputs/trait_newly_sealed.output.ron | 12 + 8 files changed, 527 insertions(+) create mode 100644 src/lints/trait_method_added.ron create mode 100644 test_crates/trait_method_added/new/Cargo.toml create mode 100644 test_crates/trait_method_added/new/src/lib.rs create mode 100644 test_crates/trait_method_added/old/Cargo.toml create mode 100644 test_crates/trait_method_added/old/src/lib.rs create mode 100644 test_outputs/trait_method_added.output.ron diff --git a/src/lints/trait_method_added.ron b/src/lints/trait_method_added.ron new file mode 100644 index 00000000..c5ebd43b --- /dev/null +++ b/src/lints/trait_method_added.ron @@ -0,0 +1,60 @@ +SemverQuery( + id: "trait_method_added", + human_readable_name: "pub trait method added", + description: "A non-sealed public trait added a new method without a default implementation", + required_update: Major, + lint_level: Deny, + reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#trait-new-item-no-default"), + query: r#" + { + CrateDiff { + current { + item { + ... on Trait { + visibility_limit @filter(op: "=", value: ["$public"]) @output + name @output + + importable_path { + path @output @tag + public_api @filter(op: "=", value: ["$true"]) + } + + method { + method_name: name @output @tag + has_body @filter(op: "!=", value: ["$true"]) + + span_: span @optional { + filename @output + begin_line @output + } + } + } + } + } + baseline { + item { + ... on Trait { + visibility_limit @filter(op: "=", value: ["$public"]) + sealed @filter(op: "!=", value: ["$true"]) + + importable_path { + path @filter(op: "=", value: ["%path"]) + public_api @filter(op: "=", value: ["$true"]) + } + + method @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { + name @filter(op: "=", value: ["%method_name"]) + } + } + } + } + } + }"#, + arguments: { + "true": true, + "public": "public", + "zero": 0, + }, + error_message: "A non-sealed public trait added a new method without a default implementation, which breaks downstream implementations of the trait", + per_result_error_template: Some("trait method {{join \"::\" path}}::{{method_name}} in file {{span_filename}}:{{span_begin_line}}"), +) diff --git a/src/query.rs b/src/query.rs index 79a1f1cd..a0125dfc 100644 --- a/src/query.rs +++ b/src/query.rs @@ -840,6 +840,7 @@ add_lints!( trait_associated_type_default_removed, trait_associated_type_now_doc_hidden, trait_default_impl_removed, + trait_method_added, trait_method_missing, trait_method_now_doc_hidden, trait_method_unsafe_added, diff --git a/test_crates/trait_method_added/new/Cargo.toml b/test_crates/trait_method_added/new/Cargo.toml new file mode 100644 index 00000000..878fba75 --- /dev/null +++ b/test_crates/trait_method_added/new/Cargo.toml @@ -0,0 +1,7 @@ +[package] +publish = false +name = "trait_method_added" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/trait_method_added/new/src/lib.rs b/test_crates/trait_method_added/new/src/lib.rs new file mode 100644 index 00000000..564a786a --- /dev/null +++ b/test_crates/trait_method_added/new/src/lib.rs @@ -0,0 +1,49 @@ +mod sealed { + pub(crate) trait Sealed {} +} + +// ---- Should be reported ---- +pub trait WillGainMethodWithoutDefault { + fn one_method(self); +} + +pub trait WillGainAnotherMethodWithoutDefault { + fn one_method(self); + fn two_method(self); +} + +pub trait WillGainMultipleMethodsWithoutDefault { + fn one_method(self); + fn two_method(self); +} + +pub trait WillGainMethodWithoutDefaultAndSeal: sealed::Sealed { + fn one_method(self); +} + +pub trait WIllGainDocHiddenMethodWithoutDefault { + #[doc(hidden)] + fn one_method(self); +} + +// ---- Should not be reported ---- +pub trait WillGainMethodWithDefault { + fn one_method(self) {} +} + +pub trait WillGainAnotherMethodWithDefault { + fn one_method(self); + fn two_method(self) {} +} + +pub trait WillGainMethodWithoutDefaultSealed: sealed::Sealed { + fn one_method(self); +} + +pub trait WillGainMethodWithoutDefaultAndLoseSeal { + fn one_method(self); +} + +pub trait WillKeepAMethodWithoutDefault { + fn one_method(self); +} diff --git a/test_crates/trait_method_added/old/Cargo.toml b/test_crates/trait_method_added/old/Cargo.toml new file mode 100644 index 00000000..878fba75 --- /dev/null +++ b/test_crates/trait_method_added/old/Cargo.toml @@ -0,0 +1,7 @@ +[package] +publish = false +name = "trait_method_added" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/test_crates/trait_method_added/old/src/lib.rs b/test_crates/trait_method_added/old/src/lib.rs new file mode 100644 index 00000000..004952ea --- /dev/null +++ b/test_crates/trait_method_added/old/src/lib.rs @@ -0,0 +1,31 @@ +mod sealed { + pub(crate) trait Sealed {} +} + +// ---- Should be reported ---- +pub trait WillGainMethodWithoutDefault {} + +pub trait WillGainAnotherMethodWithoutDefault { + fn one_method(self); +} + +pub trait WillGainMultipleMethodsWithoutDefault {} + +pub trait WillGainMethodWithoutDefaultAndSeal {} + +pub trait WIllGainDocHiddenMethodWithoutDefault {} + +// ---- Should not be reported ---- +pub trait WillGainMethodWithDefault {} + +pub trait WillGainAnotherMethodWithDefault { + fn one_method(self); +} + +pub trait WillGainMethodWithoutDefaultSealed: sealed::Sealed {} + +pub trait WillGainMethodWithoutDefaultAndLoseSeal: sealed::Sealed {} + +pub trait WillKeepAMethodWithoutDefault { + fn one_method(self); +} diff --git a/test_outputs/trait_method_added.output.ron b/test_outputs/trait_method_added.output.ron new file mode 100644 index 00000000..c1589c82 --- /dev/null +++ b/test_outputs/trait_method_added.output.ron @@ -0,0 +1,360 @@ +{ + "./test_crates/method_moved_to_trait_must_use_added/": [ + { + "method_name": String("MethodToMovedDeclaredMustUseMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(49), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(52), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMethodToMovedDeclaredMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(54), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(57), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(59), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMustUseMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(62), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(65), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedImplDeclaredMustUseMethod"), + "name": String("TraitWithMovedImplMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("enum_method_moved_to_trait_must_use_added"), + String("TraitWithMovedImplMustUseMethods"), + ]), + "span_begin_line": Uint64(135), + "span_filename": String("src/enum_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedDeclaredMustUseMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(45), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(48), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMethodToMovedDeclaredMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(50), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(53), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(55), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMustUseMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(58), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(61), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedImplDeclaredMustUseMethod"), + "name": String("TraitWithMovedImplMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("struct_method_moved_to_trait_must_use_added"), + String("TraitWithMovedImplMustUseMethods"), + ]), + "span_begin_line": Uint64(127), + "span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedDeclaredMustUseMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(49), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(52), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMethodToMovedDeclaredMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(54), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(57), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(59), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMustUseMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(62), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MustUseMessageMethodToMovedDeclaredMustUseMessageMethod"), + "name": String("TraitWithMovedDeclaredMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedDeclaredMustUseMethods"), + ]), + "span_begin_line": Uint64(65), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("MethodToMovedImplDeclaredMustUseMethod"), + "name": String("TraitWithMovedImplMustUseMethods"), + "path": List([ + String("method_moved_to_trait_must_use_added"), + String("union_method_moved_to_trait_must_use_added"), + String("TraitWithMovedImplMustUseMethods"), + ]), + "span_begin_line": Uint64(135), + "span_filename": String("src/union_method_moved_to_trait_must_use_added.rs"), + "visibility_limit": String("public"), + }, + ], + "./test_crates/trait_method_added/": [ + { + "method_name": String("one_method"), + "name": String("WillGainMethodWithoutDefault"), + "path": List([ + String("trait_method_added"), + String("WillGainMethodWithoutDefault"), + ]), + "span_begin_line": Uint64(7), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("two_method"), + "name": String("WillGainAnotherMethodWithoutDefault"), + "path": List([ + String("trait_method_added"), + String("WillGainAnotherMethodWithoutDefault"), + ]), + "span_begin_line": Uint64(12), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("one_method"), + "name": String("WillGainMultipleMethodsWithoutDefault"), + "path": List([ + String("trait_method_added"), + String("WillGainMultipleMethodsWithoutDefault"), + ]), + "span_begin_line": Uint64(16), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("two_method"), + "name": String("WillGainMultipleMethodsWithoutDefault"), + "path": List([ + String("trait_method_added"), + String("WillGainMultipleMethodsWithoutDefault"), + ]), + "span_begin_line": Uint64(17), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("one_method"), + "name": String("WillGainMethodWithoutDefaultAndSeal"), + "path": List([ + String("trait_method_added"), + String("WillGainMethodWithoutDefaultAndSeal"), + ]), + "span_begin_line": Uint64(21), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + { + "method_name": String("one_method"), + "name": String("WIllGainDocHiddenMethodWithoutDefault"), + "path": List([ + String("trait_method_added"), + String("WIllGainDocHiddenMethodWithoutDefault"), + ]), + "span_begin_line": Uint64(26), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + ], +} diff --git a/test_outputs/trait_newly_sealed.output.ron b/test_outputs/trait_newly_sealed.output.ron index 1363a9d1..14aee41b 100644 --- a/test_outputs/trait_newly_sealed.output.ron +++ b/test_outputs/trait_newly_sealed.output.ron @@ -11,6 +11,18 @@ "visibility_limit": String("public"), }, ], + "./test_crates/trait_method_added/": [ + { + "name": String("WillGainMethodWithoutDefaultAndSeal"), + "path": List([ + String("trait_method_added"), + String("WillGainMethodWithoutDefaultAndSeal"), + ]), + "span_begin_line": Uint64(20), + "span_filename": String("src/lib.rs"), + "visibility_limit": String("public"), + }, + ], "./test_crates/trait_newly_sealed/": [ { "name": String("TraitBecomesSealed"),