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