|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::eq_expr_value; |
| 3 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 4 | +use rustc_lint::LateContext; |
| 5 | + |
| 6 | +use super::CONSTANT_BOOL_EXPR; |
| 7 | + |
| 8 | +pub(crate) fn check<'tcx>( |
| 9 | + cx: &LateContext<'tcx>, |
| 10 | + e: &'tcx Expr<'_>, |
| 11 | + op: BinOpKind, |
| 12 | + left: &'tcx Expr<'_>, |
| 13 | + right: &'tcx Expr<'_>, |
| 14 | +) { |
| 15 | + match op { |
| 16 | + BinOpKind::Or => detect_always_true_or(cx, e, left, right), |
| 17 | + BinOpKind::And => detect_always_false_and(cx, e, left, right), |
| 18 | + _ => (), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// `left_lhs != left_rhs || right_lhs != right_rhs` |
| 23 | +fn detect_always_true_or<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, left: &'tcx Expr<'_>, right: &'tcx Expr<'_>) { |
| 24 | + let ExprKind::Binary(left_op, left_lhs, left_rhs) = left.kind else { |
| 25 | + return; |
| 26 | + }; |
| 27 | + let ExprKind::Binary(right_op, right_lhs, right_rhs) = right.kind else { |
| 28 | + return; |
| 29 | + }; |
| 30 | + if left_op.node != BinOpKind::Ne || right_op.node != BinOpKind::Ne { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if detect_variants(cx, e, left_lhs, right_lhs, left_rhs, right_rhs) { |
| 35 | + span_lint(cx, CONSTANT_BOOL_EXPR, e.span, "expression always evaluates to `true`"); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// `left_lhs == left_rhs && right_lhs == right_rhs` |
| 40 | +fn detect_always_false_and<'tcx>( |
| 41 | + cx: &LateContext<'tcx>, |
| 42 | + e: &'tcx Expr<'_>, |
| 43 | + left: &'tcx Expr<'_>, |
| 44 | + right: &'tcx Expr<'_>, |
| 45 | +) { |
| 46 | + let ExprKind::Binary(left_op, left_lhs, left_rhs) = left.kind else { |
| 47 | + return; |
| 48 | + }; |
| 49 | + let ExprKind::Binary(right_op, right_lhs, right_rhs) = right.kind else { |
| 50 | + return; |
| 51 | + }; |
| 52 | + if left_op.node != BinOpKind::Eq || right_op.node != BinOpKind::Eq { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if detect_variants(cx, e, left_lhs, right_lhs, left_rhs, right_rhs) { |
| 57 | + span_lint(cx, CONSTANT_BOOL_EXPR, e.span, "expression always evaluates to `false`"); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn detect_variants<'tcx>( |
| 62 | + cx: &LateContext<'tcx>, |
| 63 | + e: &'tcx Expr<'_>, |
| 64 | + left_lhs: &'tcx Expr<'_>, |
| 65 | + right_lhs: &'tcx Expr<'_>, |
| 66 | + left_rhs: &'tcx Expr<'_>, |
| 67 | + right_rhs: &'tcx Expr<'_>, |
| 68 | +) -> bool { |
| 69 | + let ctxt = e.span.ctxt(); |
| 70 | + |
| 71 | + let detect_variant = |left_a, right_a, left_literal: &Expr<'_>, right_literal: &Expr<'_>| { |
| 72 | + matches!(left_literal.kind, ExprKind::Lit(_)) |
| 73 | + && matches!(right_literal.kind, ExprKind::Lit(_)) |
| 74 | + && !eq_expr_value(cx, ctxt, left_literal, right_literal) |
| 75 | + && eq_expr_value(cx, ctxt, left_a, right_a) |
| 76 | + }; |
| 77 | + |
| 78 | + // (a CMP_OP _) BIN_OP (a CMP_OP _) |
| 79 | + detect_variant(left_lhs, right_lhs, left_rhs, right_rhs) |
| 80 | + // (a CMP_OP _) BIN_OP (_ CMP_OP a) |
| 81 | + || detect_variant(left_lhs, right_rhs, left_rhs, right_lhs) |
| 82 | + // (_ CMP_OP a) BIN_OP (a CMP_OP _) |
| 83 | + || detect_variant(left_rhs, right_lhs, left_lhs, right_rhs) |
| 84 | + // (_ CMP_OP a) BIN_OP (_ CMP_OP a) |
| 85 | + || detect_variant(left_rhs, right_rhs, left_lhs, right_lhs) |
| 86 | +} |
0 commit comments