|
| 1 | +#![allow(clippy::match_same_arms)] |
| 2 | + |
| 3 | +use std::cmp::Ordering; |
| 4 | + |
| 5 | +use clippy_utils::consts::{constant, Constant}; |
| 6 | +use if_chain::if_chain; |
| 7 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_middle::ty::layout::HasTyCtxt; |
| 10 | +use rustc_middle::ty::{Ty, TypeckResults}; |
| 11 | +use rustc_span::source_map::{Span, Spanned}; |
| 12 | + |
| 13 | +use clippy_utils::diagnostics::span_lint_and_note; |
| 14 | +use clippy_utils::source::snippet; |
| 15 | +use clippy_utils::SpanlessEq; |
| 16 | + |
| 17 | +use super::{IMPOSSIBLE_COMPARISONS, REDUNDANT_COMPARISONS}; |
| 18 | + |
| 19 | +// Extract a comparison between a const and non-const |
| 20 | +// Flip yoda conditionals, turnings expressions like `42 < x` into `x > 42` |
| 21 | +fn comparison_to_const<'tcx>( |
| 22 | + cx: &LateContext<'tcx>, |
| 23 | + typeck: &TypeckResults<'tcx>, |
| 24 | + expr: &'tcx Expr<'tcx>, |
| 25 | +) -> Option<(CmpOp, &'tcx Expr<'tcx>, &'tcx Expr<'tcx>, Constant<'tcx>, Ty<'tcx>)> { |
| 26 | + if_chain! { |
| 27 | + if let ExprKind::Binary(operator, left, right) = expr.kind; |
| 28 | + if let Ok(cmp_op) = CmpOp::try_from(operator.node); |
| 29 | + then { |
| 30 | + match (constant(cx, typeck, left), constant(cx, typeck, right)) { |
| 31 | + (Some(_), Some(_)) => None, |
| 32 | + (_, Some(con)) => Some((cmp_op, left, right, con, typeck.expr_ty(right))), |
| 33 | + (Some(con), _) => Some((cmp_op.reverse(), right, left, con, typeck.expr_ty(left))), |
| 34 | + _ => None, |
| 35 | + } |
| 36 | + } else { |
| 37 | + None |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub(super) fn check<'tcx>( |
| 43 | + cx: &LateContext<'tcx>, |
| 44 | + and_op: Spanned<BinOpKind>, |
| 45 | + left_cond: &'tcx Expr<'tcx>, |
| 46 | + right_cond: &'tcx Expr<'tcx>, |
| 47 | + span: Span, |
| 48 | +) { |
| 49 | + if_chain! { |
| 50 | + // Ensure that the binary operator is && |
| 51 | + if and_op.node == BinOpKind::And; |
| 52 | + |
| 53 | + // Check that both operands to '&&' are themselves a binary operation |
| 54 | + // The `comparison_to_const` step also checks this, so this step is just an optimization |
| 55 | + if let ExprKind::Binary(_, _, _) = left_cond.kind; |
| 56 | + if let ExprKind::Binary(_, _, _) = right_cond.kind; |
| 57 | + |
| 58 | + let typeck = cx.typeck_results(); |
| 59 | + |
| 60 | + // Check that both operands to '&&' compare a non-literal to a literal |
| 61 | + if let Some((left_cmp_op, left_expr, left_const_expr, left_const, left_type)) = |
| 62 | + comparison_to_const(cx, typeck, left_cond); |
| 63 | + if let Some((right_cmp_op, right_expr, right_const_expr, right_const, right_type)) = |
| 64 | + comparison_to_const(cx, typeck, right_cond); |
| 65 | + |
| 66 | + if left_type == right_type; |
| 67 | + |
| 68 | + // Check that the same expression is compared in both comparisons |
| 69 | + if SpanlessEq::new(cx).eq_expr(left_expr, right_expr); |
| 70 | + |
| 71 | + if !left_expr.can_have_side_effects(); |
| 72 | + |
| 73 | + // Compare the two constant expressions |
| 74 | + if let Some(ordering) = Constant::partial_cmp(cx.tcx(), left_type, &left_const, &right_const); |
| 75 | + |
| 76 | + // Rule out the `x >= 42 && x <= 42` corner case immediately |
| 77 | + // Mostly to simplify the implementation, but it is also covered by `clippy::double_comparisons` |
| 78 | + if !matches!( |
| 79 | + (&left_cmp_op, &right_cmp_op, ordering), |
| 80 | + (CmpOp::Le | CmpOp::Ge, CmpOp::Le | CmpOp::Ge, Ordering::Equal) |
| 81 | + ); |
| 82 | + |
| 83 | + then { |
| 84 | + if left_cmp_op.direction() == right_cmp_op.direction() { |
| 85 | + let lhs_str = snippet(cx, left_cond.span, "<lhs>"); |
| 86 | + let rhs_str = snippet(cx, right_cond.span, "<rhs>"); |
| 87 | + // We already know that either side of `&&` has no effect, |
| 88 | + // but emit a different error message depending on which side it is |
| 89 | + if left_side_is_useless(left_cmp_op, ordering) { |
| 90 | + span_lint_and_note( |
| 91 | + cx, |
| 92 | + REDUNDANT_COMPARISONS, |
| 93 | + span, |
| 94 | + "left-hand side of `&&` operator has no effect", |
| 95 | + Some(left_cond.span.until(right_cond.span)), |
| 96 | + &format!("`if `{rhs_str}` evaluates to true, {lhs_str}` will always evaluate to true as well"), |
| 97 | + ); |
| 98 | + } else { |
| 99 | + span_lint_and_note( |
| 100 | + cx, |
| 101 | + REDUNDANT_COMPARISONS, |
| 102 | + span, |
| 103 | + "right-hand side of `&&` operator has no effect", |
| 104 | + Some(and_op.span.to(right_cond.span)), |
| 105 | + &format!("`if `{lhs_str}` evaluates to true, {rhs_str}` will always evaluate to true as well"), |
| 106 | + ); |
| 107 | + } |
| 108 | + // We could autofix this error but choose not to, |
| 109 | + // because code triggering this lint probably not behaving correctly in the first place |
| 110 | + } |
| 111 | + else if !comparison_is_possible(left_cmp_op.direction(), ordering) { |
| 112 | + let expr_str = snippet(cx, left_expr.span, ".."); |
| 113 | + let lhs_str = snippet(cx, left_const_expr.span, "<lhs>"); |
| 114 | + let rhs_str = snippet(cx, right_const_expr.span, "<rhs>"); |
| 115 | + let note = match ordering { |
| 116 | + Ordering::Less => format!("since `{lhs_str}` < `{rhs_str}`, the expression evaluates to false for any value of `{expr_str}`"), |
| 117 | + Ordering::Equal => format!("`{expr_str}` cannot simultaneously be greater than and less than `{lhs_str}`"), |
| 118 | + Ordering::Greater => format!("since `{lhs_str}` > `{rhs_str}`, the expression evaluates to false for any value of `{expr_str}`"), |
| 119 | + }; |
| 120 | + span_lint_and_note( |
| 121 | + cx, |
| 122 | + IMPOSSIBLE_COMPARISONS, |
| 123 | + span, |
| 124 | + "boolean expression will never evaluate to 'true'", |
| 125 | + None, |
| 126 | + ¬e, |
| 127 | + ); |
| 128 | + }; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +fn left_side_is_useless(left_cmp_op: CmpOp, ordering: Ordering) -> bool { |
| 134 | + // Special-case for equal constants with an inclusive comparison |
| 135 | + if ordering == Ordering::Equal { |
| 136 | + match left_cmp_op { |
| 137 | + CmpOp::Lt | CmpOp::Gt => false, |
| 138 | + CmpOp::Le | CmpOp::Ge => true, |
| 139 | + } |
| 140 | + } else { |
| 141 | + match (left_cmp_op.direction(), ordering) { |
| 142 | + (CmpOpDirection::Lesser, Ordering::Less) => false, |
| 143 | + (CmpOpDirection::Lesser, Ordering::Equal) => false, |
| 144 | + (CmpOpDirection::Lesser, Ordering::Greater) => true, |
| 145 | + (CmpOpDirection::Greater, Ordering::Less) => true, |
| 146 | + (CmpOpDirection::Greater, Ordering::Equal) => false, |
| 147 | + (CmpOpDirection::Greater, Ordering::Greater) => false, |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +fn comparison_is_possible(left_cmp_direction: CmpOpDirection, ordering: Ordering) -> bool { |
| 153 | + match (left_cmp_direction, ordering) { |
| 154 | + (CmpOpDirection::Lesser, Ordering::Less | Ordering::Equal) => false, |
| 155 | + (CmpOpDirection::Lesser, Ordering::Greater) => true, |
| 156 | + (CmpOpDirection::Greater, Ordering::Greater | Ordering::Equal) => false, |
| 157 | + (CmpOpDirection::Greater, Ordering::Less) => true, |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +#[derive(PartialEq, Eq, Clone, Copy)] |
| 162 | +enum CmpOpDirection { |
| 163 | + Lesser, |
| 164 | + Greater, |
| 165 | +} |
| 166 | + |
| 167 | +#[derive(Clone, Copy)] |
| 168 | +enum CmpOp { |
| 169 | + Lt, |
| 170 | + Le, |
| 171 | + Ge, |
| 172 | + Gt, |
| 173 | +} |
| 174 | + |
| 175 | +impl CmpOp { |
| 176 | + fn reverse(self) -> Self { |
| 177 | + match self { |
| 178 | + CmpOp::Lt => CmpOp::Gt, |
| 179 | + CmpOp::Le => CmpOp::Ge, |
| 180 | + CmpOp::Ge => CmpOp::Le, |
| 181 | + CmpOp::Gt => CmpOp::Lt, |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + fn direction(self) -> CmpOpDirection { |
| 186 | + match self { |
| 187 | + CmpOp::Lt => CmpOpDirection::Lesser, |
| 188 | + CmpOp::Le => CmpOpDirection::Lesser, |
| 189 | + CmpOp::Ge => CmpOpDirection::Greater, |
| 190 | + CmpOp::Gt => CmpOpDirection::Greater, |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +impl TryFrom<BinOpKind> for CmpOp { |
| 196 | + type Error = (); |
| 197 | + |
| 198 | + fn try_from(bin_op: BinOpKind) -> Result<Self, Self::Error> { |
| 199 | + match bin_op { |
| 200 | + BinOpKind::Lt => Ok(CmpOp::Lt), |
| 201 | + BinOpKind::Le => Ok(CmpOp::Le), |
| 202 | + BinOpKind::Ge => Ok(CmpOp::Ge), |
| 203 | + BinOpKind::Gt => Ok(CmpOp::Gt), |
| 204 | + _ => Err(()), |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments