@@ -25,10 +25,10 @@ mod uninit_assumed_init;
25
25
mod unnecessary_filter_map;
26
26
mod unnecessary_lazy_eval;
27
27
mod unwrap_used;
28
+ mod wrong_self_convention;
28
29
mod zst_offset;
29
30
30
31
use std:: borrow:: Cow ;
31
- use std:: fmt;
32
32
use std:: iter;
33
33
34
34
use bind_instead_of_map:: BindInsteadOfMap ;
@@ -1868,7 +1868,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1868
1868
}
1869
1869
}
1870
1870
1871
- lint_wrong_self_convention (
1871
+ wrong_self_convention :: check (
1872
1872
cx,
1873
1873
& name,
1874
1874
item. vis. node. is_pub( ) ,
@@ -1924,7 +1924,14 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1924
1924
let self_ty = TraitRef :: identity( cx. tcx, item. def_id. to_def_id( ) ) . self_ty( ) ;
1925
1925
1926
1926
then {
1927
- lint_wrong_self_convention( cx, & item. ident. name. as_str( ) , false , self_ty, first_arg_ty, first_arg_span) ;
1927
+ wrong_self_convention:: check(
1928
+ cx,
1929
+ & item. ident. name. as_str( ) ,
1930
+ false ,
1931
+ self_ty,
1932
+ first_arg_ty,
1933
+ first_arg_span
1934
+ ) ;
1928
1935
}
1929
1936
}
1930
1937
@@ -1949,39 +1956,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1949
1956
extract_msrv_attr ! ( LateContext ) ;
1950
1957
}
1951
1958
1952
- fn lint_wrong_self_convention < ' tcx > (
1953
- cx : & LateContext < ' tcx > ,
1954
- item_name : & str ,
1955
- is_pub : bool ,
1956
- self_ty : & ' tcx TyS < ' tcx > ,
1957
- first_arg_ty : & ' tcx TyS < ' tcx > ,
1958
- first_arg_span : Span ,
1959
- ) {
1960
- let lint = if is_pub {
1961
- WRONG_PUB_SELF_CONVENTION
1962
- } else {
1963
- WRONG_SELF_CONVENTION
1964
- } ;
1965
- if let Some ( ( ref conv, self_kinds) ) = & CONVENTIONS . iter ( ) . find ( |( ref conv, _) | conv. check ( item_name) ) {
1966
- if !self_kinds. iter ( ) . any ( |k| k. matches ( cx, self_ty, first_arg_ty) ) {
1967
- span_lint (
1968
- cx,
1969
- lint,
1970
- first_arg_span,
1971
- & format ! (
1972
- "methods called `{}` usually take {}; consider choosing a less ambiguous name" ,
1973
- conv,
1974
- & self_kinds
1975
- . iter( )
1976
- . map( |k| k. description( ) )
1977
- . collect:: <Vec <_>>( )
1978
- . join( " or " )
1979
- ) ,
1980
- ) ;
1981
- }
1982
- }
1983
- }
1984
-
1985
1959
/// Checks for the `OR_FUN_CALL` lint.
1986
1960
#[ allow( clippy:: too_many_lines) ]
1987
1961
fn lint_or_fun_call < ' tcx > (
@@ -3415,22 +3389,6 @@ fn lint_into_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_
3415
3389
}
3416
3390
}
3417
3391
3418
- enum Convention {
3419
- Eq ( & ' static str ) ,
3420
- StartsWith ( & ' static str ) ,
3421
- }
3422
-
3423
- #[ rustfmt:: skip]
3424
- const CONVENTIONS : [ ( Convention , & [ SelfKind ] ) ; 7 ] = [
3425
- ( Convention :: Eq ( "new" ) , & [ SelfKind :: No ] ) ,
3426
- ( Convention :: StartsWith ( "as_" ) , & [ SelfKind :: Ref , SelfKind :: RefMut ] ) ,
3427
- ( Convention :: StartsWith ( "from_" ) , & [ SelfKind :: No ] ) ,
3428
- ( Convention :: StartsWith ( "into_" ) , & [ SelfKind :: Value ] ) ,
3429
- ( Convention :: StartsWith ( "is_" ) , & [ SelfKind :: Ref , SelfKind :: No ] ) ,
3430
- ( Convention :: Eq ( "to_mut" ) , & [ SelfKind :: RefMut ] ) ,
3431
- ( Convention :: StartsWith ( "to_" ) , & [ SelfKind :: Ref ] ) ,
3432
- ] ;
3433
-
3434
3392
const FN_HEADER : hir:: FnHeader = hir:: FnHeader {
3435
3393
unsafety : hir:: Unsafety :: Normal ,
3436
3394
constness : hir:: Constness :: NotConst ,
@@ -3602,25 +3560,6 @@ impl SelfKind {
3602
3560
}
3603
3561
}
3604
3562
3605
- impl Convention {
3606
- #[ must_use]
3607
- fn check ( & self , other : & str ) -> bool {
3608
- match * self {
3609
- Self :: Eq ( this) => this == other,
3610
- Self :: StartsWith ( this) => other. starts_with ( this) && this != other,
3611
- }
3612
- }
3613
- }
3614
-
3615
- impl fmt:: Display for Convention {
3616
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> Result < ( ) , fmt:: Error > {
3617
- match * self {
3618
- Self :: Eq ( this) => this. fmt ( f) ,
3619
- Self :: StartsWith ( this) => this. fmt ( f) . and_then ( |_| '*' . fmt ( f) ) ,
3620
- }
3621
- }
3622
- }
3623
-
3624
3563
#[ derive( Clone , Copy ) ]
3625
3564
enum OutType {
3626
3565
Unit ,
0 commit comments