Skip to content

Commit 356dfad

Browse files
authored
new lint: type_method_marked_deprecated (#1130)
part of: #57
1 parent 9bc897f commit 356dfad

File tree

14 files changed

+811
-0
lines changed

14 files changed

+811
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
SemverQuery(
2+
id: "type_method_marked_deprecated",
3+
human_readable_name: "type method #[deprecated] added",
4+
description: "An inherent method has been newly marked with #[deprecated].",
5+
required_update: Minor,
6+
lint_level: Deny,
7+
reference_link: Some("https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute"),
8+
query: r#"
9+
{
10+
CrateDiff {
11+
current {
12+
item {
13+
... on ImplOwner {
14+
visibility_limit @filter(op: "=", value: ["$public"])
15+
name @output
16+
owner_type: __typename @tag @output
17+
18+
importable_path {
19+
path @tag @output
20+
public_api @filter(op: "=", value: ["$true"])
21+
}
22+
23+
inherent_impl {
24+
public_api_eligible @filter(op: "=", value: ["$true"])
25+
26+
method {
27+
method_name: name @output @tag
28+
public_api_eligible @filter(op: "=", value: ["$true"])
29+
deprecated @filter(op: "=", value: ["$true"])
30+
31+
span_: span @optional {
32+
filename @output
33+
begin_line @output
34+
end_line @output
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
baseline {
42+
item {
43+
... on ImplOwner {
44+
visibility_limit @filter(op: "=", value: ["$public"])
45+
__typename @filter(op: "=", value: ["%owner_type"])
46+
47+
importable_path {
48+
path @filter(op: "=", value: ["%path"])
49+
public_api @filter(op: "=", value: ["$true"])
50+
}
51+
52+
inherent_impl {
53+
public_api_eligible @filter(op: "=", value: ["$true"])
54+
55+
method {
56+
name @filter(op: "=", value: ["%method_name"])
57+
public_api_eligible @filter(op: "=", value: ["$true"])
58+
deprecated @filter(op: "!=", value: ["$true"])
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}"#,
66+
arguments: {
67+
"public": "public",
68+
"true": true,
69+
},
70+
error_message: "A type method is now #[deprecated]. Downstream crates will get a compiler warning when using this method.",
71+
per_result_error_template: Some("method {{join \"::\" path}}::{{method_name}} in {{span_filename}}:{{span_begin_line}}"),
72+
)

src/query.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,7 @@ add_lints!(
12521252
type_allows_fewer_const_generic_params,
12531253
type_allows_fewer_generic_type_params,
12541254
type_marked_deprecated,
1255+
type_method_marked_deprecated,
12551256
type_mismatched_generic_lifetimes,
12561257
type_requires_more_const_generic_params,
12571258
type_requires_more_generic_type_params,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
publish = false
3+
name = "type_method_marked_deprecated"
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
pub enum NormalEnum {
2+
A,
3+
B,
4+
}
5+
6+
impl NormalEnum {
7+
#[deprecated]
8+
pub fn method_to_be_deprecated(&self) -> bool {
9+
matches!(self, NormalEnum::A)
10+
}
11+
12+
#[deprecated = "Use to_string instead"]
13+
pub fn method_with_message_to_be_deprecated(&self) -> String {
14+
format!("{:?}", self)
15+
}
16+
17+
#[deprecated]
18+
pub fn already_deprecated_method(&self) {}
19+
20+
pub fn stays_normal(&self) {}
21+
}
22+
23+
#[doc(hidden)]
24+
pub enum HiddenEnum {
25+
A,
26+
}
27+
28+
impl HiddenEnum {
29+
#[deprecated]
30+
pub fn method_to_be_deprecated(&self) {}
31+
}
32+
33+
pub enum EnumWithHiddenImpl {
34+
A,
35+
}
36+
37+
#[doc(hidden)]
38+
impl EnumWithHiddenImpl {
39+
#[deprecated]
40+
pub fn method_to_be_deprecated(&self) {}
41+
}
42+
43+
pub enum EnumWithHiddenMethod {
44+
A,
45+
}
46+
47+
impl EnumWithHiddenMethod {
48+
#[doc(hidden)]
49+
#[deprecated]
50+
pub fn method_to_be_deprecated(&self) {}
51+
}
52+
53+
enum PrivateEnum {
54+
A,
55+
}
56+
57+
impl PrivateEnum {
58+
#[deprecated]
59+
pub fn method_to_be_deprecated(&self) {}
60+
}
61+
62+
// Enum is deprecated but methods remain unchanged
63+
#[deprecated = "Use NewEnum instead"]
64+
pub enum EnumBecomesDeprecated {
65+
A,
66+
B,
67+
}
68+
69+
impl EnumBecomesDeprecated {
70+
// Methods intentionally not deprecated
71+
pub fn method_stays_normal(&self) -> bool {
72+
matches!(self, EnumBecomesDeprecated::A)
73+
}
74+
75+
pub fn another_normal_method(&self) -> String {
76+
format!("{:?}", self)
77+
}
78+
}
79+
80+
// Both enum and methods are deprecated
81+
#[deprecated]
82+
pub enum BothEnumAndMethodDeprecated {
83+
A,
84+
}
85+
86+
impl BothEnumAndMethodDeprecated {
87+
#[deprecated]
88+
pub fn method_to_be_deprecated(&self) {}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod enums;
2+
pub mod structs;
3+
pub mod unions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
pub struct NormalStruct {
2+
value: i32,
3+
}
4+
5+
impl NormalStruct {
6+
#[deprecated]
7+
pub fn method_to_be_deprecated(&self) -> i32 {
8+
self.value
9+
}
10+
11+
#[deprecated = "Use new_method instead"]
12+
pub fn method_with_message_to_be_deprecated(&self) -> String {
13+
"hello".to_string()
14+
}
15+
16+
#[deprecated]
17+
pub fn already_deprecated_method(&self) {}
18+
19+
pub fn stays_normal(&self) {}
20+
}
21+
22+
#[doc(hidden)]
23+
pub struct HiddenStruct {
24+
value: i32,
25+
}
26+
27+
impl HiddenStruct {
28+
#[deprecated]
29+
pub fn method_to_be_deprecated(&self) {}
30+
}
31+
32+
pub struct StructWithHiddenImpl {
33+
value: i32,
34+
}
35+
36+
#[doc(hidden)]
37+
impl StructWithHiddenImpl {
38+
#[deprecated]
39+
pub fn method_to_be_deprecated(&self) {}
40+
}
41+
42+
pub struct StructWithHiddenMethod {
43+
value: i32,
44+
}
45+
46+
impl StructWithHiddenMethod {
47+
#[doc(hidden)]
48+
#[deprecated]
49+
pub fn method_to_be_deprecated(&self) {}
50+
}
51+
52+
struct PrivateStruct {
53+
value: i32,
54+
}
55+
56+
impl PrivateStruct {
57+
#[deprecated]
58+
pub fn method_to_be_deprecated(&self) {}
59+
}
60+
61+
// Struct is deprecated but methods remain unchanged
62+
#[deprecated = "Use NewStruct instead"]
63+
pub struct StructBecomesDeprecated {
64+
value: i32,
65+
}
66+
67+
impl StructBecomesDeprecated {
68+
// Methods intentionally not deprecated
69+
pub fn method_stays_normal(&self) -> i32 {
70+
self.value
71+
}
72+
73+
pub fn another_normal_method(&self) -> String {
74+
"hello".to_string()
75+
}
76+
}
77+
78+
// Both struct and methods are deprecated
79+
#[deprecated]
80+
pub struct BothStructAndMethodDeprecated {
81+
value: i32,
82+
}
83+
84+
impl BothStructAndMethodDeprecated {
85+
#[deprecated]
86+
pub fn method_to_be_deprecated(&self) {}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
pub union NormalUnion {
2+
a: i32,
3+
b: u32,
4+
}
5+
6+
impl NormalUnion {
7+
#[deprecated]
8+
pub fn method_to_be_deprecated(&self) -> i32 {
9+
unsafe { self.a }
10+
}
11+
12+
#[deprecated = "Use get_b instead"]
13+
pub fn method_with_message_to_be_deprecated(&self) -> u32 {
14+
unsafe { self.b }
15+
}
16+
17+
#[deprecated]
18+
pub fn already_deprecated_method(&self) {}
19+
20+
pub fn stays_normal(&self) {}
21+
}
22+
23+
#[doc(hidden)]
24+
pub union HiddenUnion {
25+
a: i32,
26+
}
27+
28+
impl HiddenUnion {
29+
#[deprecated]
30+
pub fn method_to_be_deprecated(&self) {}
31+
}
32+
33+
pub union UnionWithHiddenImpl {
34+
a: i32,
35+
}
36+
37+
#[doc(hidden)]
38+
impl UnionWithHiddenImpl {
39+
#[deprecated]
40+
pub fn method_to_be_deprecated(&self) {}
41+
}
42+
43+
pub union UnionWithHiddenMethod {
44+
a: i32,
45+
}
46+
47+
impl UnionWithHiddenMethod {
48+
#[doc(hidden)]
49+
#[deprecated]
50+
pub fn method_to_be_deprecated(&self) {}
51+
}
52+
53+
union PrivateUnion {
54+
a: i32,
55+
}
56+
57+
impl PrivateUnion {
58+
#[deprecated]
59+
pub fn method_to_be_deprecated(&self) {}
60+
}
61+
62+
// Enum is deprecated but methods remain unchanged
63+
#[deprecated = "Use NewUnion instead"]
64+
pub union UnionBecomesDeprecated {
65+
a: i32,
66+
b: u32,
67+
}
68+
69+
impl UnionBecomesDeprecated {
70+
// Methods intentionally not deprecated
71+
pub fn method_stays_normal(&self) -> i32 {
72+
unsafe { self.a }
73+
}
74+
75+
pub fn another_normal_method(&self) -> u32 {
76+
unsafe { self.b }
77+
}
78+
}
79+
80+
// Both union and methods are deprecated
81+
#[deprecated]
82+
pub union BothUnionAndMethodDeprecated {
83+
a: i32,
84+
b: u32,
85+
}
86+
87+
impl BothUnionAndMethodDeprecated {
88+
#[deprecated]
89+
pub fn method_to_be_deprecated(&self) {}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
publish = false
3+
name = "type_method_marked_deprecated"
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
[dependencies]

0 commit comments

Comments
 (0)