Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add a witness for enum_variant_missing #978

Merged
merged 8 commits into from
Oct 18, 2024
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}};"#,
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
),
)
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 },
}
15 changes: 13 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,21 @@
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 },
}
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved

/// 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(22),
"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(16),
"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 = 16
hint = 'let witness = enum_variant_missing::StructVariantWillBeRemoved::Bar{...};'
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
Loading