Skip to content

Commit

Permalink
new lint: trait_method_marked_deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-III committed Feb 3, 2025
1 parent 982c35b commit 6393141
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/lints/trait_method_marked_deprecated.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
SemverQuery(
id: "trait_method_marked_deprecated",
human_readable_name: "trait method #[deprecated] added",
description: "A trait method 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"])
}
method {
method_name: name @output @tag
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"])
}
method {
name @filter(op: "=", value: ["%method_name"])
deprecated @filter(op: "!=", value: ["$true"])
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"true": true,
},
error_message: "A trait method is now #[deprecated]. Downstream crates will get a compiler warning when using this method.",
per_result_error_template: Some("method {{method_name}} 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 @@ -1220,6 +1220,7 @@ add_lints!(
trait_marked_deprecated,
trait_method_added,
trait_method_default_impl_removed,
trait_method_marked_deprecated,
trait_method_missing,
trait_method_now_doc_hidden,
trait_method_parameter_count_changed,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/trait_method_marked_deprecated/new/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_method_marked_deprecated"
version = "0.1.0"
edition = "2021"

[dependencies]
37 changes: 37 additions & 0 deletions test_crates/trait_method_marked_deprecated/new/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub trait TraitWithMethodToBeDeprecated {
// These methods are now deprecated and should be reported
#[deprecated]
fn method_to_deprecated(&self) -> i32;

#[deprecated = "This method is deprecated"]
fn method_to_deprecated_message(&self, input: &str) -> String;

// These methods should not trigger the lint
fn method_stays_normal(&self);

#[deprecated]
fn method_already_deprecated(&self);

#[deprecated = "New message"]
fn method_message_changes(&self);
}

// Changes to private trait methods should not be reported
trait PrivateTrait {
#[deprecated]
fn private_method_to_deprecated(&self);
}

#[deprecated]
pub trait DeprecatedTrait {
// Adding deprecated to a method in an already deprecated trait should not be reported
#[deprecated]
fn method_in_deprecated_trait(&self);
}

// This trait was added in the new version with deprecated methods.
// It should NOT be reported by this rule to avoid duplicate lints.
pub trait NewTraitWithDeprecatedMethod {
#[deprecated]
fn new_deprecated_method(&self);
}
7 changes: 7 additions & 0 deletions test_crates/trait_method_marked_deprecated/old/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
publish = false
name = "trait_method_marked_deprecated"
version = "0.1.0"
edition = "2021"

[dependencies]
27 changes: 27 additions & 0 deletions test_crates/trait_method_marked_deprecated/old/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub trait TraitWithMethodToBeDeprecated {
// This method will be marked deprecated
fn method_to_deprecated(&self) -> i32;

// This method will be marked deprecated with a message
fn method_to_deprecated_message(&self, input: &str) -> String;

// These methods should not trigger the lint
fn method_stays_normal(&self);

#[deprecated]
fn method_already_deprecated(&self);

#[deprecated = "Old message"]
fn method_message_changes(&self);
}

// Changes to private trait methods should not be reported
trait PrivateTrait {
fn private_method_to_deprecated(&self);
}

#[deprecated]
pub trait DeprecatedTrait {
// Adding deprecated to a method in an already deprecated trait should not be reported
fn method_in_deprecated_trait(&self);
}
54 changes: 54 additions & 0 deletions test_outputs/query_execution/trait_method_marked_deprecated.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
source: src/query.rs
expression: "&query_execution_results"
---
{
"./test_crates/trait_marked_deprecated/": [
{
"method_name": String("method"),
"name": String("TraitToDeprecatedTrait"),
"path": List([
String("trait_marked_deprecated"),
String("TraitToDeprecatedTrait"),
]),
"span_begin_line": Uint64(5),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("method"),
"name": String("TraitToDeprecatedMessageTrait"),
"path": List([
String("trait_marked_deprecated"),
String("TraitToDeprecatedMessageTrait"),
]),
"span_begin_line": Uint64(10),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
"./test_crates/trait_method_marked_deprecated/": [
{
"method_name": String("method_to_deprecated"),
"name": String("TraitWithMethodToBeDeprecated"),
"path": List([
String("trait_method_marked_deprecated"),
String("TraitWithMethodToBeDeprecated"),
]),
"span_begin_line": Uint64(1),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
{
"method_name": String("method_to_deprecated_message"),
"name": String("TraitWithMethodToBeDeprecated"),
"path": List([
String("trait_method_marked_deprecated"),
String("TraitWithMethodToBeDeprecated"),
]),
"span_begin_line": Uint64(1),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}

0 comments on commit 6393141

Please sign in to comment.