Skip to content

Commit

Permalink
feat: add trait_associated_const_default_removed lint (#888)
Browse files Browse the repository at this point in the history
* feat: add trait_associated_const_default_removed lint

* fix query

* add lint docs

* remove comments in tests
  • Loading branch information
mrnossiom authored Aug 27, 2024
1 parent bb542c4 commit 0f91c74
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/lints/trait_associated_const_default_removed.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
SemverQuery(
id: "trait_associated_const_default_removed",
human_readable_name: "non-sealed trait removed the default value for an associated constant",
description: "A non-sealed trait associated constant 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_constant {
associated_constant: name @output @tag
default @filter(op: "is_null") @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_constant {
name @filter(op: "=", value: ["%associated_constant"])
default @filter(op: "is_not_null")
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true,
},
error_message: "A non-sealed trait associated constant lost its default value, which breaks downstream implementations of the trait",
per_result_error_template: Some("trait constant {{join \"::\" path}}::{{associated_constant}} 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 @@ -834,6 +834,7 @@ add_lints!(
struct_repr_transparent_removed,
struct_with_pub_fields_changed_type,
trait_associated_const_added,
trait_associated_const_default_removed,
trait_associated_const_now_doc_hidden,
trait_associated_type_now_doc_hidden,
trait_default_impl_removed,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_associated_const_default_removed"
version = "0.1.0"
edition = "2021"

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

pub trait WillLoseDefault {
const ONE: bool;
fn make_me_non_object_safe() -> Self;
}

pub trait WillLoseDefaultSealed: sealed::Sealed {
const ONE: bool;

fn make_me_non_object_safe() -> Self;
}

pub trait Unchanged {
const ONE: bool = true;

fn make_me_non_object_safe() -> Self;
}
pub trait UnchangedSealed: sealed::Sealed {
const ONE: bool = true;

fn make_me_non_object_safe() -> Self;
}

pub trait UnchangedNoDefault {
const ONE: bool;

fn make_me_non_object_safe() -> Self;
}
pub trait UnchangedNoDefaultSealed: sealed::Sealed {
const ONE: bool;

fn make_me_non_object_safe() -> Self;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_associated_const_default_removed"
version = "0.1.0"
edition = "2021"

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

pub trait WillLoseDefault {
const ONE: bool = true;

fn make_me_non_object_safe() -> Self;
}

pub trait WillLoseDefaultSealed: sealed::Sealed {
const ONE: bool = true;

fn make_me_non_object_safe() -> Self;
}

pub trait Unchanged {
const ONE: bool = true;

fn make_me_non_object_safe() -> Self;
}
pub trait UnchangedSealed: sealed::Sealed {
const ONE: bool = true;

fn make_me_non_object_safe() -> Self;
}

pub trait UnchangedNoDefault {
const ONE: bool;

fn make_me_non_object_safe() -> Self;
}
pub trait UnchangedNoDefaultSealed: sealed::Sealed {
const ONE: bool;

fn make_me_non_object_safe() -> Self;
}
28 changes: 28 additions & 0 deletions test_outputs/trait_associated_const_default_removed.output.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"./test_crates/trait_associated_const_default_removed/": [
{
"associated_constant": String("ONE"),
"default": Null,
"path": List([
String("trait_associated_const_default_removed"),
String("WillLoseDefault"),
]),
"span_begin_line": Uint64(6),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_items_hidden_from_public_api/": [
{
"associated_constant": String("DEFAULT_REMOVED"),
"default": Null,
"path": List([
String("trait_items_hidden_from_public_api"),
String("NonSealed"),
]),
"span_begin_line": Uint64(10),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}

0 comments on commit 0f91c74

Please sign in to comment.