Skip to content

Commit

Permalink
feat: add trait_associated_type_default_removed lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnossiom committed Aug 27, 2024
1 parent bb542c4 commit 534c5dc
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/lints/trait_associated_type_default_removed.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
SemverQuery(
id: "trait_associated_type_default_removed",
human_readable_name: "non-sealed trait removed the default value for an associated type",
description: "A non-sealed trait associated type lost its default value, 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#trait-item-signature"),
query: r#"
{
CrateDiff {
current {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"])
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
associated_type {
associated_type: name @output @tag
has_default @filter(op: "!=", value: ["$true"]) @output
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"])
}
associated_type {
name @filter(op: "=", value: ["%associated_type"])
has_default @filter(op: "=", value: ["$true"])
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true,
},
error_message: "A non-sealed trait associated type lost its default value, which breaks downstream implementations of the trait",
per_result_error_template: Some("trait type {{join \"::\" path}}::{{associated_type}} 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 @@ -835,6 +835,7 @@ add_lints!(
struct_with_pub_fields_changed_type,
trait_associated_const_added,
trait_associated_const_now_doc_hidden,
trait_associated_type_default_removed,
trait_associated_type_now_doc_hidden,
trait_default_impl_removed,
trait_method_missing,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_associated_type_default_removed"
version = "0.1.0"
edition = "2021"

[dependencies]
25 changes: 25 additions & 0 deletions test_crates/trait_associated_type_default_removed/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mod sealed {
pub(crate) trait Sealed {}
}

pub trait WillLoseDefault {
type Foo;
}

pub trait WillLoseDefaultSealed: sealed::Sealed {
type Foo;
}

pub trait Unchanged {
type Foo = bool;
}
pub trait UnchangedSealed: sealed::Sealed {
type Foo = bool;
}

pub trait UnchangedNoDefault {
type Foo;
}
pub trait UnchangedNoDefaultSealed: sealed::Sealed {
type Foo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_associated_type_default_removed"
version = "0.1.0"
edition = "2021"

[dependencies]
25 changes: 25 additions & 0 deletions test_crates/trait_associated_type_default_removed/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mod sealed {
pub(crate) trait Sealed {}
}

pub trait WillLoseDefault {
type Foo = bool;
}

pub trait WillLoseDefaultSealed: sealed::Sealed {
type Foo = bool;
}

pub trait Unchanged {
type Foo = bool;
}
pub trait UnchangedSealed: sealed::Sealed {
type Foo = bool;
}

pub trait UnchangedNoDefault {
type Foo;
}
pub trait UnchangedNoDefaultSealed: sealed::Sealed {
type Foo;
}
5 changes: 5 additions & 0 deletions test_outputs/trait_associated_type_default_removed.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"./test_crates/trait_associated_type_default_removed/": [
// TODO
]
}

0 comments on commit 534c5dc

Please sign in to comment.