Skip to content

Commit da89652

Browse files
refactor(double_comparison): clean-up, simplify lint logic (#15955)
- use let-chains - rm macro - add comments explaining linted cases changelog: none
2 parents 25cbcb4 + aae10d7 commit da89652

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

clippy_lints/src/operators/double_comparison.rs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,52 @@ use rustc_span::Span;
88

99
use super::DOUBLE_COMPARISONS;
1010

11-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, op: BinOpKind, lhs: &'tcx Expr<'_>, rhs: &'tcx Expr<'_>, span: Span) {
12-
let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (&lhs.kind, &rhs.kind) {
13-
(ExprKind::Binary(lb, llhs, lrhs), ExprKind::Binary(rb, rlhs, rrhs)) => {
14-
(lb.node, llhs, lrhs, rb.node, rlhs, rrhs)
15-
},
16-
_ => return,
17-
};
18-
if !(eq_expr_value(cx, llhs, rlhs) && eq_expr_value(cx, lrhs, rrhs)) {
19-
return;
20-
}
21-
macro_rules! lint_double_comparison {
22-
($op:tt) => {{
23-
let mut applicability = Applicability::MachineApplicable;
24-
let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
25-
let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
26-
let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
27-
span_lint_and_sugg(
28-
cx,
29-
DOUBLE_COMPARISONS,
30-
span,
31-
"this binary expression can be simplified",
32-
"try",
33-
sugg,
34-
applicability,
35-
);
36-
}};
37-
}
38-
match (op, lkind, rkind) {
39-
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Lt) | (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Eq) => {
40-
lint_double_comparison!(<=);
41-
},
42-
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Eq) => {
43-
lint_double_comparison!(>=);
44-
},
45-
(BinOpKind::Or, BinOpKind::Lt, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Lt) => {
46-
lint_double_comparison!(!=);
47-
},
48-
(BinOpKind::And, BinOpKind::Le, BinOpKind::Ge) | (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => {
49-
lint_double_comparison!(==);
50-
},
51-
_ => (),
11+
pub(super) fn check(cx: &LateContext<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &Expr<'_>, span: Span) {
12+
if let ExprKind::Binary(lop, llhs, lrhs) = lhs.kind
13+
&& let ExprKind::Binary(rop, rlhs, rrhs) = rhs.kind
14+
&& eq_expr_value(cx, llhs, rlhs)
15+
&& eq_expr_value(cx, lrhs, rrhs)
16+
{
17+
let op = match (op, lop.node, rop.node) {
18+
// x == y || x < y => x <= y
19+
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Lt)
20+
// x < y || x == y => x <= y
21+
| (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Eq) => {
22+
"<="
23+
},
24+
// x == y || x > y => x >= y
25+
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Gt)
26+
// x > y || x == y => x >= y
27+
| (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Eq) => {
28+
">="
29+
},
30+
// x < y || x > y => x != y
31+
(BinOpKind::Or, BinOpKind::Lt, BinOpKind::Gt)
32+
// x > y || x < y => x != y
33+
| (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Lt) => {
34+
"!="
35+
},
36+
// x <= y && x >= y => x == y
37+
(BinOpKind::And, BinOpKind::Le, BinOpKind::Ge)
38+
// x >= y && x <= y => x == y
39+
| (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => {
40+
"=="
41+
},
42+
_ => return,
43+
};
44+
45+
let mut applicability = Applicability::MachineApplicable;
46+
let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
47+
let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
48+
let sugg = format!("{lhs_str} {op} {rhs_str}");
49+
span_lint_and_sugg(
50+
cx,
51+
DOUBLE_COMPARISONS,
52+
span,
53+
"this binary expression can be simplified",
54+
"try",
55+
sugg,
56+
applicability,
57+
);
5258
}
5359
}

0 commit comments

Comments
 (0)