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

Implement new_struct_added lint #495

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions src/lints/new_struct_added.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SemverQuery(
id: "new_struct_added",
human_readable_name: "new struct added",
description: "A new public struct has been exposed.",
required_update: Minor,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
required_update: Minor,
required_update: Minor,
lint_level: Allow,

or

Suggested change
required_update: Minor,
required_update: Minor,
lint_level: Warn,

With the new version of cargo-semver-checks, it is now possible to have allow-by-default and warn-by-default checks.

Copy link
Owner

Choose a reason for hiding this comment

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

I'd probably make this Allow by default. I'm sure many users will want to opt-in, but it might be a bit noisy to enable for everyone.


// TODO: Change the reference link to point to the cargo semver reference
// once it has a section on attribute #[must_use].
reference_link: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"),
Comment on lines +7 to +9
Copy link
Owner

Choose a reason for hiding this comment

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

This seems like copy-pasta, change it to None for now.

query: r#"
{
CrateDiff {
current {
item {
... on Struct {
name @tag(name: "new_crate_name") @output(name: "new_struct_added_name")
visibility_limit @filter(op: "=", value: ["$public"])

span_: span {
filename @output
begin_line @output
}
}
}
}

baseline {
item @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
... on Struct {
name @filter(op: "=", value: ["%new_crate_name"])
visibility_limit @filter(op: "=", value: ["$public"])
}
}
}
Comment on lines +14 to +34
Copy link
Owner

Choose a reason for hiding this comment

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

This will actually also fire on structs that are just moved, renamed, and re-exported in their original place under their new name, which is a false-positive unless there's a new name in the public API.

You'll need to use importable_path to avoid that.

}
}"#,
arguments: {
"public": "public",
"zero": 0,
},
error_message: "A new struct has been added to externally exposed API, this requires a minor version bump.",
per_result_error_template: Some("struct {{new_struct_added_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 @@ -501,4 +501,5 @@ add_lints!(
enum_tuple_variant_field_missing,
enum_tuple_variant_field_added,
trait_removed_supertrait,
new_struct_added,
);
7 changes: 7 additions & 0 deletions test_crates/new_struct_added/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "new_struct_added"
version = "0.1.0"
edition = "2021"

[dependencies]
2 changes: 2 additions & 0 deletions test_crates/new_struct_added/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
struct A;
pub struct B;
7 changes: 7 additions & 0 deletions test_crates/new_struct_added/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "new_struct_added"
version = "0.1.0"
edition = "2021"

[dependencies]
1 change: 1 addition & 0 deletions test_crates/new_struct_added/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
struct A;
66 changes: 66 additions & 0 deletions test_outputs/new_struct_added.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"./test_crates/inherent_method_must_use_added/": [
{
"new_struct_added_name": String("EnumToStructWithMustUseMethods"),
"span_begin_line": Uint64(8),
"span_filename": String("src/item_type_changed_inherent_method_must_use_added.rs"),
},
{
"new_struct_added_name": String("UnionToStructWithMustUseMethods"),
"span_begin_line": Uint64(141),
"span_filename": String("src/item_type_changed_inherent_method_must_use_added.rs"),
},
{
"new_struct_added_name": String("NewStructWithMustUseMethods"),
"span_begin_line": Uint64(68),
"span_filename": String("src/struct_inherent_method_must_use_added.rs"),
},
],
"./test_crates/method_moved_to_trait_must_use_added/": [
{
"new_struct_added_name": String("NewStructWithTraitMustUseMethods"),
"span_begin_line": Uint64(147),
"span_filename": String("src/struct_method_moved_to_trait_must_use_added.rs"),
},
],
"./test_crates/move_item_and_reexport/": [
{
"new_struct_added_name": String("HasBeenRenamedTypedefReexport"),
"span_begin_line": Uint64(25),
"span_filename": String("src/lib.rs"),
},
{
"new_struct_added_name": String("HasBeenRenamedTypedefWithGenericsReexport"),
"span_begin_line": Uint64(27),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/new_struct_added/": [
{
"new_struct_added_name": String("B"),
"span_begin_line": Uint64(2),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/nonbreaking_item_rename/": [
{
"new_struct_added_name": String("NewName"),
"span_begin_line": Uint64(2),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/struct_must_use_added/": [
{
"new_struct_added_name": String("MustUseNewStruct"),
"span_begin_line": Uint64(60),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/type_marked_deprecated/": [
{
"new_struct_added_name": String("DeprecatedNewStruct"),
"span_begin_line": Uint64(57),
"span_filename": String("src/structs.rs"),
},
],
}
Copy link
Owner

Choose a reason for hiding this comment

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

This is lacking a trailing newline, please configure your editor to add them automatically.

Loading