From 4ca319887558e71bdf7d82b735b0d96ab931826b Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Fri, 18 Oct 2024 13:18:34 +0200 Subject: [PATCH] chore: add a witness for `enum_variant_missing` (#978) * made sure that variant missing has a witness * made sure that the whitness respects tuple, struct and plain enums with the correct brackets * fixed line numbers having shifted due to whitepace fixes * Apply suggestions from code review * apply cargo fmt * fixed things missed while rebasing onto remote --------- Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> --- src/lints/enum_variant_missing.ron | 5 +++ .../enum_variant_missing/new/src/lib.rs | 10 +++++- .../enum_variant_missing/old/src/lib.rs | 19 +++++++++-- .../query_execution/enum_missing.snap | 2 +- .../query_execution/enum_variant_missing.snap | 32 +++++++++++++++++-- .../witnesses/enum_variant_missing.snap | 24 ++++++++++++++ 6 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 test_outputs/witnesses/enum_variant_missing.snap diff --git a/src/lints/enum_variant_missing.ron b/src/lints/enum_variant_missing.ron index 258fc630..4af5f023 100644 --- a/src/lints/enum_variant_missing.ron +++ b/src/lints/enum_variant_missing.ron @@ -19,6 +19,8 @@ SemverQuery( } variant { + kind: __typename @output + variant_name: name @output @tag public_api_eligible @filter(op: "=", value: ["$true"]) @@ -56,4 +58,7 @@ SemverQuery( }, error_message: "A publicly-visible enum has at least one variant that is no longer available under its prior name. It may have been renamed or removed entirely.", per_result_error_template: Some("variant {{enum_name}}::{{variant_name}}, previously in file {{span_filename}}:{{span_begin_line}}"), + witness: ( + hint_template: r#"let witness = {{join "::" path}}::{{variant_name}}{{#if (eq kind "StructVariant")}} {...}{{/if}}{{#if (eq kind "TupleVariant")}}(...){{/if}};"#, + ), ) diff --git a/test_crates/enum_variant_missing/new/src/lib.rs b/test_crates/enum_variant_missing/new/src/lib.rs index 9e519bbb..2b172768 100644 --- a/test_crates/enum_variant_missing/new/src/lib.rs +++ b/test_crates/enum_variant_missing/new/src/lib.rs @@ -1,3 +1,11 @@ -pub enum VariantWillBeRemoved { +pub enum PlainVariantWillBeRemoved { Foo, } + +pub enum TupleVariantWillBeRemoved { + Foo(usize), +} + +pub enum StructVariantWillBeRemoved { + Foo { bar: usize }, +} diff --git a/test_crates/enum_variant_missing/old/src/lib.rs b/test_crates/enum_variant_missing/old/src/lib.rs index a136c7c3..39203aca 100644 --- a/test_crates/enum_variant_missing/old/src/lib.rs +++ b/test_crates/enum_variant_missing/old/src/lib.rs @@ -1,10 +1,25 @@ -pub enum VariantWillBeRemoved { +pub enum PlainVariantWillBeRemoved { Foo, - /// Testing: Bar, } +pub enum TupleVariantWillBeRemoved { + Foo(usize), + /// Testing: + Bar(usize), +} + +pub enum StructVariantWillBeRemoved { + Foo { + bar: usize, + }, + /// Testing: + Bar { + bar: usize, + }, +} + /// This enum should not be reported by the `enum_variant_missing` rule: /// it will be removed altogether, so the correct rule to catch it is /// the `enum_missing` rule and not the rule for missing variants. diff --git a/test_outputs/query_execution/enum_missing.snap b/test_outputs/query_execution/enum_missing.snap index 57a19292..05a1df7a 100644 --- a/test_outputs/query_execution/enum_missing.snap +++ b/test_outputs/query_execution/enum_missing.snap @@ -56,7 +56,7 @@ expression: "&query_execution_results" String("enum_variant_missing"), String("ShouldNotMatch"), ]), - "span_begin_line": Uint64(11), + "span_begin_line": Uint64(26), "span_filename": String("src/lib.rs"), "visibility_limit": String("public"), }, diff --git a/test_outputs/query_execution/enum_variant_missing.snap b/test_outputs/query_execution/enum_variant_missing.snap index 88533788..50402829 100644 --- a/test_outputs/query_execution/enum_variant_missing.snap +++ b/test_outputs/query_execution/enum_variant_missing.snap @@ -6,6 +6,7 @@ expression: "&query_execution_results" "./test_crates/enum_struct_variant_field_missing/": [ { "enum_name": String("IgnoredEnum"), + "kind": String("StructVariant"), "path": List([ String("enum_struct_variant_field_missing"), String("IgnoredEnum"), @@ -18,12 +19,37 @@ expression: "&query_execution_results" ], "./test_crates/enum_variant_missing/": [ { - "enum_name": String("VariantWillBeRemoved"), + "enum_name": String("PlainVariantWillBeRemoved"), + "kind": String("PlainVariant"), "path": List([ String("enum_variant_missing"), - String("VariantWillBeRemoved"), + String("PlainVariantWillBeRemoved"), ]), - "span_begin_line": Uint64(5), + "span_begin_line": Uint64(4), + "span_filename": String("src/lib.rs"), + "variant_name": String("Bar"), + "visibility_limit": String("public"), + }, + { + "enum_name": String("TupleVariantWillBeRemoved"), + "kind": String("TupleVariant"), + "path": List([ + String("enum_variant_missing"), + String("TupleVariantWillBeRemoved"), + ]), + "span_begin_line": Uint64(10), + "span_filename": String("src/lib.rs"), + "variant_name": String("Bar"), + "visibility_limit": String("public"), + }, + { + "enum_name": String("StructVariantWillBeRemoved"), + "kind": String("StructVariant"), + "path": List([ + String("enum_variant_missing"), + String("StructVariantWillBeRemoved"), + ]), + "span_begin_line": Uint64(18), "span_filename": String("src/lib.rs"), "variant_name": String("Bar"), "visibility_limit": String("public"), diff --git a/test_outputs/witnesses/enum_variant_missing.snap b/test_outputs/witnesses/enum_variant_missing.snap new file mode 100644 index 00000000..edec097c --- /dev/null +++ b/test_outputs/witnesses/enum_variant_missing.snap @@ -0,0 +1,24 @@ +--- +source: src/query.rs +description: "Lint `enum_variant_missing` did not have the expected witness output.\nSee https://github.com/obi1kenobi/cargo-semver-checks/blob/main/CONTRIBUTING.md#testing-witnesses\nfor more information." +expression: "&actual_witnesses" +--- +[["./test_crates/enum_struct_variant_field_missing/"]] +filename = 'src/lib.rs' +begin_line = 14 +hint = 'let witness = enum_struct_variant_field_missing::IgnoredEnum::StructVariantWillBeMissing {...};' + +[["./test_crates/enum_variant_missing/"]] +filename = 'src/lib.rs' +begin_line = 4 +hint = 'let witness = enum_variant_missing::PlainVariantWillBeRemoved::Bar;' + +[["./test_crates/enum_variant_missing/"]] +filename = 'src/lib.rs' +begin_line = 10 +hint = 'let witness = enum_variant_missing::TupleVariantWillBeRemoved::Bar(...);' + +[["./test_crates/enum_variant_missing/"]] +filename = 'src/lib.rs' +begin_line = 18 +hint = 'let witness = enum_variant_missing::StructVariantWillBeRemoved::Bar {...};'