Skip to content

Commit eda45aa

Browse files
committed
minmax: use let chain
1 parent 488a545 commit eda45aa

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

clippy_lints/src/minmax.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
77
use rustc_span::sym;
8-
use std::cmp::Ordering;
8+
use std::cmp::Ordering::{Equal, Greater, Less};
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -36,26 +36,21 @@ declare_lint_pass!(MinMaxPass => [MIN_MAX]);
3636

3737
impl<'tcx> LateLintPass<'tcx> for MinMaxPass {
3838
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
39-
if let Some((outer_max, outer_c, oe)) = min_max(cx, expr) {
40-
if let Some((inner_max, inner_c, ie)) = min_max(cx, oe) {
41-
if outer_max == inner_max {
42-
return;
43-
}
44-
match (
45-
outer_max,
46-
Constant::partial_cmp(cx.tcx, cx.typeck_results().expr_ty(ie), &outer_c, &inner_c),
47-
) {
48-
(_, None) | (MinMax::Max, Some(Ordering::Less)) | (MinMax::Min, Some(Ordering::Greater)) => (),
49-
_ => {
50-
span_lint(
51-
cx,
52-
MIN_MAX,
53-
expr.span,
54-
"this `min`/`max` combination leads to constant result",
55-
);
56-
},
57-
}
58-
}
39+
if let Some((outer_max, outer_c, oe)) = min_max(cx, expr)
40+
&& let Some((inner_max, inner_c, ie)) = min_max(cx, oe)
41+
&& outer_max != inner_max
42+
&& let Some(ord) = Constant::partial_cmp(cx.tcx, cx.typeck_results().expr_ty(ie), &outer_c, &inner_c)
43+
&& matches!(
44+
(outer_max, ord),
45+
(MinMax::Max, Equal | Greater) | (MinMax::Min, Equal | Less)
46+
)
47+
{
48+
span_lint(
49+
cx,
50+
MIN_MAX,
51+
expr.span,
52+
"this `min`/`max` combination leads to constant result",
53+
);
5954
}
6055
}
6156
}

0 commit comments

Comments
 (0)