Skip to content

Commit b5ef66f

Browse files
committed
Optimize by doing a cheap check for double binary expression first
1 parent 8f40d09 commit b5ef66f

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

clippy_lints/src/operators/double_const_comparison.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ pub(super) fn check<'tcx>(
5151
// Ensure that the binary operator is &&
5252
if and_op.node == BinOpKind::And;
5353

54-
let typeck_results = cx.typeck_results();
55-
let mut const_context = consts::ConstEvalLateContext::new(cx, typeck_results);
54+
// Check that both operands to '&&' are themselves a binary operation
55+
// The `comparison_to_const` step also checks this, so this step is just an optimization
56+
if let ExprKind::Binary(_, _, _) = left_cond.kind;
57+
if let ExprKind::Binary(_, _, _) = right_cond.kind;
58+
59+
let typeck = cx.typeck_results();
60+
let mut const_context = consts::ConstEvalLateContext::new(cx, typeck);
5661

5762
// Check that both operands to '&&' compare a non-literal to a literal
5863
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);
64+
comparison_to_const(&mut const_context, typeck, left_cond);
6065
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);
66+
comparison_to_const(&mut const_context, typeck, right_cond);
6267

6368
if left_type == right_type;
6469

0 commit comments

Comments
 (0)