-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add enum variant discriminant changed lint
- Loading branch information
1 parent
269f88b
commit 0e24e8e
Showing
7 changed files
with
197 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
SemverQuery( | ||
id: "enum_variant_discriminant_changed", | ||
human_readable_name: "Public enum's variant had its discriminant changed from its previous value", | ||
description: "A public enum's variant had its discriminant changed from its previous value.", | ||
reference: Some("The public enum's variant had its discriminant changed from its previous value. This can cause compatibility issues, which may break FFI use cases."), | ||
required_update: Major, | ||
lint_level: Deny, | ||
reference_link: Some("https://doc.rust-lang.org/reference/items/enumerations.html#discriminants"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
baseline { | ||
item { | ||
... on Enum { | ||
visibility_limit @filter(op: "=", value: ["$public"]) @output | ||
enum_name: name @output @tag | ||
importable_path { | ||
path @output @tag | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
variant { | ||
variant_name: name @output @tag | ||
discriminant @optional { | ||
old_value: value @output @tag | ||
} | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on Enum { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
name @filter(op: "=", value: ["%enum_name"]) | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
variant { | ||
name @filter(op: "=", value: ["%variant_name"]) | ||
discriminant { | ||
new_value: value @output @filter (op: "!=", value: ["%old_value"]) | ||
} | ||
} | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"true": true, | ||
}, | ||
error_message: "The public enum's variant had its discriminant changed from its previous value. This can cause compatibility issues, which may break FFI use cases.", | ||
per_result_error_template: Some("variant {{enum_name}}::{{variant_name}} {{old_value}} -> {{new_value}} in {{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
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,7 @@ | ||
[package] | ||
publish = false | ||
name = "enum_variant_discriminant_changed" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
31 changes: 31 additions & 0 deletions
31
test_crates/enum_variant_discriminant_changed/new/src/lib.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,31 @@ | ||
// Explicit discriminant changed values. By doing so, it changed the implicit | ||
// discriminant's value as well, should be reported. | ||
#[repr(u8, C)] | ||
pub enum ExplicitAndImplicitDiscriminantsAreChanged { | ||
First = 2, | ||
Second, | ||
Third = 5, | ||
} | ||
|
||
// Implicit discriminant changed values when becoming explicit, should be reported. | ||
#[repr(u8)] | ||
pub enum ImplicitDiscriminantBecomesExplicit { | ||
Tuple(), | ||
Struct, | ||
Unit = 5, | ||
} | ||
|
||
// Discriminant changed to be doc hidden and explicit. Being doc hidden is not relevant | ||
// since it's still part of the public API, should be reported. | ||
pub enum DiscriminantBecomesDocHiddenAndExplicit { | ||
First, | ||
#[doc(hidden)] | ||
Second = 2, | ||
} | ||
|
||
// Explicit discriminants changed values, but being private dominates, should not be | ||
// reported. | ||
enum PrivateEnum { | ||
First = 10, | ||
Second = 11, | ||
} |
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,7 @@ | ||
[package] | ||
publish = false | ||
name = "enum_variant_discriminant_changed" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
30 changes: 30 additions & 0 deletions
30
test_crates/enum_variant_discriminant_changed/old/src/lib.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,30 @@ | ||
// Explicit discriminant changed values. By doing so, it changed the implicit | ||
// discriminant's value as well, should be reported. | ||
#[repr(u8, C)] | ||
pub enum ExplicitAndImplicitDiscriminantsAreChanged { | ||
First = 1, | ||
Second, | ||
Third = 5, | ||
} | ||
|
||
// Implicit discriminant changed values when becoming explicit, should be reported. | ||
#[repr(u8)] | ||
pub enum ImplicitDiscriminantBecomesExplicit { | ||
Tuple(), | ||
Struct {}, | ||
Unit, | ||
} | ||
|
||
// Discriminant changed to be doc hidden and explicit. Being doc hidden is not relevant | ||
// since it's still part of the public API, should be reported. | ||
pub enum DiscriminantBecomesDocHiddenAndExplicit { | ||
First, | ||
Second, | ||
} | ||
|
||
// Explicit discriminants changed values, but being private dominates, should not be | ||
// reported. | ||
enum PrivateEnum { | ||
First = 1, | ||
Second = 2, | ||
} |
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,56 @@ | ||
{ | ||
"./test_crates/enum_variant_discriminant_changed/": [ | ||
{ | ||
"enum_name": String("ExplicitAndImplicitDiscriminantsAreChanged"), | ||
"new_value": String("2"), | ||
"old_value": String("1"), | ||
"path": List([ | ||
String("enum_variant_discriminant_changed"), | ||
String("ExplicitAndImplicitDiscriminantsAreChanged"), | ||
]), | ||
"span_begin_line": Uint64(4), | ||
"span_filename": String("src/lib.rs"), | ||
"variant_name": String("First"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"enum_name": String("ExplicitAndImplicitDiscriminantsAreChanged"), | ||
"new_value": String("3"), | ||
"old_value": String("2"), | ||
"path": List([ | ||
String("enum_variant_discriminant_changed"), | ||
String("ExplicitAndImplicitDiscriminantsAreChanged"), | ||
]), | ||
"span_begin_line": Uint64(4), | ||
"span_filename": String("src/lib.rs"), | ||
"variant_name": String("Second"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"enum_name": String("ImplicitDiscriminantBecomesExplicit"), | ||
"new_value": String("5"), | ||
"old_value": String("2"), | ||
"path": List([ | ||
String("enum_variant_discriminant_changed"), | ||
String("ImplicitDiscriminantBecomesExplicit"), | ||
]), | ||
"span_begin_line": Uint64(12), | ||
"span_filename": String("src/lib.rs"), | ||
"variant_name": String("Unit"), | ||
"visibility_limit": String("public"), | ||
}, | ||
{ | ||
"enum_name": String("DiscriminantBecomesDocHiddenAndExplicit"), | ||
"new_value": String("2"), | ||
"old_value": String("1"), | ||
"path": List([ | ||
String("enum_variant_discriminant_changed"), | ||
String("DiscriminantBecomesDocHiddenAndExplicit"), | ||
]), | ||
"span_begin_line": Uint64(20), | ||
"span_filename": String("src/lib.rs"), | ||
"variant_name": String("Second"), | ||
"visibility_limit": String("public"), | ||
}, | ||
] | ||
} |