Skip to content

Commit 887bb81

Browse files
Chore: Add witness hint for enum_tuple_variant_field_added lint (#1212)
Adds a witness hint for the `enum_tuple_variant_field_added` lint. ## Example `enum_tuple_variant_field_added` outputs the following witness hint for the `./test_crates/enum_tuple_variant_field_added/` test. ```rust match value { enum_tuple_variant_field_added::PublicEnum::TupleVariantWithFieldAdded (_, _) => (), _ => (), } ``` This compiles on the old version, but not on the new due to the lack of exhaustiveness in the destructuring of the enum variant. ## Commit Notes + Added witness hint for enum_tuple_variant_field_added + Added new query for baseline_field_names for extra witness data ## Comments It took a little bit to figure out what would be the best way to execute this in the handlebars template. Settled on an `#each` iterating over the baseline fields, though the indices are discarded and just replaced with underscores. There's also an `#unless @last` to only join with the commas, rather than always placing it after.
1 parent 0c0ea42 commit 887bb81

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/lints/enum_tuple_variant_field_added.ron

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ SemverQuery(
6969
field @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
7070
name @filter(op: "=", value: ["%field_name"])
7171
}
72+
73+
field @fold @transform(op: "count") @output(name: "field_count")
7274
}
7375
}
7476
}
@@ -84,4 +86,10 @@ SemverQuery(
8486
},
8587
error_message: "An enum's exhaustive tuple variant has a new field, which has to be included when constructing or matching on this variant.",
8688
per_result_error_template: Some("field {{field_name}} of variant {{enum_name}}::{{variant_name}} in {{span_filename}}:{{span_begin_line}}"),
89+
witness: (
90+
hint_template: r#"match value {
91+
{{ join "::" path }}::{{ variant_name }} ({{#repeat field_count }}_{{#unless @last}}, {{/unless}}{{/repeat}}) => (),
92+
_ => (),
93+
}"#,
94+
),
8795
)

test_outputs/query_execution/enum_tuple_variant_field_added.snap

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ snapshot_kind: text
77
"./test_crates/enum_struct_field_hidden_from_public_api/": [
88
{
99
"enum_name": String("AddedVariantField"),
10+
"field_count": Uint64(1),
1011
"field_name": String("1"),
1112
"path": List([
1213
String("enum_struct_field_hidden_from_public_api"),
@@ -21,6 +22,7 @@ snapshot_kind: text
2122
"./test_crates/enum_tuple_variant_field_added/": [
2223
{
2324
"enum_name": String("PublicEnum"),
25+
"field_count": Uint64(2),
2426
"field_name": String("2"),
2527
"path": List([
2628
String("enum_tuple_variant_field_added"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: src/query.rs
3+
description: "Lint `enum_tuple_variant_field_added` did not have the expected witness output.\nSee https://github.com/obi1kenobi/cargo-semver-checks/blob/main/CONTRIBUTING.md#testing-witnesses\nfor more information."
4+
expression: "&actual_witnesses"
5+
snapshot_kind: text
6+
---
7+
[["./test_crates/enum_struct_field_hidden_from_public_api/"]]
8+
filename = 'src/lib.rs'
9+
begin_line = 43
10+
hint = '''
11+
match value {
12+
enum_struct_field_hidden_from_public_api::AddedVariantField::TupleVariant (_) => (),
13+
_ => (),
14+
}'''
15+
16+
[["./test_crates/enum_tuple_variant_field_added/"]]
17+
filename = 'src/lib.rs'
18+
begin_line = 5
19+
hint = '''
20+
match value {
21+
enum_tuple_variant_field_added::PublicEnum::TupleVariantWithFieldAdded (_, _) => (),
22+
_ => (),
23+
}'''

0 commit comments

Comments
 (0)