|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::eager_or_lazy::switch_to_eager_eval; |
| 3 | +use clippy_utils::peel_hir_pat_refs; |
| 4 | +use clippy_utils::source::snippet_with_applicability; |
| 5 | +use clippy_utils::sugg::Sugg; |
| 6 | +use rustc_ast::UnOp; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::def::Res; |
| 9 | +use rustc_hir::{BinOpKind, Body, Expr, ExprKind, HirId, QPath}; |
| 10 | +use rustc_lint::LateContext; |
| 11 | +use rustc_middle::ty::{self}; |
| 12 | +use rustc_span::source_map::Spanned; |
| 13 | + |
| 14 | +use super::MANUAL_CONTAINS; |
| 15 | + |
| 16 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, closure_arg: &Expr<'_>) { |
| 17 | + let mut app = Applicability::MachineApplicable; |
| 18 | + |
| 19 | + if !expr.span.from_expansion() |
| 20 | + // check if `iter().any()` can be replaced with `contains()` |
| 21 | + && let ExprKind::Closure(closure) = closure_arg.kind |
| 22 | + && let Body{params: [param],value} = cx.tcx.hir().body(closure.body) |
| 23 | + && let ExprKind::Binary(op, lhs, rhs) = value.kind |
| 24 | + && let (peeled_ref_pat, _) = peel_hir_pat_refs(param.pat) |
| 25 | + && let Some((snip,snip_expr)) = can_replace_with_contains(cx, op, lhs, rhs, peeled_ref_pat.hir_id, &mut app) |
| 26 | + && let ref_type = cx.typeck_results().expr_ty_adjusted(recv) |
| 27 | + && let ty::Ref(_, inner_type, _) = ref_type.kind() |
| 28 | + && let ty::Slice(slice_type) = inner_type.kind() |
| 29 | + && *slice_type == cx.typeck_results().expr_ty(snip_expr) |
| 30 | + { |
| 31 | + span_lint_and_sugg( |
| 32 | + cx, |
| 33 | + MANUAL_CONTAINS, |
| 34 | + expr.span, |
| 35 | + "using `contains()` instead of `iter().any()` is more efficient", |
| 36 | + "try", |
| 37 | + format!( |
| 38 | + "{}.contains({})", |
| 39 | + snippet_with_applicability(cx, recv.span, "_", &mut app), |
| 40 | + snip |
| 41 | + ), |
| 42 | + app, |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +enum EligibleArg { |
| 48 | + IsClosureArg, |
| 49 | + ContainsArg(String), |
| 50 | +} |
| 51 | + |
| 52 | +fn try_get_eligible_arg<'tcx>( |
| 53 | + cx: &LateContext<'tcx>, |
| 54 | + expr: &'tcx Expr<'tcx>, |
| 55 | + closure_arg_id: HirId, |
| 56 | + applicability: &mut Applicability, |
| 57 | +) -> Option<(EligibleArg, &'tcx Expr<'tcx>)> { |
| 58 | + let mut get_snippet = |expr: &Expr<'_>, needs_borrow: bool| { |
| 59 | + let sugg = Sugg::hir_with_applicability(cx, expr, "_", applicability); |
| 60 | + EligibleArg::ContainsArg((if needs_borrow { sugg.addr() } else { sugg }).to_string()) |
| 61 | + }; |
| 62 | + |
| 63 | + match expr.kind { |
| 64 | + ExprKind::Path(QPath::Resolved(_, path)) => { |
| 65 | + if path.res == Res::Local(closure_arg_id) { |
| 66 | + Some((EligibleArg::IsClosureArg, expr)) |
| 67 | + } else { |
| 68 | + Some((get_snippet(expr, true), expr)) |
| 69 | + } |
| 70 | + }, |
| 71 | + ExprKind::Unary(UnOp::Deref, inner) => { |
| 72 | + if let ExprKind::Path(QPath::Resolved(_, path)) = inner.kind { |
| 73 | + if path.res == Res::Local(closure_arg_id) { |
| 74 | + Some((EligibleArg::IsClosureArg, expr)) |
| 75 | + } else { |
| 76 | + Some((get_snippet(inner, false), expr)) |
| 77 | + } |
| 78 | + } else { |
| 79 | + None |
| 80 | + } |
| 81 | + }, |
| 82 | + _ => { |
| 83 | + if switch_to_eager_eval(cx, expr) { |
| 84 | + Some((get_snippet(expr, true), expr)) |
| 85 | + } else { |
| 86 | + None |
| 87 | + } |
| 88 | + }, |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn can_replace_with_contains<'tcx>( |
| 93 | + cx: &LateContext<'tcx>, |
| 94 | + bin_op: Spanned<BinOpKind>, |
| 95 | + left_expr: &'tcx Expr<'tcx>, |
| 96 | + right_expr: &'tcx Expr<'tcx>, |
| 97 | + closure_arg_id: HirId, |
| 98 | + applicability: &mut Applicability, |
| 99 | +) -> Option<(String, &'tcx Expr<'tcx>)> { |
| 100 | + if bin_op.node != BinOpKind::Eq { |
| 101 | + return None; |
| 102 | + } |
| 103 | + |
| 104 | + let left_candidate = try_get_eligible_arg(cx, left_expr, closure_arg_id, applicability)?; |
| 105 | + let right_candidate = try_get_eligible_arg(cx, right_expr, closure_arg_id, applicability)?; |
| 106 | + match (left_candidate, right_candidate) { |
| 107 | + ((EligibleArg::IsClosureArg, _), (EligibleArg::ContainsArg(snip), candidate_expr)) |
| 108 | + | ((EligibleArg::ContainsArg(snip), candidate_expr), (EligibleArg::IsClosureArg, _)) => { |
| 109 | + Some((snip, candidate_expr)) |
| 110 | + }, |
| 111 | + _ => None, |
| 112 | + } |
| 113 | +} |
0 commit comments