Skip to content

Commit

Permalink
chore: add a witness for enum_variant_missing (#978)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
CommanderStorm and obi1kenobi authored Oct 18, 2024
1 parent ed5fa24 commit 4ca3198
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/lints/enum_variant_missing.ron
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ SemverQuery(
}
variant {
kind: __typename @output
variant_name: name @output @tag
public_api_eligible @filter(op: "=", value: ["$true"])
Expand Down Expand Up @@ -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}};"#,
),
)
10 changes: 9 additions & 1 deletion test_crates/enum_variant_missing/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
pub enum VariantWillBeRemoved {
pub enum PlainVariantWillBeRemoved {
Foo,
}

pub enum TupleVariantWillBeRemoved {
Foo(usize),
}

pub enum StructVariantWillBeRemoved {
Foo { bar: usize },
}
19 changes: 17 additions & 2 deletions test_crates/enum_variant_missing/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
pub enum VariantWillBeRemoved {
pub enum PlainVariantWillBeRemoved {
Foo,

/// Testing: <https://doc.rust-lang.org/cargo/reference/semver.html#item-remove>
Bar,
}

pub enum TupleVariantWillBeRemoved {
Foo(usize),
/// Testing: <https://doc.rust-lang.org/cargo/reference/semver.html#item-remove>
Bar(usize),
}

pub enum StructVariantWillBeRemoved {
Foo {
bar: usize,
},
/// Testing: <https://doc.rust-lang.org/cargo/reference/semver.html#item-remove>
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.
Expand Down
2 changes: 1 addition & 1 deletion test_outputs/query_execution/enum_missing.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand Down
32 changes: 29 additions & 3 deletions test_outputs/query_execution/enum_variant_missing.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand Down
24 changes: 24 additions & 0 deletions test_outputs/witnesses/enum_variant_missing.snap
Original file line number Diff line number Diff line change
@@ -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 {...};'

0 comments on commit 4ca3198

Please sign in to comment.