forked from obi1kenobi/cargo-semver-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New lint - detecting field removals from a struct variant in enum (ob…
…i1kenobi#156) * Added first, untested code * Changed tabs to spaces * First nonworking test * Fixed test * Changed occurences of structvariant with struct_variant * Improved descriptions Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>
- Loading branch information
1 parent
3d585a1
commit 19fa6aa
Showing
6 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
semver_tests/src/test_cases/enum_struct_variant_field_missing.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
pub enum Enum { | ||
FieldWillBeMissing { | ||
foo: usize, | ||
|
||
/// Testing: <https://doc.rust-lang.org/cargo/reference/semver.html#item-remove> | ||
#[cfg(not(feature = "enum_struct_variant_field_missing"))] | ||
bar: usize, | ||
} | ||
} | ||
|
||
/// This struct variant should not be reported by the `enum_struct_variant_field_missing` rule: | ||
/// it will be removed altogether, so the correct rule to catch it is | ||
/// the `enum_variant_missing` rule and not the rule for missing fields. | ||
pub enum IgnoredEnum { | ||
#[cfg(not(feature = "enum_struct_variant_field_missing"))] | ||
StructVariantWillBeMissing { | ||
foo: usize, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
SemverQuery( | ||
id: "enum_struct_variant_field_missing", | ||
human_readable_name: "pub enum struct variant's field removed or renamed" , | ||
description: "An enum's struct variant has a field that is no longer available under its prior name.", | ||
required_update: Major, | ||
reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#item-remove"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
baseline { | ||
item { | ||
... on Enum { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
enum_name: name @output @tag | ||
importable_path { | ||
path @output @tag | ||
} | ||
variant { | ||
... on StructVariant { | ||
variant_name: name @output @tag | ||
field { | ||
field_name: name @output @tag | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on Enum { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @filter(op: "=", value: ["%enum_name"]) | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
} | ||
variant { | ||
... on StructVariant { | ||
name @filter(op: "=", value: ["%variant_name"]) | ||
field @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) { | ||
name @filter(op: "=", value: ["%field_name"]) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"zero": 0, | ||
}, | ||
error_message: "A publicly-visible enum has a struct variant whose field is no longer available under its prior name. It may have been renamed or removed entirely.", | ||
per_result_error_template: Some("field {{field_name}} of variant {{enum_name}}::{{variant_name}}, previously in file {{span_filename}}:{{span_begin_line}}"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test_data/enum_struct_variant_field_missing.output.ron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[ | ||
{ | ||
"enum_name": String("Enum"), | ||
"variant_name": String("FieldWillBeMissing"), | ||
"path": List([ | ||
String("semver_tests"), | ||
String("test_cases"), | ||
String("enum_struct_variant_field_missing"), | ||
String("Enum"), | ||
]), | ||
"field_name": String("bar"), | ||
"span_filename": String("src/test_cases/enum_struct_variant_field_missing.rs"), | ||
"span_begin_line": Uint64(7), | ||
} | ||
] |