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 trait_newly_sealed new lint #876

Merged
merged 2 commits into from
Aug 20, 2024
Merged
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
52 changes: 52 additions & 0 deletions src/lints/trait_newly_sealed.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
SemverQuery(
id: "trait_newly_sealed",
human_readable_name: "pub trait became sealed",
description: "A public trait became sealed, so users of this trait can not implement it anymore",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed"),
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 a great reference link!

query: r#"
{
CrateDiff {
baseline {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"]) @output
sealed @filter(op: "!=", value: ["$true"])

importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
}
}
}
current {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"])
sealed @filter(op: "=", value: ["$true"])
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,
},
error_message: "A publicly-visible trait became sealed, so downstream crates are no longer able to implement it",
per_result_error_template: Some("trait {{join \"::\" path}} 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 @@ -841,6 +841,7 @@ add_lints!(
trait_method_unsafe_removed,
trait_missing,
trait_must_use_added,
trait_newly_sealed,
trait_no_longer_object_safe,
trait_now_doc_hidden,
trait_removed_associated_constant,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/trait_newly_sealed/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_newly_sealed"
version = "0.1.0"
edition = "2021"

[dependencies]
24 changes: 24 additions & 0 deletions test_crates/trait_newly_sealed/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
mod private {
pub trait Sealed {}
pub struct Token;
}

// Public trait becomes sealed, should be reported
pub trait TraitBecomesSealed: private::Sealed {}

// Trait that was not publicly-visible becomes sealed, shouldnt get reported as its still private
trait TraitRemainsPrivateButSealed: private::Sealed {}

// Method on public trait becomes sealed, should be reported
pub trait TraitMethodBecomesSealed {
fn method(&self, _: private::Token);
}

// Trait that was not publicly-visible becomes public and sealed, shouldnt be
// reported
pub trait TraitBecomesPublicAndSealed: private::Sealed {}

// Trait becomes private and sealed. The fact that it's private dominates,
// and should be the only violation that's reported:
// Thus being newly sealed is not the main problem
trait TraitBecomesPrivateAndSealed: private::Sealed {}
7 changes: 7 additions & 0 deletions test_crates/trait_newly_sealed/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_newly_sealed"
version = "0.1.0"
edition = "2021"

[dependencies]
18 changes: 18 additions & 0 deletions test_crates/trait_newly_sealed/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Public trait becomes sealed, should be reported
pub trait TraitBecomesSealed {}

// Trait that was not publicly-visible becomes sealed, shouldnt get reported as its still private
trait TraitRemainsPrivateButSealed {}

// Method on public trait becomes sealed, should be reported
pub trait TraitMethodBecomesSealed {
fn method(&self);
}
// Trait that was not publicly-visible becomes public and sealed, shouldnt be
// reported
trait TraitBecomesPublicAndSealed {}

// Trait becomes private and sealed. The fact that it's private dominates,
// and should be the only violation that's reported:
// Thus being newly sealed is not the main problem
pub trait TraitBecomesPrivateAndSealed {}
12 changes: 12 additions & 0 deletions test_outputs/trait_missing.output.ron
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,16 @@
"visibility_limit": String("public"),
},
],
"./test_crates/trait_newly_sealed/": [
{
"name": String("TraitBecomesPrivateAndSealed"),
"path": List([
String("trait_newly_sealed"),
String("TraitBecomesPrivateAndSealed"),
]),
"span_begin_line": Uint64(18),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}
25 changes: 25 additions & 0 deletions test_outputs/trait_newly_sealed.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"./test_crates/trait_newly_sealed/": [
{
"name": String("TraitBecomesSealed"),
"path": List([
String("trait_newly_sealed"),
String("TraitBecomesSealed"),
]),
"span_begin_line": Uint64(7),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
{
"name": String("TraitMethodBecomesSealed"),
"path": List([
String("trait_newly_sealed"),
String("TraitMethodBecomesSealed"),
]),
"span_begin_line": Uint64(13),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}

Loading