Skip to content

Commit 1f4abb4

Browse files
committed
Use BinOpKind instead of BinOp for function args where possible.
Because it's nice to avoid passing in unnecessary data.
1 parent 978ed96 commit 1f4abb4

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

clippy_lints/src/missing_asserts_for_indexing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::{LitKind, RangeLimits};
1010
use rustc_data_structures::packed::Pu128;
1111
use rustc_data_structures::unhash::UnindexMap;
1212
use rustc_errors::{Applicability, Diag};
13-
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp};
13+
use rustc_hir::{BinOpKind, Block, Body, Expr, ExprKind, UnOp};
1414
use rustc_lint::{LateContext, LateLintPass};
1515
use rustc_session::declare_lint_pass;
1616
use rustc_span::source_map::Spanned;
@@ -97,7 +97,7 @@ enum LengthComparison {
9797
///
9898
/// E.g. for `v.len() > 5` this returns `Some((LengthComparison::IntLessThanLength, 5, v.len()))`
9999
fn len_comparison<'hir>(
100-
bin_op: BinOp,
100+
bin_op: BinOpKind,
101101
left: &'hir Expr<'hir>,
102102
right: &'hir Expr<'hir>,
103103
) -> Option<(LengthComparison, usize, &'hir Expr<'hir>)> {
@@ -112,7 +112,7 @@ fn len_comparison<'hir>(
112112

113113
// normalize comparison, `v.len() > 4` becomes `4 < v.len()`
114114
// this simplifies the logic a bit
115-
let (op, left, right) = normalize_comparison(bin_op.node, left, right)?;
115+
let (op, left, right) = normalize_comparison(bin_op, left, right)?;
116116
match (op, left.kind, right.kind) {
117117
(Rel::Lt, int_lit_pat!(left), _) => Some((LengthComparison::IntLessThanLength, left as usize, right)),
118118
(Rel::Lt, _, int_lit_pat!(right)) => Some((LengthComparison::LengthLessThanInt, right as usize, left)),
@@ -138,7 +138,7 @@ fn assert_len_expr<'hir>(
138138
&& let ExprKind::Unary(UnOp::Not, condition) = &cond.kind
139139
&& let ExprKind::Binary(bin_op, left, right) = &condition.kind
140140

141-
&& let Some((cmp, asserted_len, slice_len)) = len_comparison(*bin_op, left, right)
141+
&& let Some((cmp, asserted_len, slice_len)) = len_comparison(bin_op.node, left, right)
142142
&& let ExprKind::MethodCall(method, recv, [], _) = &slice_len.kind
143143
&& cx.typeck_results().expr_ty_adjusted(recv).peel_refs().is_slice()
144144
&& method.ident.name == sym::len

clippy_utils/src/consts.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_apfloat::ieee::{Half, Quad};
1515
use rustc_ast::ast::{self, LitFloatType, LitKind};
1616
use rustc_hir::def::{DefKind, Res};
1717
use rustc_hir::{
18-
BinOp, BinOpKind, Block, ConstBlock, Expr, ExprKind, HirId, Item, ItemKind, Node, PatExpr, PatExprKind, QPath, UnOp,
18+
BinOpKind, Block, ConstBlock, Expr, ExprKind, HirId, Item, ItemKind, Node, PatExpr, PatExprKind, QPath, UnOp,
1919
};
2020
use rustc_lexer::tokenize;
2121
use rustc_lint::LateContext;
@@ -506,7 +506,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
506506
UnOp::Deref => Some(if let Constant::Ref(r) = o { *r } else { o }),
507507
}),
508508
ExprKind::If(cond, then, ref otherwise) => self.ifthenelse(cond, then, *otherwise),
509-
ExprKind::Binary(op, left, right) => self.binop(op, left, right),
509+
ExprKind::Binary(op, left, right) => self.binop(op.node, left, right),
510510
ExprKind::Call(callee, []) => {
511511
// We only handle a few const functions for now.
512512
if let ExprKind::Path(qpath) = &callee.kind
@@ -744,7 +744,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
744744
}
745745
}
746746

747-
fn binop(&self, op: BinOp, left: &Expr<'_>, right: &Expr<'_>) -> Option<Constant<'tcx>> {
747+
fn binop(&self, op: BinOpKind, left: &Expr<'_>, right: &Expr<'_>) -> Option<Constant<'tcx>> {
748748
let l = self.expr(left)?;
749749
let r = self.expr(right);
750750
match (l, r) {
@@ -757,15 +757,15 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
757757

758758
// Using / or %, where the left-hand argument is the smallest integer of a signed integer type and
759759
// the right-hand argument is -1 always panics, even with overflow-checks disabled
760-
if let BinOpKind::Div | BinOpKind::Rem = op.node
760+
if let BinOpKind::Div | BinOpKind::Rem = op
761761
&& l == ty_min_value
762762
&& r == -1
763763
{
764764
return None;
765765
}
766766

767767
let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
768-
match op.node {
768+
match op {
769769
// When +, * or binary - create a value greater than the maximum value, or less than
770770
// the minimum value that can be stored, it panics.
771771
BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(zext),
@@ -792,7 +792,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
792792
ty::Uint(ity) => {
793793
let bits = ity.bits();
794794

795-
match op.node {
795+
match op {
796796
BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
797797
BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
798798
BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
@@ -815,7 +815,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
815815
_ => None,
816816
},
817817
// FIXME(f16_f128): add these types when binary operations are available on all platforms
818-
(Constant::F32(l), Some(Constant::F32(r))) => match op.node {
818+
(Constant::F32(l), Some(Constant::F32(r))) => match op {
819819
BinOpKind::Add => Some(Constant::F32(l + r)),
820820
BinOpKind::Sub => Some(Constant::F32(l - r)),
821821
BinOpKind::Mul => Some(Constant::F32(l * r)),
@@ -829,7 +829,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
829829
BinOpKind::Gt => Some(Constant::Bool(l > r)),
830830
_ => None,
831831
},
832-
(Constant::F64(l), Some(Constant::F64(r))) => match op.node {
832+
(Constant::F64(l), Some(Constant::F64(r))) => match op {
833833
BinOpKind::Add => Some(Constant::F64(l + r)),
834834
BinOpKind::Sub => Some(Constant::F64(l - r)),
835835
BinOpKind::Mul => Some(Constant::F64(l * r)),
@@ -843,7 +843,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
843843
BinOpKind::Gt => Some(Constant::Bool(l > r)),
844844
_ => None,
845845
},
846-
(l, r) => match (op.node, l, r) {
846+
(l, r) => match (op, l, r) {
847847
(BinOpKind::And, Constant::Bool(false), _) => Some(Constant::Bool(false)),
848848
(BinOpKind::Or, Constant::Bool(true), _) => Some(Constant::Bool(true)),
849849
(BinOpKind::And, Constant::Bool(true), Some(r)) | (BinOpKind::Or, Constant::Bool(false), Some(r)) => {

0 commit comments

Comments
 (0)