minmax
: use let chain
This commit is contained in:
parent
488a545a50
commit
eda45aaba1
@ -5,7 +5,7 @@ use rustc_hir::{Expr, ExprKind};
|
|||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::declare_lint_pass;
|
use rustc_session::declare_lint_pass;
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering::{Equal, Greater, Less};
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// ### What it does
|
/// ### What it does
|
||||||
@ -36,26 +36,21 @@ declare_lint_pass!(MinMaxPass => [MIN_MAX]);
|
|||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for MinMaxPass {
|
impl<'tcx> LateLintPass<'tcx> for MinMaxPass {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||||
if let Some((outer_max, outer_c, oe)) = min_max(cx, expr) {
|
if let Some((outer_max, outer_c, oe)) = min_max(cx, expr)
|
||||||
if let Some((inner_max, inner_c, ie)) = min_max(cx, oe) {
|
&& let Some((inner_max, inner_c, ie)) = min_max(cx, oe)
|
||||||
if outer_max == inner_max {
|
&& outer_max != inner_max
|
||||||
return;
|
&& let Some(ord) = Constant::partial_cmp(cx.tcx, cx.typeck_results().expr_ty(ie), &outer_c, &inner_c)
|
||||||
}
|
&& matches!(
|
||||||
match (
|
(outer_max, ord),
|
||||||
outer_max,
|
(MinMax::Max, Equal | Greater) | (MinMax::Min, Equal | Less)
|
||||||
Constant::partial_cmp(cx.tcx, cx.typeck_results().expr_ty(ie), &outer_c, &inner_c),
|
)
|
||||||
) {
|
{
|
||||||
(_, None) | (MinMax::Max, Some(Ordering::Less)) | (MinMax::Min, Some(Ordering::Greater)) => (),
|
span_lint(
|
||||||
_ => {
|
cx,
|
||||||
span_lint(
|
MIN_MAX,
|
||||||
cx,
|
expr.span,
|
||||||
MIN_MAX,
|
"this `min`/`max` combination leads to constant result",
|
||||||
expr.span,
|
);
|
||||||
"this `min`/`max` combination leads to constant result",
|
|
||||||
);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user