1
1
mod bind_instead_of_map;
2
2
mod bytes_nth;
3
+ mod chars_cmp;
4
+ mod chars_next_cmp;
3
5
mod clone_on_copy;
4
6
mod clone_on_ref_ptr;
5
7
mod expect_fun_call;
@@ -2024,7 +2026,7 @@ struct BinaryExprInfo<'a> {
2024
2026
/// Checks for the `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
2025
2027
fn lint_binary_expr_with_method_call ( cx : & LateContext < ' _ > , info : & mut BinaryExprInfo < ' _ > ) {
2026
2028
macro_rules! lint_with_both_lhs_and_rhs {
2027
- ( $func: ident , $cx: expr, $info: ident) => {
2029
+ ( $func: expr , $cx: expr, $info: ident) => {
2028
2030
if !$func( $cx, $info) {
2029
2031
:: std:: mem:: swap( & mut $info. chain, & mut $info. other) ;
2030
2032
if $func( $cx, $info) {
@@ -2034,67 +2036,18 @@ fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExpr
2034
2036
} ;
2035
2037
}
2036
2038
2037
- lint_with_both_lhs_and_rhs ! ( lint_chars_next_cmp , cx, info) ;
2039
+ lint_with_both_lhs_and_rhs ! ( chars_next_cmp :: check , cx, info) ;
2038
2040
lint_with_both_lhs_and_rhs ! ( lint_chars_last_cmp, cx, info) ;
2039
2041
lint_with_both_lhs_and_rhs ! ( lint_chars_next_cmp_with_unwrap, cx, info) ;
2040
2042
lint_with_both_lhs_and_rhs ! ( lint_chars_last_cmp_with_unwrap, cx, info) ;
2041
2043
}
2042
2044
2043
- /// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
2044
- fn lint_chars_cmp (
2045
- cx : & LateContext < ' _ > ,
2046
- info : & BinaryExprInfo < ' _ > ,
2047
- chain_methods : & [ & str ] ,
2048
- lint : & ' static Lint ,
2049
- suggest : & str ,
2050
- ) -> bool {
2051
- if_chain ! {
2052
- if let Some ( args) = method_chain_args( info. chain, chain_methods) ;
2053
- if let hir:: ExprKind :: Call ( ref fun, ref arg_char) = info. other. kind;
2054
- if arg_char. len( ) == 1 ;
2055
- if let hir:: ExprKind :: Path ( ref qpath) = fun. kind;
2056
- if let Some ( segment) = single_segment_path( qpath) ;
2057
- if segment. ident. name == sym:: Some ;
2058
- then {
2059
- let mut applicability = Applicability :: MachineApplicable ;
2060
- let self_ty = cx. typeck_results( ) . expr_ty_adjusted( & args[ 0 ] [ 0 ] ) . peel_refs( ) ;
2061
-
2062
- if * self_ty. kind( ) != ty:: Str {
2063
- return false ;
2064
- }
2065
-
2066
- span_lint_and_sugg(
2067
- cx,
2068
- lint,
2069
- info. expr. span,
2070
- & format!( "you should use the `{}` method" , suggest) ,
2071
- "like this" ,
2072
- format!( "{}{}.{}({})" ,
2073
- if info. eq { "" } else { "!" } ,
2074
- snippet_with_applicability( cx, args[ 0 ] [ 0 ] . span, ".." , & mut applicability) ,
2075
- suggest,
2076
- snippet_with_applicability( cx, arg_char[ 0 ] . span, ".." , & mut applicability) ) ,
2077
- applicability,
2078
- ) ;
2079
-
2080
- return true ;
2081
- }
2082
- }
2083
-
2084
- false
2085
- }
2086
-
2087
- /// Checks for the `CHARS_NEXT_CMP` lint.
2088
- fn lint_chars_next_cmp < ' tcx > ( cx : & LateContext < ' tcx > , info : & BinaryExprInfo < ' _ > ) -> bool {
2089
- lint_chars_cmp ( cx, info, & [ "chars" , "next" ] , CHARS_NEXT_CMP , "starts_with" )
2090
- }
2091
-
2092
2045
/// Checks for the `CHARS_LAST_CMP` lint.
2093
2046
fn lint_chars_last_cmp < ' tcx > ( cx : & LateContext < ' tcx > , info : & BinaryExprInfo < ' _ > ) -> bool {
2094
- if lint_chars_cmp ( cx, info, & [ "chars" , "last" ] , CHARS_LAST_CMP , "ends_with" ) {
2047
+ if chars_cmp :: check ( cx, info, & [ "chars" , "last" ] , CHARS_LAST_CMP , "ends_with" ) {
2095
2048
true
2096
2049
} else {
2097
- lint_chars_cmp ( cx, info, & [ "chars" , "next_back" ] , CHARS_LAST_CMP , "ends_with" )
2050
+ chars_cmp :: check ( cx, info, & [ "chars" , "next_back" ] , CHARS_LAST_CMP , "ends_with" )
2098
2051
}
2099
2052
}
2100
2053
0 commit comments