@@ -21,8 +21,10 @@ const CONVENTIONS: [(&[Convention], &[SelfKind]); 9] = [
21
21
22
22
// Conversion using `to_` can use borrowed (non-Copy types) or owned (Copy types).
23
23
// Source: https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv
24
- ( & [ Convention :: StartsWith ( "to_" ) , Convention :: NotEndsWith ( "_mut" ) , Convention :: IsSelfTypeCopy ( false ) , Convention :: ImplementsTrait ( false ) ] , & [ SelfKind :: Ref ] ) ,
25
- ( & [ Convention :: StartsWith ( "to_" ) , Convention :: NotEndsWith ( "_mut" ) , Convention :: IsSelfTypeCopy ( true ) , Convention :: ImplementsTrait ( false ) ] , & [ SelfKind :: Value ] ) ,
24
+ ( & [ Convention :: StartsWith ( "to_" ) , Convention :: NotEndsWith ( "_mut" ) , Convention :: IsSelfTypeCopy ( false ) ,
25
+ Convention :: IsTraitItem ( false ) ] , & [ SelfKind :: Ref ] ) ,
26
+ ( & [ Convention :: StartsWith ( "to_" ) , Convention :: NotEndsWith ( "_mut" ) , Convention :: IsSelfTypeCopy ( true ) ,
27
+ Convention :: IsTraitItem ( false ) , Convention :: ImplementsTrait ( false ) ] , & [ SelfKind :: Value ] ) ,
26
28
] ;
27
29
28
30
enum Convention {
@@ -32,18 +34,27 @@ enum Convention {
32
34
NotEndsWith ( & ' static str ) ,
33
35
IsSelfTypeCopy ( bool ) ,
34
36
ImplementsTrait ( bool ) ,
37
+ IsTraitItem ( bool ) ,
35
38
}
36
39
37
40
impl Convention {
38
41
#[ must_use]
39
- fn check < ' tcx > ( & self , cx : & LateContext < ' tcx > , self_ty : & ' tcx TyS < ' tcx > , other : & str , is_trait_def : bool ) -> bool {
42
+ fn check < ' tcx > (
43
+ & self ,
44
+ cx : & LateContext < ' tcx > ,
45
+ self_ty : & ' tcx TyS < ' tcx > ,
46
+ other : & str ,
47
+ implements_trait : bool ,
48
+ is_trait_item : bool ,
49
+ ) -> bool {
40
50
match * self {
41
51
Self :: Eq ( this) => this == other,
42
52
Self :: StartsWith ( this) => other. starts_with ( this) && this != other,
43
53
Self :: EndsWith ( this) => other. ends_with ( this) && this != other,
44
- Self :: NotEndsWith ( this) => !Self :: EndsWith ( this) . check ( cx, self_ty, other, is_trait_def ) ,
54
+ Self :: NotEndsWith ( this) => !Self :: EndsWith ( this) . check ( cx, self_ty, other, implements_trait , is_trait_item ) ,
45
55
Self :: IsSelfTypeCopy ( is_true) => is_true == is_copy ( cx, self_ty) ,
46
- Self :: ImplementsTrait ( is_true) => is_true == is_trait_def,
56
+ Self :: ImplementsTrait ( is_true) => is_true == implements_trait,
57
+ Self :: IsTraitItem ( is_true) => is_true == is_trait_item,
47
58
}
48
59
}
49
60
}
@@ -60,19 +71,25 @@ impl fmt::Display for Convention {
60
71
} ,
61
72
Self :: ImplementsTrait ( is_true) => {
62
73
let ( negation, s_suffix) = if is_true { ( "" , "s" ) } else { ( " does not" , "" ) } ;
63
- format ! ( "Method{} implement{} a trait" , negation, s_suffix) . fmt ( f)
74
+ format ! ( "method{} implement{} a trait" , negation, s_suffix) . fmt ( f)
75
+ } ,
76
+ Self :: IsTraitItem ( is_true) => {
77
+ let suffix = if is_true { " is" } else { " is not" } ;
78
+ format ! ( "method{} a trait item" , suffix) . fmt ( f)
64
79
} ,
65
80
}
66
81
}
67
82
}
68
83
84
+ #[ allow( clippy:: too_many_arguments) ]
69
85
pub ( super ) fn check < ' tcx > (
70
86
cx : & LateContext < ' tcx > ,
71
87
item_name : & str ,
72
88
is_pub : bool ,
73
89
self_ty : & ' tcx TyS < ' tcx > ,
74
90
first_arg_ty : & ' tcx TyS < ' tcx > ,
75
91
first_arg_span : Span ,
92
+ implements_trait : bool ,
76
93
is_trait_item : bool ,
77
94
) {
78
95
let lint = if is_pub {
@@ -83,7 +100,7 @@ pub(super) fn check<'tcx>(
83
100
if let Some ( ( conventions, self_kinds) ) = & CONVENTIONS . iter ( ) . find ( |( convs, _) | {
84
101
convs
85
102
. iter ( )
86
- . all ( |conv| conv. check ( cx, self_ty, item_name, is_trait_item) )
103
+ . all ( |conv| conv. check ( cx, self_ty, item_name, implements_trait , is_trait_item) )
87
104
} ) {
88
105
if !self_kinds. iter ( ) . any ( |k| k. matches ( cx, self_ty, first_arg_ty) ) {
89
106
let suggestion = {
@@ -99,6 +116,7 @@ pub(super) fn check<'tcx>(
99
116
. filter_map ( |conv| {
100
117
if ( cut_ends_with_conv && matches ! ( conv, Convention :: NotEndsWith ( _) ) )
101
118
|| matches ! ( conv, Convention :: ImplementsTrait ( _) )
119
+ || matches ! ( conv, Convention :: IsTraitItem ( _) )
102
120
{
103
121
None
104
122
} else {
0 commit comments