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

Add enum_no_longer_non_exhaustive lint. #1181

Merged
merged 7 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/lints/enum_unmarked_non_exhaustive.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
SemverQuery(
id: "enum_unmarked_non_exhaustive",
human_readable_name: "enum unmarked #[non_exhaustive]",
description: "A Public API enum is no longer #[non_exhaustive]",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure your capitalization and word/style choices match existing lints.

required_update: Minor,
lint_level: Allow,
reference_link: Some("https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute"),
query: r#"
{
CrateDiff {
current {
item {
... on Enum {
visibility_limit @filter(op: "=", value: ["$public"]) @output
name @output @tag

attrs @filter(op: "not_contains", value: ["$non_exhaustive"])

importable_path {
path @output @tag
}

span_: span @optional {
filename @output
begin_line @output
}
}
}
}
baseline {
item {
... on Enum {
visibility_limit @filter(op: "=", value: ["$public"])
name @filter(op: "=", value: ["%name"])

attrs @filter(op: "contains", value: ["$non_exhaustive"])

importable_path {
path @filter(op: "=", value: ["%path"])
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"non_exhaustive": "#[non_exhaustive]",
},
error_message: "A Public API enum is no longer #[non_exhaustive]. External code may assume all variants are known, causing breaking changes if new ones are added.",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: please pay close attention to capitalization and style and ensure everything matches existing lints. For example, we don't capitalize Public.

per_result_error_template: Some("enum {{name}} in {{span_filename}}:{{span_begin_line}}"),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,7 @@ add_lints!(
enum_tuple_variant_field_missing,
enum_tuple_variant_field_now_doc_hidden,
enum_unit_variant_changed_kind,
enum_unmarked_non_exhaustive,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind renaming this lint to follow the implicit convention for items that have stopped being ? For example, consider function_abi_no_longer_unwind.

enum_variant_added,
enum_variant_marked_deprecated,
enum_variant_marked_non_exhaustive,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/enum_unmarked_non_exhaustive/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "enum_unmarked_non_exhaustive"
version = "0.1.0"
edition = "2021"

[dependencies]
6 changes: 6 additions & 0 deletions test_crates/enum_unmarked_non_exhaustive/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![no_std]

pub enum SimpleEnum {
VariantA,
VariantB,
}
7 changes: 7 additions & 0 deletions test_crates/enum_unmarked_non_exhaustive/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "enum_unmarked_non_exhaustive"
version = "0.1.0"
edition = "2021"

[dependencies]
7 changes: 7 additions & 0 deletions test_crates/enum_unmarked_non_exhaustive/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![no_std]

#[non_exhaustive]
pub enum SimpleEnum {
VariantA,
VariantB,
}
42 changes: 42 additions & 0 deletions test_outputs/query_execution/enum_unmarked_non_exhaustive.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: src/query.rs
expression: "&query_execution_results"
---
{
"./test_crates/enum_repr_variant_discriminant_changed/": [
{
"name": String("DiscriminantIsChangedButAVariantIsNonExhaustive"),
"path": List([
String("enum_repr_variant_discriminant_changed"),
String("DiscriminantIsChangedButAVariantIsNonExhaustive"),
]),
"span_begin_line": Uint64(23),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
"./test_crates/enum_unmarked_non_exhaustive/": [
{
"name": String("SimpleEnum"),
"path": List([
String("enum_unmarked_non_exhaustive"),
String("SimpleEnum"),
]),
"span_begin_line": Uint64(3),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
"./test_crates/type_mismatched_generic_lifetimes/": [
{
"name": String("Either"),
"path": List([
String("type_mismatched_generic_lifetimes"),
String("Either"),
]),
"span_begin_line": Uint64(7),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}
Loading