@@ -45,52 +45,48 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
4545 if filter. ident. name == sym!( filter) ;
4646 if filter_args. len( ) == 2 ;
4747 if let ExprKind :: Closure ( _, _, body_id, _, _) = filter_args[ 1 ] . kind;
48+ let body = cx. tcx. hir( ) . body( body_id) ;
49+ if body. params. len( ) == 1 ;
50+ if let Some ( argname) = get_pat_name( & body. params[ 0 ] . pat) ;
51+ if let ExprKind :: Binary ( ref op, ref l, ref r) = body. value. kind;
52+ if op. node == BinOpKind :: Eq ;
53+ if match_type( cx,
54+ cx. typeck_results( ) . expr_ty( & filter_args[ 0 ] ) . peel_refs( ) ,
55+ & paths:: SLICE_ITER ) ;
4856 then {
49- let body = cx. tcx. hir( ) . body( body_id) ;
50- if_chain! {
51- if body. params. len( ) == 1 ;
52- if let Some ( argname) = get_pat_name( & body. params[ 0 ] . pat) ;
53- if let ExprKind :: Binary ( ref op, ref l, ref r) = body. value. kind;
54- if op. node == BinOpKind :: Eq ;
55- if match_type( cx,
56- cx. typeck_results( ) . expr_ty( & filter_args[ 0 ] ) . peel_refs( ) ,
57- & paths:: SLICE_ITER ) ;
58- then {
59- let needle = match get_path_name( l) {
60- Some ( name) if check_arg( name, argname, r) => r,
61- _ => match get_path_name( r) {
62- Some ( name) if check_arg( name, argname, l) => l,
63- _ => { return ; }
64- }
65- } ;
66- if ty:: Uint ( UintTy :: U8 ) != * cx. typeck_results( ) . expr_ty( needle) . peel_refs( ) . kind( ) {
67- return ;
68- }
69- let haystack = if let ExprKind :: MethodCall ( ref path, _, ref args, _) =
70- filter_args[ 0 ] . kind {
71- let p = path. ident. name;
72- if ( p == sym:: iter || p == sym!( iter_mut) ) && args. len( ) == 1 {
73- & args[ 0 ]
74- } else {
75- & filter_args[ 0 ]
76- }
77- } else {
78- & filter_args[ 0 ]
79- } ;
80- let mut applicability = Applicability :: MaybeIncorrect ;
81- span_lint_and_sugg(
82- cx,
83- NAIVE_BYTECOUNT ,
84- expr. span,
85- "you appear to be counting bytes the naive way" ,
86- "consider using the bytecount crate" ,
87- format!( "bytecount::count({}, {})" ,
88- snippet_with_applicability( cx, haystack. span, ".." , & mut applicability) ,
89- snippet_with_applicability( cx, needle. span, ".." , & mut applicability) ) ,
90- applicability,
91- ) ;
57+ let needle = match get_path_name( l) {
58+ Some ( name) if check_arg( name, argname, r) => r,
59+ _ => match get_path_name( r) {
60+ Some ( name) if check_arg( name, argname, l) => l,
61+ _ => { return ; }
9262 }
9363 } ;
64+ if ty:: Uint ( UintTy :: U8 ) != * cx. typeck_results( ) . expr_ty( needle) . peel_refs( ) . kind( ) {
65+ return ;
66+ }
67+ let haystack = if let ExprKind :: MethodCall ( ref path, _, ref args, _) =
68+ filter_args[ 0 ] . kind {
69+ let p = path. ident. name;
70+ if ( p == sym:: iter || p == sym!( iter_mut) ) && args. len( ) == 1 {
71+ & args[ 0 ]
72+ } else {
73+ & filter_args[ 0 ]
74+ }
75+ } else {
76+ & filter_args[ 0 ]
77+ } ;
78+ let mut applicability = Applicability :: MaybeIncorrect ;
79+ span_lint_and_sugg(
80+ cx,
81+ NAIVE_BYTECOUNT ,
82+ expr. span,
83+ "you appear to be counting bytes the naive way" ,
84+ "consider using the bytecount crate" ,
85+ format!( "bytecount::count({}, {})" ,
86+ snippet_with_applicability( cx, haystack. span, ".." , & mut applicability) ,
87+ snippet_with_applicability( cx, needle. span, ".." , & mut applicability) ) ,
88+ applicability,
89+ ) ;
9490 }
9591 } ;
9692 }
0 commit comments