|
| 1 | +use rustc_errors::Applicability; |
| 2 | +use rustc_hir::{Closure, Expr, ExprKind, HirId, StmtKind, UnOp}; |
| 3 | +use rustc_lint::LateContext; |
| 4 | +use rustc_middle::ty; |
| 5 | +use rustc_span::Span; |
| 6 | + |
| 7 | +use super::utils::get_last_chain_binding_hir_id; |
| 8 | +use super::NEEDLESS_CHARACTER_ITERATION; |
| 9 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 10 | +use clippy_utils::source::snippet_opt; |
| 11 | +use clippy_utils::{match_def_path, path_to_local_id, peel_blocks}; |
| 12 | + |
| 13 | +fn peels_expr_ref<'a, 'tcx>(mut expr: &'a Expr<'tcx>) -> &'a Expr<'tcx> { |
| 14 | + while let ExprKind::AddrOf(_, _, e) = expr.kind { |
| 15 | + expr = e; |
| 16 | + } |
| 17 | + expr |
| 18 | +} |
| 19 | + |
| 20 | +fn handle_expr( |
| 21 | + cx: &LateContext<'_>, |
| 22 | + expr: &Expr<'_>, |
| 23 | + first_param: HirId, |
| 24 | + span: Span, |
| 25 | + before_chars: Span, |
| 26 | + revert: bool, |
| 27 | +) { |
| 28 | + match expr.kind { |
| 29 | + ExprKind::MethodCall(method, receiver, [], _) => { |
| 30 | + if method.ident.name.as_str() == "is_ascii" |
| 31 | + && path_to_local_id(receiver, first_param) |
| 32 | + && let char_arg_ty = cx.typeck_results().expr_ty_adjusted(receiver).peel_refs() |
| 33 | + && *char_arg_ty.kind() == ty::Char |
| 34 | + && let Some(snippet) = snippet_opt(cx, before_chars) |
| 35 | + { |
| 36 | + span_lint_and_sugg( |
| 37 | + cx, |
| 38 | + NEEDLESS_CHARACTER_ITERATION, |
| 39 | + span, |
| 40 | + "checking if a string is ascii using iterators", |
| 41 | + "try", |
| 42 | + format!("{}{snippet}.is_ascii()", if revert { "!" } else { "" }), |
| 43 | + Applicability::MachineApplicable, |
| 44 | + ); |
| 45 | + } |
| 46 | + }, |
| 47 | + ExprKind::Block(block, _) => { |
| 48 | + if block.stmts.iter().any(|stmt| !matches!(stmt.kind, StmtKind::Let(_))) { |
| 49 | + // If there is something else than let bindings, then better not emit the lint. |
| 50 | + return; |
| 51 | + } |
| 52 | + if let Some(block_expr) = block.expr |
| 53 | + // First we ensure that this is a "binding chain" (each statement is a binding |
| 54 | + // of the previous one) and that it is a binding of the closure argument. |
| 55 | + && let Some(last_chain_binding_id) = |
| 56 | + get_last_chain_binding_hir_id(first_param, block.stmts) |
| 57 | + { |
| 58 | + handle_expr(cx, block_expr, last_chain_binding_id, span, before_chars, revert); |
| 59 | + } |
| 60 | + }, |
| 61 | + ExprKind::Unary(UnOp::Not, expr) => handle_expr(cx, expr, first_param, span, before_chars, !revert), |
| 62 | + ExprKind::Call(fn_path, [arg]) => { |
| 63 | + if let ExprKind::Path(path) = fn_path.kind |
| 64 | + && let Some(fn_def_id) = cx.qpath_res(&path, fn_path.hir_id).opt_def_id() |
| 65 | + && match_def_path(cx, fn_def_id, &["core", "char", "methods", "<impl char>", "is_ascii"]) |
| 66 | + && path_to_local_id(peels_expr_ref(arg), first_param) |
| 67 | + && let Some(snippet) = snippet_opt(cx, before_chars) |
| 68 | + { |
| 69 | + span_lint_and_sugg( |
| 70 | + cx, |
| 71 | + NEEDLESS_CHARACTER_ITERATION, |
| 72 | + span, |
| 73 | + "checking if a string is ascii using iterators", |
| 74 | + "try", |
| 75 | + format!("{}{snippet}.is_ascii()", if revert { "!" } else { "" }), |
| 76 | + Applicability::MachineApplicable, |
| 77 | + ); |
| 78 | + } |
| 79 | + }, |
| 80 | + _ => {}, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +pub(super) fn check(cx: &LateContext<'_>, call_expr: &Expr<'_>, recv: &Expr<'_>, closure_arg: &Expr<'_>) { |
| 85 | + if let ExprKind::Closure(&Closure { body, .. }) = closure_arg.kind |
| 86 | + && let body = cx.tcx.hir().body(body) |
| 87 | + && let Some(first_param) = body.params.first() |
| 88 | + && let ExprKind::MethodCall(method, mut recv, [], _) = recv.kind |
| 89 | + && method.ident.name.as_str() == "chars" |
| 90 | + && let str_ty = cx.typeck_results().expr_ty_adjusted(recv).peel_refs() |
| 91 | + && *str_ty.kind() == ty::Str |
| 92 | + { |
| 93 | + let expr_start = recv.span; |
| 94 | + while let ExprKind::MethodCall(_, new_recv, _, _) = recv.kind { |
| 95 | + recv = new_recv; |
| 96 | + } |
| 97 | + let body_expr = peel_blocks(body.value); |
| 98 | + |
| 99 | + handle_expr( |
| 100 | + cx, |
| 101 | + body_expr, |
| 102 | + first_param.pat.hir_id, |
| 103 | + recv.span.with_hi(call_expr.span.hi()), |
| 104 | + recv.span.with_hi(expr_start.hi()), |
| 105 | + false, |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments