@@ -21,8 +21,10 @@ const CONVENTIONS: [(&[Convention], &[SelfKind]); 9] = [
2121
2222 // Conversion using `to_` can use borrowed (non-Copy types) or owned (Copy types).
2323 // 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 ] ) ,
2628] ;
2729
2830enum Convention {
@@ -32,18 +34,27 @@ enum Convention {
3234 NotEndsWith ( & ' static str ) ,
3335 IsSelfTypeCopy ( bool ) ,
3436 ImplementsTrait ( bool ) ,
37+ IsTraitItem ( bool ) ,
3538}
3639
3740impl Convention {
3841 #[ 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 {
4050 match * self {
4151 Self :: Eq ( this) => this == other,
4252 Self :: StartsWith ( this) => other. starts_with ( this) && this != other,
4353 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 ) ,
4555 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,
4758 }
4859 }
4960}
@@ -60,19 +71,25 @@ impl fmt::Display for Convention {
6071 } ,
6172 Self :: ImplementsTrait ( is_true) => {
6273 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)
6479 } ,
6580 }
6681 }
6782}
6883
84+ #[ allow( clippy:: too_many_arguments) ]
6985pub ( super ) fn check < ' tcx > (
7086 cx : & LateContext < ' tcx > ,
7187 item_name : & str ,
7288 is_pub : bool ,
7389 self_ty : & ' tcx TyS < ' tcx > ,
7490 first_arg_ty : & ' tcx TyS < ' tcx > ,
7591 first_arg_span : Span ,
92+ implements_trait : bool ,
7693 is_trait_item : bool ,
7794) {
7895 let lint = if is_pub {
@@ -83,7 +100,7 @@ pub(super) fn check<'tcx>(
83100 if let Some ( ( conventions, self_kinds) ) = & CONVENTIONS . iter ( ) . find ( |( convs, _) | {
84101 convs
85102 . 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) )
87104 } ) {
88105 if !self_kinds. iter ( ) . any ( |k| k. matches ( cx, self_ty, first_arg_ty) ) {
89106 let suggestion = {
@@ -99,6 +116,7 @@ pub(super) fn check<'tcx>(
99116 . filter_map ( |conv| {
100117 if ( cut_ends_with_conv && matches ! ( conv, Convention :: NotEndsWith ( _) ) )
101118 || matches ! ( conv, Convention :: ImplementsTrait ( _) )
119+ || matches ! ( conv, Convention :: IsTraitItem ( _) )
102120 {
103121 None
104122 } else {
0 commit comments