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 lint for union field missing case #729

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 67 additions & 0 deletions src/lints/union_field_missing.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
SemverQuery(
id: "union_field_missing",
human_readable_name: "pub union pub field is removed or renamed",
description: "pub union pub field is removed or renamed. No longer present under it's previous name, by whatever cause.",
required_update: Major,
reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#item-remove"),
query: r#"
{
CrateDiff {
baseline {
item {
... on Union {
visibility_limit @filter(op: "=", value: ["$public"])
union_name: name @output @tag

importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}

field {
field_name: name @output @tag
visibility_limit @filter(op: "=", value: ["$public"])
public_api_eligible @filter(op: "=", value: ["$true"])

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

importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "=", value: ["$true"])
}


obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
field @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
visibility_limit @filter(op: "=", value: ["$public"])
name @filter(op: "=", value: ["%field_name"])

}




obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true,
"zero": 0,
},
error_message: "A pub field in a pub union has been removed.",
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
per_result_error_template: Some("union {{union_name}} in file {{span_filename}}:{{span_begin_line}} missing field {{field_name}}"),
obi1kenobi marked this conversation as resolved.
Show resolved Hide resolved
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ macro_rules! add_lints {
}

add_lints!(
union_field_missing,
pub_static_now_doc_hidden,
function_abi_no_longer_unwind,
pub_module_level_const_now_doc_hidden,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/union_field_missing/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "union_field_missing"
version = "0.1.0"
edition = "2021"

[dependencies]
22 changes: 22 additions & 0 deletions test_crates/union_field_missing/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// private union shouldn't cause any breaking changes
union PrivateUnion {
f3: f32,
}


// pub union with private fields shouldn't cause any breaking changes
pub union PubUnionPrivateField {
f3: f32,
}


// pub union with pub fields renamed should cause breaking changes
pub union PubUnionPubFieldRenamed{
pub f1: u32,
pub f3: f32,
}

// pub union with pub fields removed should cause breaking changes
pub union PubUnionPubFieldRemoved{
pub f1: u32,
}
7 changes: 7 additions & 0 deletions test_crates/union_field_missing/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "union_field_missing"
version = "0.1.0"
edition = "2021"

[dependencies]
25 changes: 25 additions & 0 deletions test_crates/union_field_missing/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// private union shouldn't cause any breaking changes
union PrivateUnion {
f1: u32,
f2: f32,
}


// pub union with private fields shouldn't cause any breaking changes
pub union PubUnionPrivateField {
f1: u32,
f2: f32,
}


// pub union with pub fields renamed should cause breaking changes
pub union PubUnionPubFieldRenamed{
pub f1: u32,
pub f2: f32,
}

// pub union with pub fields renamed should cause breaking changes
pub union PubUnionPubFieldRemoved{
pub f1: u32,
pub f2: f32,
}
24 changes: 24 additions & 0 deletions test_outputs/union_field_missing.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"./test_crates/union_field_missing/": [
{
"field_name": String("f2"),
"path": List([
String("union_field_missing"),
String("PubUnionPubFieldRenamed"),
]),
"span_begin_line": Uint64(18),
"span_filename": String("src/lib.rs"),
"union_name": String("PubUnionPubFieldRenamed"),
},
{
"field_name": String("f2"),
"path": List([
String("union_field_missing"),
String("PubUnionPubFieldRemoved"),
]),
"span_begin_line": Uint64(24),
"span_filename": String("src/lib.rs"),
"union_name": String("PubUnionPubFieldRemoved"),
},
]
}
Loading