-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add trait_associated_const_default_removed lint (#888)
* feat: add trait_associated_const_default_removed lint * fix query * add lint docs * remove comments in tests
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test_crates/trait_associated_const_default_removed/new/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
test_crates/trait_associated_const_default_removed/new/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 7 additions & 0 deletions
7
test_crates/trait_associated_const_default_removed/old/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
test_crates/trait_associated_const_default_removed/old/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
test_outputs/trait_associated_const_default_removed.output.ron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
}, | ||
], | ||
} |