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

feat: add lint struct_with_no_pub_fields_changed #962

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
76 changes: 76 additions & 0 deletions src/lints/struct_with_no_pub_fields_changed.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
SemverQuery(
id: "struct_with_no_pub_fields_changed",
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
human_readable_name: "public API struct with no public fields is no longer a struct",
description: "A struct without pub fields was converted into an enum or union, breaking pattern matching.",
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
required_update: Major,
lint_level: Deny,
reference_link: Some("https://internals.rust-lang.org/t/rest-patterns-foo-should-match-non-struct-types/21607"),
reference: Some(
r#"\
Even if a structs does not expose pub fields, pattern matching like `matches!(value, Example { .. })` is accessible outside their defining crate. \
Changing such a struct to make it an enum or union will break this pattern match, as their syntax is different.
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved

More info: https://github.com/obi1kenobi/cargo-semver-checks/issues/954
"#
),
query: r#"
{
CrateDiff {
baseline {
item {
... on Struct {
struct_typename: __typename @tag @output
visibility_limit @filter(op: "=", value: ["$public"]) @output
struct_type @output

# Ensure the struct does not have pub fields to prevent overlap with struct_with_pub_fields_changed
field @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
visibility_limit @filter(op: "=", value: ["$public"])
public_api_eligible @filter(op: "=", value: ["$true"])
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
}

# Ensure the struct does have non-pub fields to prevent overlap with constructible_struct_changed_type
field @fold @transform(op: "count") @filter(op: ">", value: ["$zero"]) {
visibility_limit @filter(op: "!=", value: ["$public"])
}

importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
}
}
}
current {
item {
... on ImplOwner {
current_typename: __typename @filter(op: "!=", value: ["%struct_typename"])
@output
visibility_limit @filter(op: "=", value: ["$public"])
name @output

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

span_: span @optional {
filename @output
begin_line @output
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true,
"zero": 0,
},
error_message: "A struct without pub fields became an enum or union, breaking pattern matching.",
per_result_error_template: Some("struct {{join \"::\" path}} became {{lowercase current_typename}} in file {{span_filename}}:{{span_begin_line}}"),
witness: (
hint_template: r#"matches!(value, {{join "::" path}}{...});"#,
),
)
4 changes: 2 additions & 2 deletions src/lints/struct_with_pub_fields_changed_type.ron
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SemverQuery(
id: "struct_with_pub_fields_changed_type",
human_readable_name: "struct with pub fields became an enum or union",
description: "A struct was converted into an enum or union, breaking accesses to its fields.",
description: "A struct with pub fields was converted into an enum or union, breaking accesses to its public fields.",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://github.com/obi1kenobi/cargo-semver-checks/issues/297#issuecomment-1399099659"),
Expand Down Expand Up @@ -64,6 +64,6 @@ More info: https://github.com/obi1kenobi/cargo-semver-checks/issues/297
"true": true,
"zero": 0,
},
error_message: "A struct became an enum or union, breaking accesses to its public fields.",
error_message: "A struct with pub fields became an enum or union, breaking accesses to its public fields.",
per_result_error_template: Some("struct {{join \"::\" path}} became {{lowercase current_typename}} in file {{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 @@ -1065,6 +1065,7 @@ add_lints!(
struct_pub_field_now_doc_hidden,
struct_repr_transparent_removed,
struct_with_pub_fields_changed_type,
struct_with_no_pub_fields_changed,
trait_added_supertrait,
trait_associated_const_added,
trait_associated_const_default_removed,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/struct_with_no_pub_fields_changed/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "struct_with_no_pub_fields_changed"
version = "0.1.0"
edition = "2021"

[dependencies]
104 changes: 104 additions & 0 deletions test_crates/struct_with_no_pub_fields_changed/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
pub enum PubStructChangedToEnum {
Foo(usize),
}

pub union PubStructChangedToUnion {
foo: usize
}

pub enum PubStructWithNonPubDocFieldChangedToEnum {
Foo(usize),
}


pub union PubStructWithNonPubDocFieldChangedToUnion {
/// Despite this field being pub, hiding it makes this not be `public_api_eligible` anymore
/// This struct should trigger `struct_with_no_pub_fields_changed` instead of `struct_with_pub_fields_changed`
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
#[doc(hidden)]
pub foo: usize,
}

pub enum PubStructWithNonPubDocFieldAndNonPubFieldChangedToEnum {
Foo(usize),
Bar(usize),
}


pub union PubStructWithNonPubDocFieldAndNonPubFieldChangedToUnion {
foo: usize,
/// Despite this field being pub, hiding it makes this not be `public_api_eligible` anymore
/// This struct should trigger `struct_with_no_pub_fields_changed` instead of `struct_with_pub_fields_changed`
#[doc(hidden)]
pub bar: usize,
}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// The struct is not `public_api_eligible`.
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub enum NonPubDocStructChangedToEnum {
Foo(usize),
}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// The struct is not `public_api_eligible`.
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub union NonPubDocStructChangedToUnion {
foo: usize,
}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `struct_with_pub_fields_changed` instead
pub enum PubStructWithPubFieldChangedToEnum {
Foo(usize),
Bar(usize),
}


/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `struct_with_pub_fields_changed` instead
pub union PubStructWithPubFieldChangedToUnion {
foo: usize,
pub bar: usize,

}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `struct_missing` instead
pub type PubStructChangedToType = u8;

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `constructible_struct_changed_type` instead
pub enum PubStructChangedToNoFieldsEnum {}

/// This struct should not be reported by the `struct_pub_field_missing` rule:
/// since the struct is not pub in the first place, changing it does not change the API
enum NonPubStructChangedToEnum {
Foo(usize),
}

/// This struct should not be reported by the `struct_pub_field_missing` rule:
/// since the struct is not pub in the first place, changing it does not change the API
union NonPubStructChangedToUnion {
foo: usize
}

type NonPubStructChangedToType = u8;

mod not_pub_visible {
/// This struct should not be reported by the `struct_pub_field_missing` rule:
/// since the struct is not in a pub module, changing it does not change the API
pub enum NonReachableStructChangedToEnum {
Foo(usize),
}
/// This struct should not be reported by the `struct_pub_field_missing` rule:
/// since the struct is not in a pub module, changing it does not change the API
pub union NonReachableStructChangedToUnion {
foo: usize
}
/// This struct should not be reported by the `struct_pub_field_missing` rule:
/// since the struct is not in a pub module, changing it does not change the API
pub type NonReachableStructChangedToType = u8;
}

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

[dependencies]
119 changes: 119 additions & 0 deletions test_crates/struct_with_no_pub_fields_changed/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
pub struct PubStructChangedToEnum {
foo: usize,
}

pub struct PubStructChangedToUnion {
foo: usize,
}

pub struct PubStructWithNonPubDocFieldChangedToEnum {
/// Despite this field being pub, hiding it makes this not be `public_api_eligible` anymore
/// This struct should trigger `struct_with_no_pub_fields_changed` instead of `struct_with_pub_fields_changed`
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub foo: usize,
}


pub struct PubStructWithNonPubDocFieldChangedToUnion {
/// Despite this field being pub, hiding it makes this not be `public_api_eligible` anymore
/// This struct should trigger `struct_with_no_pub_fields_changed` instead of `struct_with_pub_fields_changed`
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub foo: usize,
}

pub struct PubStructWithNonPubDocFieldAndNonPubFieldChangedToEnum {
foo: usize,
/// Despite this field being pub, hiding it makes this not be `public_api_eligible` anymore
/// This struct should trigger `struct_with_no_pub_fields_changed` instead of `struct_with_pub_fields_changed`
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub bar: usize,
}


pub struct PubStructWithNonPubDocFieldAndNonPubFieldChangedToUnion {
foo: usize,
/// Despite this field being pub, hiding it makes this not be `public_api_eligible` anymore
/// This struct should trigger `struct_with_no_pub_fields_changed` instead of `struct_with_pub_fields_changed`
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub bar: usize,
}



/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// The struct is not `public_api_eligible`.
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub struct NonPubDocStructChangedToEnum {
foo: usize,
}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// The struct is not `public_api_eligible`.
/// See https://predr.ag/blog/checking-semver-for-doc-hidden-items/ for additional context
#[doc(hidden)]
pub struct NonPubDocStructChangedToUnion {
foo: usize,
}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `struct_with_pub_fields_changed` instead
pub struct PubStructWithPubFieldChangedToEnum {
foo: usize,
pub bar: usize,
}


/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `struct_with_pub_fields_changed` instead
pub struct PubStructWithPubFieldChangedToUnion {
foo: usize,
pub bar: usize,

}


/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `struct_missing` instead
pub struct PubStructChangedToType(u8);

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// This is `constructible_struct_changed_type` instead
pub struct PubStructChangedToNoFieldsEnum {}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// since the struct is not pub in the first place, changing it does not change the API
struct NonPubStructChangedToEnum {
foo: usize,
}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// since the struct is not pub in the first place, changing it does not change the API
struct NonPubStructChangedToUnion {
foo: usize,
}

/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// since the struct is not pub in the first place, changing it does not change the API
struct NonPubStructChangedToType(u8);

mod not_pub_visible {
/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// since the struct is not in a pub module, changing it does not change the API
pub struct NonReachableStructChangedToEnum {
foo: usize,
}
/// This struct should not be reported by the `struct_with_no_pub_fields_changed` rule:
/// since the struct is not in a pub module, changing it does not change the API
pub struct NonReachableStructChangedToUnion {
foo: usize,
}
/// This struct should not be reported by the `struct_pub_field_missing` rule:
/// since the struct is not in a pub module, changing it does not change the API
pub struct NonReachableStructChangedToType(u8);
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,19 @@ expression: "&query_execution_results"
"visibility_limit": String("public"),
},
],
"./test_crates/struct_with_no_pub_fields_changed/": [
{
"current_typename": String("Enum"),
"name": String("PubStructChangedToNoFieldsEnum"),
"path": List([
String("struct_with_no_pub_fields_changed"),
String("PubStructChangedToNoFieldsEnum"),
]),
"span_begin_line": Uint64(73),
"span_filename": String("src/lib.rs"),
"struct_type": String("plain"),
"struct_typename": String("Struct"),
"visibility_limit": String("public"),
},
],
}
13 changes: 13 additions & 0 deletions test_outputs/query_execution/struct_missing.snap
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ expression: "&query_execution_results"
"visibility_limit": String("public"),
},
],
"./test_crates/struct_with_no_pub_fields_changed/": [
{
"name": String("PubStructChangedToType"),
"path": List([
String("struct_with_no_pub_fields_changed"),
String("PubStructChangedToType"),
]),
"span_begin_line": Uint64(82),
"span_filename": String("src/lib.rs"),
"struct_type": String("tuple"),
"visibility_limit": String("public"),
},
],
"./test_crates/switch_to_reexport_as_underscore/": [
{
"name": String("Struct"),
Expand Down
Loading
Loading