Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new lint: trait_method_marked_deprecated #1098

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/lints/trait_method_marked_deprecated.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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
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"])
}

method {
name @filter(op: "=", value: ["%method_name"])
public_api_eligible @filter(op: "=", value: ["$true"])
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]
51 changes: 51 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,51 @@
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);
}

// Hidden trait - changes to its methods should not be reported
#[doc(hidden)]
pub trait HiddenTrait {
#[deprecated]
fn method_becomes_deprecated(&self);
}

// Public trait with hidden method should not be reported
pub trait PublicTraitWithHiddenMethod {
#[doc(hidden)]
#[deprecated]
fn hidden_method_becomes_deprecated(&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]
39 changes: 39 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,39 @@
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);
}

// Hidden trait - changes to its methods should not be reported
#[doc(hidden)]
pub trait HiddenTrait {
fn method_becomes_deprecated(&self);
}

// Public trait with hidden method should not be reported
pub trait PublicTraitWithHiddenMethod {
#[doc(hidden)]
fn hidden_method_becomes_deprecated(&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"),
},
],
}
Loading