Skip to content

Commit

Permalink
simplify matching on binop result
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Nov 16, 2023
1 parent 7696c9d commit aca4086
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions clippy_utils/src/eager_or_lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,17 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS

ExprKind::Binary(op, left, right)
if matches!(op.node, BinOpKind::Div | BinOpKind::Rem)
&& let left_ty = self.cx.typeck_results().expr_ty(left)
&& let right_ty = self.cx.typeck_results().expr_ty(right)
&& let left = constant(self.cx, self.cx.typeck_results(), left)
.and_then(|c| c.int_value(self.cx, left_ty))
&& let right = constant(self.cx, self.cx.typeck_results(), right)
.and_then(|c| c.int_value(self.cx, right_ty))
&& match (left, right) {
// `1 / x` -- x might be zero
(_, None) => true,
// `x / -1` -- x might be T::MIN = panic
#[expect(clippy::match_same_arms)]
(None, Some(FullInt::S(-1))) => true,
// anything else is either fine or checked by the compiler
_ => false,
} =>
&& matches!(
(left, right),
// `1 / x`: x might be zero
(_, None)
// `x / -1`: x might be T::MIN
| (None, Some(FullInt::S(-1)))
) =>
{
self.eagerness |= NoChange;
},
Expand Down

0 comments on commit aca4086

Please sign in to comment.