Skip to content

Commit

Permalink
new lint: trait_associated_const_marked_deprecated (#1100)
Browse files Browse the repository at this point in the history
part of: #57
  • Loading branch information
Frank-III authored Feb 4, 2025
1 parent 4b47eb8 commit 3bd958d
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/lints/trait_associated_const_marked_deprecated.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
SemverQuery(
id: "trait_associated_const_marked_deprecated",
human_readable_name: "trait associated constant #[deprecated] added",
description: "A trait's associated constant has been newly marked with #[deprecated].",
required_update: Minor,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute"),
query: r#"
{
CrateDiff {
current {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"])
name @output
importable_path {
path @tag @output
public_api @filter(op: "=", value: ["$true"])
}
associated_constant {
associated_constant: name @output @tag
public_api_eligible @filter(op: "=", value: ["$true"])
deprecated @filter(op: "=", value: ["$true"])
span_: span @optional {
filename @output
begin_line @output
}
}
}
}
}
baseline {
item {
... on Trait {
visibility_limit @filter(op: "=", value: ["$public"]) @output
importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "=", value: ["$true"])
}
associated_constant {
name @filter(op: "=", value: ["%associated_constant"])
public_api_eligible @filter(op: "=", value: ["$true"])
deprecated @filter(op: "!=", value: ["$true"])
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true,
},
error_message: "A trait associated constant is now #[deprecated]. Downstream crates will get a compiler warning when using it.",
per_result_error_template: Some("associated constant {{associated_constant}} in trait {{join \"::\" path}} in {{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 @@ -1214,6 +1214,7 @@ add_lints!(
trait_allows_fewer_generic_type_params,
trait_associated_const_added,
trait_associated_const_default_removed,
trait_associated_const_marked_deprecated,
trait_associated_const_now_doc_hidden,
trait_associated_type_added,
trait_associated_type_default_removed,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_associated_const_marked_deprecated"
version = "0.1.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pub trait TraitWithConstToBeDeprecated {
// These constants are now deprecated and should be reported
#[deprecated]
const CONST_TO_DEPRECATED: i32 = 42;

#[deprecated = "This constant is deprecated"]
const CONST_TO_DEPRECATED_MESSAGE: &'static str = "hello";

// These constants should not trigger the lint
const CONST_STAYS_NORMAL: bool = true;

#[deprecated]
const CONST_ALREADY_DEPRECATED: u32 = 100;

#[deprecated = "New message"]
const CONST_MESSAGE_CHANGES: char = 'x';
}

// Changes to private trait constants should not be reported
trait PrivateTrait {
#[deprecated]
const PRIVATE_CONST_TO_DEPRECATED: i32 = 0;
}

#[deprecated]
pub trait DeprecatedTrait {
// Adding deprecated to a constant in an already deprecated trait should not be reported
#[deprecated]
const CONST_IN_DEPRECATED_TRAIT: i32 = 1;
}

// Hidden trait - changes to its constants should not be reported
#[doc(hidden)]
pub trait HiddenTrait {
#[deprecated]
const CONST_BECOMES_DEPRECATED: i32 = 2;
}

// Public trait with hidden constant should not be reported
pub trait PublicTraitWithHiddenConst {
#[doc(hidden)]
#[deprecated]
const HIDDEN_CONST_BECOMES_DEPRECATED: i32 = 3;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_associated_const_marked_deprecated"
version = "0.1.0"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub trait TraitWithConstToBeDeprecated {
// This associated constant will be marked deprecated
const CONST_TO_DEPRECATED: i32 = 42;

// This associated constant will be marked deprecated with a message
const CONST_TO_DEPRECATED_MESSAGE: &'static str = "hello";

// These constants should not trigger the lint
const CONST_STAYS_NORMAL: bool = true;

#[deprecated]
const CONST_ALREADY_DEPRECATED: u32 = 100;

#[deprecated = "Old message"]
const CONST_MESSAGE_CHANGES: char = 'x';
}

// Changes to private trait constants should not be reported
trait PrivateTrait {
const PRIVATE_CONST_TO_DEPRECATED: i32 = 0;
}

#[deprecated]
pub trait DeprecatedTrait {
// Adding deprecated to a constant in an already deprecated trait should not be reported
const CONST_IN_DEPRECATED_TRAIT: i32 = 1;
}

// Hidden trait - changes to its constants should not be reported
#[doc(hidden)]
pub trait HiddenTrait {
const CONST_BECOMES_DEPRECATED: i32 = 2;
}

// Public trait with hidden constant should not be reported
pub trait PublicTraitWithHiddenConst {
#[doc(hidden)]
const HIDDEN_CONST_BECOMES_DEPRECATED: i32 = 3;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: src/query.rs
expression: "&query_execution_results"
---
{
"./test_crates/trait_associated_const_marked_deprecated/": [
{
"associated_constant": String("CONST_TO_DEPRECATED"),
"name": String("TraitWithConstToBeDeprecated"),
"path": List([
String("trait_associated_const_marked_deprecated"),
String("TraitWithConstToBeDeprecated"),
]),
"span_begin_line": Uint64(4),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
{
"associated_constant": String("CONST_TO_DEPRECATED_MESSAGE"),
"name": String("TraitWithConstToBeDeprecated"),
"path": List([
String("trait_associated_const_marked_deprecated"),
String("TraitWithConstToBeDeprecated"),
]),
"span_begin_line": Uint64(7),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}

0 comments on commit 3bd958d

Please sign in to comment.