Skip to content

Commit

Permalink
feat: add trait_added_supertrait (#892)
Browse files Browse the repository at this point in the history
* feat: add trait_added_supertrait

* review

* typo and std supertraits

* add std to core supertrait test

* bless tests

* Update src/lints/trait_added_supertrait.ron

Co-authored-by: Max Carr <[email protected]>

---------

Co-authored-by: Predrag Gruevski <[email protected]>
Co-authored-by: Max Carr <[email protected]>
  • Loading branch information
3 people authored Sep 21, 2024
1 parent 1a56407 commit 44aea25
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/lints/trait_added_supertrait.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
SemverQuery(
id: "trait_added_supertrait",
human_readable_name: "non-sealed trait added new supertraits",
description: "A non-sealed trait added one or more supertraits, which breaks downstream implementations of the trait",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#generic-bounds-tighten"),
query: r#"
{
CrateDiff {
current {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"])
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
supertrait {
supertrait: name @output @tag
}
span_: span @optional {
filename @output
begin_line @output
}
}
}
}
baseline {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"]) @output
sealed @filter(op: "!=", value: ["$true"])
importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "=", value: ["$true"])
}
supertrait @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
name @filter(op: "=", value: ["%supertrait"])
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true,
"zero": 0,
},
error_message: "A non-sealed trait added one or more supertraits, which breaks downstream implementations of the trait",
per_result_error_template: Some("trait {{join \"::\" path}} gained {{supertrait}} 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 @@ -1077,6 +1077,7 @@ add_lints!(
struct_pub_field_now_doc_hidden,
struct_repr_transparent_removed,
struct_with_pub_fields_changed_type,
trait_added_supertrait,
trait_associated_const_added,
trait_associated_const_default_removed,
trait_associated_const_now_doc_hidden,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/trait_added_supertrait/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_added_supertrait"
version = "0.1.0"
edition = "2021"

[dependencies]
23 changes: 23 additions & 0 deletions test_crates/trait_added_supertrait/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub trait TraitOne {}
pub trait TraitTwo {}

mod sealed {
pub trait Sealed {}
}

pub trait Unchanged {}
pub trait UnchangedSealed: sealed::Sealed {}

pub trait WillGainOne: TraitOne {}
pub trait WillGainOneSealed: TraitOne + sealed::Sealed {}

pub trait WillGainAnotherOne: TraitOne + TraitTwo {}
pub trait WillGainAnotherOneSealed: TraitOne + TraitTwo + sealed::Sealed {}

pub trait WillGainStdOne: Sync {}
pub trait WillGainStdTwo: core::fmt::Debug {}
pub trait WillGainStdThree: PartialEq {
fn make_me_non_object_safe() -> Self;
}

pub trait WillChangeStdToCore: core::fmt::Debug {}
7 changes: 7 additions & 0 deletions test_crates/trait_added_supertrait/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_added_supertrait"
version = "0.1.0"
edition = "2021"

[dependencies]
23 changes: 23 additions & 0 deletions test_crates/trait_added_supertrait/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub trait TraitOne {}
pub trait TraitTwo {}

mod sealed {
pub trait Sealed {}
}

pub trait Unchanged {}
pub trait UnchangedSealed: sealed::Sealed {}

pub trait WillGainOne {}
pub trait WillGainOneSealed: sealed::Sealed {}

pub trait WillGainAnotherOne: TraitOne {}
pub trait WillGainAnotherOneSealed: TraitOne + sealed::Sealed {}

pub trait WillGainStdOne {}
pub trait WillGainStdTwo {}
pub trait WillGainStdThree {
fn make_me_non_object_safe() -> Self;
}

pub trait WillChangeStdToCore: std::fmt::Debug {}
102 changes: 102 additions & 0 deletions test_outputs/trait_added_supertrait.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"./test_crates/trait_added_supertrait/": [
{
"path": List([
String("trait_added_supertrait"),
String("WillGainOne"),
]),
"span_begin_line": Uint64(11),
"span_filename": String("src/lib.rs"),
"supertrait": String("TraitOne"),
"visibility_limit": String("public"),
},
{
"path": List([
String("trait_added_supertrait"),
String("WillGainAnotherOne"),
]),
"span_begin_line": Uint64(14),
"span_filename": String("src/lib.rs"),
"supertrait": String("TraitTwo"),
"visibility_limit": String("public"),
},
{
"path": List([
String("trait_added_supertrait"),
String("WillGainStdOne"),
]),
"span_begin_line": Uint64(17),
"span_filename": String("src/lib.rs"),
"supertrait": String("Sync"),
"visibility_limit": String("public"),
},
{
"path": List([
String("trait_added_supertrait"),
String("WillGainStdTwo"),
]),
"span_begin_line": Uint64(18),
"span_filename": String("src/lib.rs"),
"supertrait": String("Debug"),
"visibility_limit": String("public"),
},
{
"path": List([
String("trait_added_supertrait"),
String("WillGainStdThree"),
]),
"span_begin_line": Uint64(19),
"span_filename": String("src/lib.rs"),
"supertrait": String("PartialEq"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_associated_const_added/": [
{
"path": List([
String("trait_associated_const_added"),
String("WillGainConstWithoutDefaultAndSeal"),
]),
"span_begin_line": Uint64(16),
"span_filename": String("src/lib.rs"),
"supertrait": String("Sealed"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_associated_type_added/": [
{
"path": List([
String("trait_associated_type_added"),
String("WillGainTypeWithoutDefaultAndSeal"),
]),
"span_begin_line": Uint64(19),
"span_filename": String("src/lib.rs"),
"supertrait": String("Sealed"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_method_added/": [
{
"path": List([
String("trait_method_added"),
String("WillGainMethodWithoutDefaultAndSeal"),
]),
"span_begin_line": Uint64(20),
"span_filename": String("src/lib.rs"),
"supertrait": String("Sealed"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_newly_sealed/": [
{
"path": List([
String("trait_newly_sealed"),
String("TraitBecomesSealed"),
]),
"span_begin_line": Uint64(7),
"span_filename": String("src/lib.rs"),
"supertrait": String("Sealed"),
"visibility_limit": String("public"),
},
],
}

0 comments on commit 44aea25

Please sign in to comment.