@@ -24,10 +24,14 @@ fn handle_expr(
2424 span : Span ,
2525 before_chars : Span ,
2626 revert : bool ,
27+ is_all : bool ,
2728) {
2829 match expr. kind {
2930 ExprKind :: MethodCall ( method, receiver, [ ] , _) => {
30- if method. ident . name . as_str ( ) == "is_ascii"
31+ // If we have `!is_ascii`, then only `.any()` should warn. And if the condition is
32+ // `is_ascii`, then only `.all()` should warn.
33+ if revert != is_all
34+ && method. ident . name . as_str ( ) == "is_ascii"
3135 && path_to_local_id ( receiver, first_param)
3236 && let char_arg_ty = cx. typeck_results ( ) . expr_ty_adjusted ( receiver) . peel_refs ( )
3337 && * char_arg_ty. kind ( ) == ty:: Char
@@ -55,12 +59,23 @@ fn handle_expr(
5559 && let Some ( last_chain_binding_id) =
5660 get_last_chain_binding_hir_id ( first_param, block. stmts )
5761 {
58- handle_expr ( cx, block_expr, last_chain_binding_id, span, before_chars, revert) ;
62+ handle_expr (
63+ cx,
64+ block_expr,
65+ last_chain_binding_id,
66+ span,
67+ before_chars,
68+ revert,
69+ is_all,
70+ ) ;
5971 }
6072 } ,
61- ExprKind :: Unary ( UnOp :: Not , expr) => handle_expr ( cx, expr, first_param, span, before_chars, !revert) ,
73+ ExprKind :: Unary ( UnOp :: Not , expr) => handle_expr ( cx, expr, first_param, span, before_chars, !revert, is_all ) ,
6274 ExprKind :: Call ( fn_path, [ arg] ) => {
63- if let ExprKind :: Path ( path) = fn_path. kind
75+ // If we have `!is_ascii`, then only `.any()` should warn. And if the condition is
76+ // `is_ascii`, then only `.all()` should warn.
77+ if revert != is_all
78+ && let ExprKind :: Path ( path) = fn_path. kind
6479 && let Some ( fn_def_id) = cx. qpath_res ( & path, fn_path. hir_id ) . opt_def_id ( )
6580 && match_def_path ( cx, fn_def_id, & [ "core" , "char" , "methods" , "<impl char>" , "is_ascii" ] )
6681 && path_to_local_id ( peels_expr_ref ( arg) , first_param)
@@ -81,7 +96,7 @@ fn handle_expr(
8196 }
8297}
8398
84- pub ( super ) fn check ( cx : & LateContext < ' _ > , call_expr : & Expr < ' _ > , recv : & Expr < ' _ > , closure_arg : & Expr < ' _ > ) {
99+ pub ( super ) fn check ( cx : & LateContext < ' _ > , call_expr : & Expr < ' _ > , recv : & Expr < ' _ > , closure_arg : & Expr < ' _ > , is_all : bool ) {
85100 if let ExprKind :: Closure ( & Closure { body, .. } ) = closure_arg. kind
86101 && let body = cx. tcx . hir ( ) . body ( body)
87102 && let Some ( first_param) = body. params . first ( )
@@ -103,6 +118,7 @@ pub(super) fn check(cx: &LateContext<'_>, call_expr: &Expr<'_>, recv: &Expr<'_>,
103118 recv. span . with_hi ( call_expr. span . hi ( ) ) ,
104119 recv. span . with_hi ( expr_start. hi ( ) ) ,
105120 false ,
121+ is_all,
106122 ) ;
107123 }
108124}
0 commit comments