1
Fork 0

BinOpKind

This commit is contained in:
csmoe 2018-07-12 15:50:09 +08:00 committed by Oliver Schneider
parent 1bd17e4fa2
commit 5d4102ee78
36 changed files with 260 additions and 248 deletions

View file

@ -57,19 +57,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
match expr.node { match expr.node {
hir::ExprKind::Binary(ref op, ref l, ref r) => { hir::ExprKind::Binary(ref op, ref l, ref r) => {
match op.node { match op.node {
hir::BiAnd hir::BinOpKind::And
| hir::BiOr | hir::BinOpKind::Or
| hir::BiBitAnd | hir::BinOpKind::BitAnd
| hir::BiBitOr | hir::BinOpKind::BitOr
| hir::BiBitXor | hir::BinOpKind::BitXor
| hir::BiShl | hir::BinOpKind::Shl
| hir::BiShr | hir::BinOpKind::Shr
| hir::BiEq | hir::BinOpKind::Eq
| hir::BiLt | hir::BinOpKind::Lt
| hir::BiLe | hir::BinOpKind::Le
| hir::BiNe | hir::BinOpKind::Ne
| hir::BiGe | hir::BinOpKind::Ge
| hir::BiGt => return, | hir::BinOpKind::Gt => return,
_ => (), _ => (),
} }
let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r)); let (l_ty, r_ty) = (cx.tables.expr_ty(l), cx.tables.expr_ty(r));

View file

@ -175,18 +175,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
cx, cx,
ty, ty,
rty.into(), rty.into(),
Add: BiAdd, Add: BinOpKind::Add,
Sub: BiSub, Sub: BinOpKind::Sub,
Mul: BiMul, Mul: BinOpKind::Mul,
Div: BiDiv, Div: BinOpKind::Div,
Rem: BiRem, Rem: BinOpKind::Rem,
And: BiAnd, And: BinOpKind::And,
Or: BiOr, Or: BinOpKind::Or,
BitAnd: BiBitAnd, BitAnd: BinOpKind::BitAnd,
BitOr: BiBitOr, BitOr: BinOpKind::BitOr,
BitXor: BiBitXor, BitXor: BinOpKind::BitXor,
Shr: BiShr, Shr: BinOpKind::Shr,
Shl: BiShl Shl: BinOpKind::Shl
) { ) {
span_lint_and_then( span_lint_and_then(
cx, cx,
@ -224,13 +224,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
// a = b commutative_op a // a = b commutative_op a
if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) { if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r) {
match op.node { match op.node {
hir::BiAdd hir::BinOpKind::Add
| hir::BiMul | hir::BinOpKind::Mul
| hir::BiAnd | hir::BinOpKind::And
| hir::BiOr | hir::BinOpKind::Or
| hir::BiBitXor | hir::BinOpKind::BitXor
| hir::BiBitAnd | hir::BinOpKind::BitAnd
| hir::BiBitOr => { | hir::BinOpKind::BitOr => {
lint(assignee, l); lint(assignee, l);
}, },
_ => {}, _ => {},
@ -244,11 +244,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
} }
} }
fn is_commutative(op: hir::BinOp_) -> bool { fn is_commutative(op: hir::BinOpKind) -> bool {
use rustc::hir::BinOp_::*; use rustc::hir::BinOpKind::*;
match op { match op {
BiAdd | BiMul | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr | BiEq | BiNe => true, Add | Mul | And | Or | BitXor | BitAnd | BitOr | Eq | Ne => true,
BiSub | BiDiv | BiRem | BiShl | BiShr | BiLt | BiLe | BiGe | BiGt => false, Sub | Div | Rem | Shl | Shr | Lt | Le | Ge | Gt => false,
} }
} }

View file

@ -120,9 +120,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
} }
if_chain! { if_chain! {
if let ExprKind::Binary(ref op, ref left, ref right) = e.node; if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
if BinOp_::BiEq == op.node; if BinOpKind::Eq == op.node;
if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node; if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node;
if BinOp_::BiBitAnd == op1.node; if BinOpKind::BitAnd == op1.node;
if let ExprKind::Lit(ref lit) = right1.node; if let ExprKind::Lit(ref lit) = right1.node;
if let LitKind::Int(n, _) = lit.node; if let LitKind::Int(n, _) = lit.node;
if let ExprKind::Lit(ref lit1) = right.node; if let ExprKind::Lit(ref lit1) = right.node;
@ -143,22 +143,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
} }
} }
fn invert_cmp(cmp: BinOp_) -> BinOp_ { fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
match cmp { match cmp {
BiEq => BiEq, BinOpKind::Eq => BinOpKind::Eq,
BiNe => BiNe, BinOpKind::Ne => BinOpKind::Ne,
BiLt => BiGt, BinOpKind::Lt => BinOpKind::Gt,
BiGt => BiLt, BinOpKind::Gt => BinOpKind::Lt,
BiLe => BiGe, BinOpKind::Le => BinOpKind::Ge,
BiGe => BiLe, BinOpKind::Ge => BinOpKind::Le,
_ => BiOr, // Dummy _ => BinOpKind::Or, // Dummy
} }
} }
fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u128, span: Span) { fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
if let ExprKind::Binary(ref op, ref left, ref right) = bit_op.node { if let ExprKind::Binary(ref op, ref left, ref right) = bit_op.node {
if op.node != BiBitAnd && op.node != BiBitOr { if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
return; return;
} }
fetch_int_literal(cx, right) fetch_int_literal(cx, right)
@ -167,10 +167,10 @@ fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u12
} }
} }
fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u128, cmp_value: u128, span: Span) { fn check_bit_mask(cx: &LateContext, bit_op: BinOpKind, cmp_op: BinOpKind, mask_value: u128, cmp_value: u128, span: Span) {
match cmp_op { match cmp_op {
BiEq | BiNe => match bit_op { BinOpKind::Eq | BinOpKind::Ne => match bit_op {
BiBitAnd => if mask_value & cmp_value != cmp_value { BinOpKind::BitAnd => if mask_value & cmp_value != cmp_value {
if cmp_value != 0 { if cmp_value != 0 {
span_lint( span_lint(
cx, cx,
@ -186,7 +186,7 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
} else if mask_value == 0 { } else if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero"); span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
}, },
BiBitOr => if mask_value | cmp_value != cmp_value { BinOpKind::BitOr => if mask_value | cmp_value != cmp_value {
span_lint( span_lint(
cx, cx,
BAD_BIT_MASK, BAD_BIT_MASK,
@ -200,8 +200,8 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
}, },
_ => (), _ => (),
}, },
BiLt | BiGe => match bit_op { BinOpKind::Lt | BinOpKind::Ge => match bit_op {
BiBitAnd => if mask_value < cmp_value { BinOpKind::BitAnd => if mask_value < cmp_value {
span_lint( span_lint(
cx, cx,
BAD_BIT_MASK, BAD_BIT_MASK,
@ -215,7 +215,7 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
} else if mask_value == 0 { } else if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero"); span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
}, },
BiBitOr => if mask_value >= cmp_value { BinOpKind::BitOr => if mask_value >= cmp_value {
span_lint( span_lint(
cx, cx,
BAD_BIT_MASK, BAD_BIT_MASK,
@ -229,11 +229,11 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
} else { } else {
check_ineffective_lt(cx, span, mask_value, cmp_value, "|"); check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
}, },
BiBitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"), BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
_ => (), _ => (),
}, },
BiLe | BiGt => match bit_op { BinOpKind::Le | BinOpKind::Gt => match bit_op {
BiBitAnd => if mask_value <= cmp_value { BinOpKind::BitAnd => if mask_value <= cmp_value {
span_lint( span_lint(
cx, cx,
BAD_BIT_MASK, BAD_BIT_MASK,
@ -247,7 +247,7 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
} else if mask_value == 0 { } else if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero"); span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
}, },
BiBitOr => if mask_value > cmp_value { BinOpKind::BitOr => if mask_value > cmp_value {
span_lint( span_lint(
cx, cx,
BAD_BIT_MASK, BAD_BIT_MASK,
@ -261,7 +261,7 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
} else { } else {
check_ineffective_gt(cx, span, mask_value, cmp_value, "|"); check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
}, },
BiBitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"), BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
_ => (), _ => (),
}, },
_ => (), _ => (),

View file

@ -84,7 +84,7 @@ struct Hir2Qmm<'a, 'tcx: 'a, 'v> {
} }
impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> { impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
fn extract(&mut self, op: BinOp_, a: &[&'v Expr], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> { fn extract(&mut self, op: BinOpKind, a: &[&'v Expr], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
for a in a { for a in a {
if let ExprKind::Binary(binop, ref lhs, ref rhs) = a.node { if let ExprKind::Binary(binop, ref lhs, ref rhs) = a.node {
if binop.node == op { if binop.node == op {
@ -103,8 +103,8 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
match e.node { match e.node {
ExprKind::Unary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)), ExprKind::Unary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)),
ExprKind::Binary(binop, ref lhs, ref rhs) => match binop.node { ExprKind::Binary(binop, ref lhs, ref rhs) => match binop.node {
BiOr => return Ok(Bool::Or(self.extract(BiOr, &[lhs, rhs], Vec::new())?)), BinOpKind::Or => return Ok(Bool::Or(self.extract(BinOpKind::Or, &[lhs, rhs], Vec::new())?)),
BiAnd => return Ok(Bool::And(self.extract(BiAnd, &[lhs, rhs], Vec::new())?)), BinOpKind::And => return Ok(Bool::And(self.extract(BinOpKind::And, &[lhs, rhs], Vec::new())?)),
_ => (), _ => (),
}, },
ExprKind::Lit(ref lit) => match lit.node { ExprKind::Lit(ref lit) => match lit.node {
@ -137,12 +137,12 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
} }
}; };
match binop.node { match binop.node {
BiEq => mk_expr(BiNe), BinOpKind::Eq => mk_expr(BinOpKind::Ne),
BiNe => mk_expr(BiEq), BinOpKind::Ne => mk_expr(BinOpKind::Eq),
BiGt => mk_expr(BiLe), BinOpKind::Gt => mk_expr(BinOpKind::Le),
BiGe => mk_expr(BiLt), BinOpKind::Ge => mk_expr(BinOpKind::Lt),
BiLt => mk_expr(BiGe), BinOpKind::Lt => mk_expr(BinOpKind::Ge),
BiLe => mk_expr(BiGt), BinOpKind::Le => mk_expr(BinOpKind::Gt),
_ => continue, _ => continue,
} }
}, },
@ -185,12 +185,12 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
} }
match binop.node { match binop.node {
BiEq => Some(" != "), BinOpKind::Eq => Some(" != "),
BiNe => Some(" == "), BinOpKind::Ne => Some(" == "),
BiLt => Some(" >= "), BinOpKind::Lt => Some(" >= "),
BiGt => Some(" <= "), BinOpKind::Gt => Some(" <= "),
BiLe => Some(" > "), BinOpKind::Le => Some(" > "),
BiGe => Some(" < "), BinOpKind::Ge => Some(" < "),
_ => None, _ => None,
}.and_then(|op| Some(format!("{}{}{}", self.snip(lhs)?, op, self.snip(rhs)?))) }.and_then(|op| Some(format!("{}{}{}", self.snip(lhs)?, op, self.snip(rhs)?)))
}, },
@ -441,7 +441,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
return; return;
} }
match e.node { match e.node {
ExprKind::Binary(binop, _, _) if binop.node == BiOr || binop.node == BiAnd => self.bool_expr(e), ExprKind::Binary(binop, _, _) if binop.node == BinOpKind::Or || binop.node == BinOpKind::And => self.bool_expr(e),
ExprKind::Unary(UnNot, ref inner) => if self.cx.tables.node_types()[inner.hir_id].is_bool() { ExprKind::Unary(UnNot, ref inner) => if self.cx.tables.node_types()[inner.hir_id].is_bool() {
self.bool_expr(e); self.bool_expr(e);
} else { } else {

View file

@ -51,7 +51,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
if body.arguments.len() == 1; if body.arguments.len() == 1;
if let Some(argname) = get_pat_name(&body.arguments[0].pat); if let Some(argname) = get_pat_name(&body.arguments[0].pat);
if let ExprKind::Binary(ref op, ref l, ref r) = body.value.node; if let ExprKind::Binary(ref op, ref l, ref r) = body.value.node;
if op.node == BiEq; if op.node == BinOpKind::Eq;
if match_type(cx, if match_type(cx,
walk_ptrs_ty(cx.tables.expr_ty(&filter_args[0])), walk_ptrs_ty(cx.tables.expr_ty(&filter_args[0])),
&paths::SLICE_ITER); &paths::SLICE_ITER);

View file

@ -340,43 +340,43 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
let r = sext(self.tcx, r, ity); let r = sext(self.tcx, r, ity);
let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity)); let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
match op.node { match op.node {
BiAdd => l.checked_add(r).map(zext), BinOpKind::Add => l.checked_add(r).map(zext),
BiSub => l.checked_sub(r).map(zext), BinOpKind::Sub => l.checked_sub(r).map(zext),
BiMul => l.checked_mul(r).map(zext), BinOpKind::Mul => l.checked_mul(r).map(zext),
BiDiv if r != 0 => l.checked_div(r).map(zext), BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
BiRem if r != 0 => l.checked_rem(r).map(zext), BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
BiShr => l.checked_shr(r as u128 as u32).map(zext), BinOpKind::Shr => l.checked_shr(r as u128 as u32).map(zext),
BiShl => l.checked_shl(r as u128 as u32).map(zext), BinOpKind::Shl => l.checked_shl(r as u128 as u32).map(zext),
BiBitXor => Some(zext(l ^ r)), BinOpKind::BitXor => Some(zext(l ^ r)),
BiBitOr => Some(zext(l | r)), BinOpKind::BitOr => Some(zext(l | r)),
BiBitAnd => Some(zext(l & r)), BinOpKind::BitAnd => Some(zext(l & r)),
BiEq => Some(Constant::Bool(l == r)), BinOpKind::Eq => Some(Constant::Bool(l == r)),
BiNe => Some(Constant::Bool(l != r)), BinOpKind::Ne => Some(Constant::Bool(l != r)),
BiLt => Some(Constant::Bool(l < r)), BinOpKind::Lt => Some(Constant::Bool(l < r)),
BiLe => Some(Constant::Bool(l <= r)), BinOpKind::Le => Some(Constant::Bool(l <= r)),
BiGe => Some(Constant::Bool(l >= r)), BinOpKind::Ge => Some(Constant::Bool(l >= r)),
BiGt => Some(Constant::Bool(l > r)), BinOpKind::Gt => Some(Constant::Bool(l > r)),
_ => None, _ => None,
} }
} }
ty::TyUint(_) => { ty::TyUint(_) => {
match op.node { match op.node {
BiAdd => l.checked_add(r).map(Constant::Int), BinOpKind::Add => l.checked_add(r).map(Constant::Int),
BiSub => l.checked_sub(r).map(Constant::Int), BinOpKind::Sub => l.checked_sub(r).map(Constant::Int),
BiMul => l.checked_mul(r).map(Constant::Int), BinOpKind::Mul => l.checked_mul(r).map(Constant::Int),
BiDiv => l.checked_div(r).map(Constant::Int), BinOpKind::Div => l.checked_div(r).map(Constant::Int),
BiRem => l.checked_rem(r).map(Constant::Int), BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
BiShr => l.checked_shr(r as u32).map(Constant::Int), BinOpKind::Shr => l.checked_shr(r as u32).map(Constant::Int),
BiShl => l.checked_shl(r as u32).map(Constant::Int), BinOpKind::Shl => l.checked_shl(r as u32).map(Constant::Int),
BiBitXor => Some(Constant::Int(l ^ r)), BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
BiBitOr => Some(Constant::Int(l | r)), BinOpKind::BitOr => Some(Constant::Int(l | r)),
BiBitAnd => Some(Constant::Int(l & r)), BinOpKind::BitAnd => Some(Constant::Int(l & r)),
BiEq => Some(Constant::Bool(l == r)), BinOpKind::Eq => Some(Constant::Bool(l == r)),
BiNe => Some(Constant::Bool(l != r)), BinOpKind::Ne => Some(Constant::Bool(l != r)),
BiLt => Some(Constant::Bool(l < r)), BinOpKind::Lt => Some(Constant::Bool(l < r)),
BiLe => Some(Constant::Bool(l <= r)), BinOpKind::Le => Some(Constant::Bool(l <= r)),
BiGe => Some(Constant::Bool(l >= r)), BinOpKind::Ge => Some(Constant::Bool(l >= r)),
BiGt => Some(Constant::Bool(l > r)), BinOpKind::Gt => Some(Constant::Bool(l > r)),
_ => None, _ => None,
} }
}, },
@ -384,40 +384,40 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
} }
}, },
(Constant::F32(l), Some(Constant::F32(r))) => match op.node { (Constant::F32(l), Some(Constant::F32(r))) => match op.node {
BiAdd => Some(Constant::F32(l + r)), BinOpKind::Add => Some(Constant::F32(l + r)),
BiSub => Some(Constant::F32(l - r)), BinOpKind::Sub => Some(Constant::F32(l - r)),
BiMul => Some(Constant::F32(l * r)), BinOpKind::Mul => Some(Constant::F32(l * r)),
BiDiv => Some(Constant::F32(l / r)), BinOpKind::Div => Some(Constant::F32(l / r)),
BiRem => Some(Constant::F32(l % r)), BinOpKind::Rem => Some(Constant::F32(l % r)),
BiEq => Some(Constant::Bool(l == r)), BinOpKind::Eq => Some(Constant::Bool(l == r)),
BiNe => Some(Constant::Bool(l != r)), BinOpKind::Ne => Some(Constant::Bool(l != r)),
BiLt => Some(Constant::Bool(l < r)), BinOpKind::Lt => Some(Constant::Bool(l < r)),
BiLe => Some(Constant::Bool(l <= r)), BinOpKind::Le => Some(Constant::Bool(l <= r)),
BiGe => Some(Constant::Bool(l >= r)), BinOpKind::Ge => Some(Constant::Bool(l >= r)),
BiGt => Some(Constant::Bool(l > r)), BinOpKind::Gt => Some(Constant::Bool(l > r)),
_ => None, _ => None,
}, },
(Constant::F64(l), Some(Constant::F64(r))) => match op.node { (Constant::F64(l), Some(Constant::F64(r))) => match op.node {
BiAdd => Some(Constant::F64(l + r)), BinOpKind::Add => Some(Constant::F64(l + r)),
BiSub => Some(Constant::F64(l - r)), BinOpKind::Sub => Some(Constant::F64(l - r)),
BiMul => Some(Constant::F64(l * r)), BinOpKind::Mul => Some(Constant::F64(l * r)),
BiDiv => Some(Constant::F64(l / r)), BinOpKind::Div => Some(Constant::F64(l / r)),
BiRem => Some(Constant::F64(l % r)), BinOpKind::Rem => Some(Constant::F64(l % r)),
BiEq => Some(Constant::Bool(l == r)), BinOpKind::Eq => Some(Constant::Bool(l == r)),
BiNe => Some(Constant::Bool(l != r)), BinOpKind::Ne => Some(Constant::Bool(l != r)),
BiLt => Some(Constant::Bool(l < r)), BinOpKind::Lt => Some(Constant::Bool(l < r)),
BiLe => Some(Constant::Bool(l <= r)), BinOpKind::Le => Some(Constant::Bool(l <= r)),
BiGe => Some(Constant::Bool(l >= r)), BinOpKind::Ge => Some(Constant::Bool(l >= r)),
BiGt => Some(Constant::Bool(l > r)), BinOpKind::Gt => Some(Constant::Bool(l > r)),
_ => None, _ => None,
}, },
(l, r) => match (op.node, l, r) { (l, r) => match (op.node, l, r) {
(BiAnd, Constant::Bool(false), _) => Some(Constant::Bool(false)), (BinOpKind::And, Constant::Bool(false), _) => Some(Constant::Bool(false)),
(BiOr, Constant::Bool(true), _) => Some(Constant::Bool(true)), (BinOpKind::Or, Constant::Bool(true), _) => Some(Constant::Bool(true)),
(BiAnd, Constant::Bool(true), Some(r)) | (BiOr, Constant::Bool(false), Some(r)) => Some(r), (BinOpKind::And, Constant::Bool(true), Some(r)) | (BinOpKind::Or, Constant::Bool(false), Some(r)) => Some(r),
(BiBitXor, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l ^ r)), (BinOpKind::BitXor, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l ^ r)),
(BiBitAnd, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l & r)), (BinOpKind::BitAnd, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l & r)),
(BiBitOr, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l | r)), (BinOpKind::BitOr, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l | r)),
_ => None, _ => None,
}, },
} }

View file

@ -171,7 +171,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CCHelper<'a, 'tcx> {
ExprKind::Binary(op, _, _) => { ExprKind::Binary(op, _, _) => {
walk_expr(self, e); walk_expr(self, e);
match op.node { match op.node {
BiAnd | BiOr => self.short_circuits += 1, BinOpKind::And | BinOpKind::Or => self.short_circuits += 1,
_ => (), _ => (),
} }
}, },

View file

@ -41,7 +41,7 @@ impl<'a, 'tcx> DoubleComparisonPass {
fn check_binop( fn check_binop(
&self, &self,
cx: &LateContext<'a, 'tcx>, cx: &LateContext<'a, 'tcx>,
op: BinOp_, op: BinOpKind,
lhs: &'tcx Expr, lhs: &'tcx Expr,
rhs: &'tcx Expr, rhs: &'tcx Expr,
span: Span, span: Span,
@ -67,10 +67,10 @@ impl<'a, 'tcx> DoubleComparisonPass {
}} }}
} }
match (op, lkind, rkind) { match (op, lkind, rkind) {
(BiOr, BiEq, BiLt) | (BiOr, BiLt, BiEq) => lint_double_comparison!(<=), (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Lt) | (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Eq) => lint_double_comparison!(<=),
(BiOr, BiEq, BiGt) | (BiOr, BiGt, BiEq) => lint_double_comparison!(>=), (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Eq) => lint_double_comparison!(>=),
(BiOr, BiLt, BiGt) | (BiOr, BiGt, BiLt) => lint_double_comparison!(!=), (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Lt) => lint_double_comparison!(!=),
(BiAnd, BiLe, BiGe) | (BiAnd, BiGe, BiLe) => lint_double_comparison!(==), (BinOpKind::And, BinOpKind::Le, BinOpKind::Ge) | (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => lint_double_comparison!(==),
_ => (), _ => (),
}; };
} }

View file

@ -38,7 +38,7 @@ impl LintPass for DurationSubsec {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if_chain! { if_chain! {
if let ExprKind::Binary(Spanned { node: BiDiv, .. }, ref left, ref right) = expr.node; if let ExprKind::Binary(Spanned { node: BinOpKind::Div, .. }, ref left, ref right) = expr.node;
if let ExprKind::MethodCall(ref method_path, _ , ref args) = left.node; if let ExprKind::MethodCall(ref method_path, _ , ref args) = left.node;
if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(&args[0])), &paths::DURATION); if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(&args[0])), &paths::DURATION);
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.tables, right); if let Some((Constant::Int(divisor), _)) = constant(cx, cx.tables, right);

View file

@ -66,20 +66,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
return; return;
} }
let (trait_id, requires_ref) = match op.node { let (trait_id, requires_ref) = match op.node {
BiAdd => (cx.tcx.lang_items().add_trait(), false), BinOpKind::Add => (cx.tcx.lang_items().add_trait(), false),
BiSub => (cx.tcx.lang_items().sub_trait(), false), BinOpKind::Sub => (cx.tcx.lang_items().sub_trait(), false),
BiMul => (cx.tcx.lang_items().mul_trait(), false), BinOpKind::Mul => (cx.tcx.lang_items().mul_trait(), false),
BiDiv => (cx.tcx.lang_items().div_trait(), false), BinOpKind::Div => (cx.tcx.lang_items().div_trait(), false),
BiRem => (cx.tcx.lang_items().rem_trait(), false), BinOpKind::Rem => (cx.tcx.lang_items().rem_trait(), false),
// don't lint short circuiting ops // don't lint short circuiting ops
BiAnd | BiOr => return, BinOpKind::And | BinOpKind::Or => return,
BiBitXor => (cx.tcx.lang_items().bitxor_trait(), false), BinOpKind::BitXor => (cx.tcx.lang_items().bitxor_trait(), false),
BiBitAnd => (cx.tcx.lang_items().bitand_trait(), false), BinOpKind::BitAnd => (cx.tcx.lang_items().bitand_trait(), false),
BiBitOr => (cx.tcx.lang_items().bitor_trait(), false), BinOpKind::BitOr => (cx.tcx.lang_items().bitor_trait(), false),
BiShl => (cx.tcx.lang_items().shl_trait(), false), BinOpKind::Shl => (cx.tcx.lang_items().shl_trait(), false),
BiShr => (cx.tcx.lang_items().shr_trait(), false), BinOpKind::Shr => (cx.tcx.lang_items().shr_trait(), false),
BiNe | BiEq => (cx.tcx.lang_items().eq_trait(), true), BinOpKind::Ne | BinOpKind::Eq => (cx.tcx.lang_items().eq_trait(), true),
BiLt | BiLe | BiGe | BiGt => (cx.tcx.lang_items().ord_trait(), true), BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ge | BinOpKind::Gt => (cx.tcx.lang_items().ord_trait(), true),
}; };
if let Some(trait_id) = trait_id { if let Some(trait_id) = trait_id {
#[allow(match_same_arms)] #[allow(match_same_arms)]
@ -159,7 +159,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
fn is_valid_operator(op: BinOp) -> bool { fn is_valid_operator(op: BinOp) -> bool {
match op.node { match op.node {
BiSub | BiDiv | BiEq | BiLt | BiLe | BiGt | BiGe | BiNe | BiAnd | BiOr | BiBitXor | BiBitAnd | BiBitOr => true, BinOpKind::Sub | BinOpKind::Div | BinOpKind::Eq | BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge | BinOpKind::Ne | BinOpKind::And | BinOpKind::Or | BinOpKind::BitXor | BinOpKind::BitAnd | BinOpKind::BitOr => true,
_ => false, _ => false,
} }
} }

View file

@ -38,11 +38,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
} }
if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node { if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
match cmp.node { match cmp.node {
BiMul | BiBitAnd => { BinOpKind::Mul | BinOpKind::BitAnd => {
check(cx, left, e.span); check(cx, left, e.span);
check(cx, right, e.span); check(cx, right, e.span);
}, },
BiDiv => check(cx, left, e.span), BinOpKind::Div => check(cx, left, e.span),
_ => (), _ => (),
} }
} }

View file

@ -229,7 +229,7 @@ fn check_expr<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, expr: &'tcx Expr) -> St
walk_expr(vis, expr); walk_expr(vis, expr);
}, },
ExprKind::Binary(op, _, _) | ExprKind::AssignOp(op, _, _) => { ExprKind::Binary(op, _, _) | ExprKind::AssignOp(op, _, _) => {
if op.node == BiAnd || op.node == BiOr { if op.node == BinOpKind::And || op.node == BinOpKind::Or {
// x && y and x || y always evaluate x first, so these are // x && y and x || y always evaluate x first, so these are
// strictly sequenced. // strictly sequenced.
} else { } else {

View file

@ -38,17 +38,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
} }
if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node { if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
match cmp.node { match cmp.node {
BiAdd | BiBitOr | BiBitXor => { BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
check(cx, left, 0, e.span, right.span); check(cx, left, 0, e.span, right.span);
check(cx, right, 0, e.span, left.span); check(cx, right, 0, e.span, left.span);
}, },
BiShl | BiShr | BiSub => check(cx, right, 0, e.span, left.span), BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
BiMul => { BinOpKind::Mul => {
check(cx, left, 1, e.span, right.span); check(cx, left, 1, e.span, right.span);
check(cx, right, 1, e.span, left.span); check(cx, right, 1, e.span, left.span);
}, },
BiDiv => check(cx, right, 1, e.span, left.span), BinOpKind::Div => check(cx, right, 1, e.span, left.span),
BiBitAnd => { BinOpKind::BitAnd => {
check(cx, left, -1, e.span, right.span); check(cx, left, -1, e.span, right.span);
check(cx, right, -1, e.span, left.span); check(cx, right, -1, e.span, left.span);
}, },

View file

@ -81,24 +81,24 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LenZero {
if let ExprKind::Binary(Spanned { node: cmp, .. }, ref left, ref right) = expr.node { if let ExprKind::Binary(Spanned { node: cmp, .. }, ref left, ref right) = expr.node {
match cmp { match cmp {
BiEq => { BinOpKind::Eq => {
check_cmp(cx, expr.span, left, right, "", 0); // len == 0 check_cmp(cx, expr.span, left, right, "", 0); // len == 0
check_cmp(cx, expr.span, right, left, "", 0); // 0 == len check_cmp(cx, expr.span, right, left, "", 0); // 0 == len
}, },
BiNe => { BinOpKind::Ne => {
check_cmp(cx, expr.span, left, right, "!", 0); // len != 0 check_cmp(cx, expr.span, left, right, "!", 0); // len != 0
check_cmp(cx, expr.span, right, left, "!", 0); // 0 != len check_cmp(cx, expr.span, right, left, "!", 0); // 0 != len
}, },
BiGt => { BinOpKind::Gt => {
check_cmp(cx, expr.span, left, right, "!", 0); // len > 0 check_cmp(cx, expr.span, left, right, "!", 0); // len > 0
check_cmp(cx, expr.span, right, left, "", 1); // 1 > len check_cmp(cx, expr.span, right, left, "", 1); // 1 > len
}, },
BiLt => { BinOpKind::Lt => {
check_cmp(cx, expr.span, left, right, "", 1); // len < 1 check_cmp(cx, expr.span, left, right, "", 1); // len < 1
check_cmp(cx, expr.span, right, left, "!", 0); // 0 < len check_cmp(cx, expr.span, right, left, "!", 0); // 0 < len
}, },
BiGe => check_cmp(cx, expr.span, left, right, "!", 1), // len <= 1 BinOpKind::Ge => check_cmp(cx, expr.span, left, right, "!", 1), // len <= 1
BiLe => check_cmp(cx, expr.span, right, left, "!", 1), // 1 >= len BinOpKind::Le => check_cmp(cx, expr.span, right, left, "!", 1), // 1 >= len
_ => (), _ => (),
} }
} }

View file

@ -771,7 +771,7 @@ fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr, var:
let offset = match idx.node { let offset = match idx.node {
ExprKind::Binary(op, ref lhs, ref rhs) => match op.node { ExprKind::Binary(op, ref lhs, ref rhs) => match op.node {
BinOp_::BiAdd => { BinOpKindAdd => {
let offset_opt = if same_var(cx, lhs, var) { let offset_opt = if same_var(cx, lhs, var) {
extract_offset(cx, rhs, var) extract_offset(cx, rhs, var)
} else if same_var(cx, rhs, var) { } else if same_var(cx, rhs, var) {
@ -782,7 +782,7 @@ fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr, var:
offset_opt.map(Offset::positive) offset_opt.map(Offset::positive)
}, },
BinOp_::BiSub if same_var(cx, lhs, var) => extract_offset(cx, rhs, var).map(Offset::negative), BinOpKind::Sub if same_var(cx, lhs, var) => extract_offset(cx, rhs, var).map(Offset::negative),
_ => None, _ => None,
}, },
ExprKind::Path(..) => if same_var(cx, idx, var) { ExprKind::Path(..) => if same_var(cx, idx, var) {
@ -1884,7 +1884,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
match parent.node { match parent.node {
ExprKind::AssignOp(op, ref lhs, ref rhs) => { ExprKind::AssignOp(op, ref lhs, ref rhs) => {
if lhs.id == expr.id { if lhs.id == expr.id {
if op.node == BiAdd && is_integer_literal(rhs, 1) { if op.node == BinOpKind::Add && is_integer_literal(rhs, 1) {
*state = match *state { *state = match *state {
VarState::Initial if self.depth == 0 => VarState::IncrOnce, VarState::Initial if self.depth == 0 => VarState::IncrOnce,
_ => VarState::DontWarn, _ => VarState::DontWarn,

View file

@ -789,12 +789,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
_ => (), _ => (),
} }
}, },
hir::ExprKind::Binary(op, ref lhs, ref rhs) if op.node == hir::BiEq || op.node == hir::BiNe => { hir::ExprKind::Binary(op, ref lhs, ref rhs) if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne => {
let mut info = BinaryExprInfo { let mut info = BinaryExprInfo {
expr, expr,
chain: lhs, chain: lhs,
other: rhs, other: rhs,
eq: op.node == hir::BiEq, eq: op.node == hir::BinOpKind::Eq,
}; };
lint_binary_expr_with_method_call(cx, &mut info); lint_binary_expr_with_method_call(cx, &mut info);
}, },
@ -1274,7 +1274,7 @@ fn lint_unnecessary_fold(cx: &LateContext, expr: &hir::Expr, fold_args: &[hir::E
fn check_fold_with_op( fn check_fold_with_op(
cx: &LateContext, cx: &LateContext,
fold_args: &[hir::Expr], fold_args: &[hir::Expr],
op: hir::BinOp_, op: hir::BinOpKind,
replacement_method_name: &str, replacement_method_name: &str,
replacement_has_args: bool) { replacement_has_args: bool) {
@ -1332,16 +1332,16 @@ fn lint_unnecessary_fold(cx: &LateContext, expr: &hir::Expr, fold_args: &[hir::E
hir::ExprKind::Lit(ref lit) => { hir::ExprKind::Lit(ref lit) => {
match lit.node { match lit.node {
ast::LitKind::Bool(false) => check_fold_with_op( ast::LitKind::Bool(false) => check_fold_with_op(
cx, fold_args, hir::BinOp_::BiOr, "any", true cx, fold_args, hir::BinOpKind::Or, "any", true
), ),
ast::LitKind::Bool(true) => check_fold_with_op( ast::LitKind::Bool(true) => check_fold_with_op(
cx, fold_args, hir::BinOp_::BiAnd, "all", true cx, fold_args, hir::BinOpKind::And, "all", true
), ),
ast::LitKind::Int(0, _) => check_fold_with_op( ast::LitKind::Int(0, _) => check_fold_with_op(
cx, fold_args, hir::BinOp_::BiAdd, "sum", false cx, fold_args, hir::BinOpKindAdd, "sum", false
), ),
ast::LitKind::Int(1, _) => check_fold_with_op( ast::LitKind::Int(1, _) => check_fold_with_op(
cx, fold_args, hir::BinOp_::BiMul, "product", false cx, fold_args, hir::BinOpKind::Mul, "product", false
), ),
_ => return _ => return
} }

View file

@ -305,7 +305,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
if_chain! { if_chain! {
if let StmtSemi(ref expr, _) = s.node; if let StmtSemi(ref expr, _) = s.node;
if let ExprKind::Binary(ref binop, ref a, ref b) = expr.node; if let ExprKind::Binary(ref binop, ref a, ref b) = expr.node;
if binop.node == BiAnd || binop.node == BiOr; if binop.node == BinOpKind::And || binop.node == BinOpKind::Or;
if let Some(sugg) = Sugg::hir_opt(cx, a); if let Some(sugg) = Sugg::hir_opt(cx, a);
then { then {
span_lint_and_then(cx, span_lint_and_then(cx,
@ -313,7 +313,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
s.span, s.span,
"boolean short circuit operator in statement may be clearer using an explicit test", "boolean short circuit operator in statement may be clearer using an explicit test",
|db| { |db| {
let sugg = if binop.node == BiOr { !sugg } else { sugg }; let sugg = if binop.node == BinOpKind::Or { !sugg } else { sugg };
db.span_suggestion(s.span, "replace it with", db.span_suggestion(s.span, "replace it with",
format!("if {} {{ {}; }}", sugg, &snippet(cx, b.span, ".."))); format!("if {} {{ {}; }}", sugg, &snippet(cx, b.span, "..")));
}); });
@ -339,7 +339,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
check_to_owned(cx, left, right); check_to_owned(cx, left, right);
check_to_owned(cx, right, left); check_to_owned(cx, right, left);
} }
if (op == BiEq || op == BiNe) && (is_float(cx, left) || is_float(cx, right)) { if (op == BinOpKind::Eq || op == BinOpKind::Ne) && (is_float(cx, left) || is_float(cx, right)) {
if is_allowed(cx, left) || is_allowed(cx, right) { if is_allowed(cx, left) || is_allowed(cx, right) {
return; return;
} }
@ -367,7 +367,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
); );
db.span_note(expr.span, "std::f32::EPSILON and std::f64::EPSILON are available."); db.span_note(expr.span, "std::f32::EPSILON and std::f64::EPSILON are available.");
}); });
} else if op == BiRem && is_integer_literal(right, 1) { } else if op == BinOpKind::Rem && is_integer_literal(right, 1) {
span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0"); span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0");
} }
}, },

View file

@ -123,7 +123,7 @@ impl LintPass for BoolComparison {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
use self::Expression::*; use self::Expression::*;
if let ExprKind::Binary(Spanned { node: BiEq, .. }, ref left_side, ref right_side) = e.node { if let ExprKind::Binary(Spanned { node: BinOpKind::Eq, .. }, ref left_side, ref right_side) = e.node {
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) { match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
(Bool(true), Other) => { (Bool(true), Other) => {
let hint = snippet(cx, right_side.span, "..").into_owned(); let hint = snippet(cx, right_side.span, "..").into_owned();

View file

@ -56,7 +56,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
if !in_external_macro(cx, expr.span); if !in_external_macro(cx, expr.span);
if let ExprKind::Unary(UnOp::UnNot, ref inner) = expr.node; if let ExprKind::Unary(UnOp::UnNot, ref inner) = expr.node;
if let ExprKind::Binary(ref op, ref left, _) = inner.node; if let ExprKind::Binary(ref op, ref left, _) = inner.node;
if let BinOp_::BiLe | BinOp_::BiGe | BinOp_::BiLt | BinOp_::BiGt = op.node; if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;
then { then {

View file

@ -33,7 +33,7 @@ impl LintPass for NegMultiply {
#[allow(match_same_arms)] #[allow(match_same_arms)]
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NegMultiply { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NegMultiply {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if let ExprKind::Binary(Spanned { node: BiMul, .. }, ref l, ref r) = e.node { if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref l, ref r) = e.node {
match (&l.node, &r.node) { match (&l.node, &r.node) {
(&ExprKind::Unary(..), &ExprKind::Unary(..)) => (), (&ExprKind::Unary(..), &ExprKind::Unary(..)) => (),
(&ExprKind::Unary(UnNeg, ref lit), _) => check_mul(cx, e.span, lit, r), (&ExprKind::Unary(UnNeg, ref lit), _) => check_mul(cx, e.span, lit, r),

View file

@ -133,7 +133,7 @@ fn reduce_expression<'a>(cx: &LateContext, expr: &'a Expr) -> Option<Vec<&'a Exp
} }
match expr.node { match expr.node {
ExprKind::Index(ref a, ref b) => Some(vec![&**a, &**b]), ExprKind::Index(ref a, ref b) => Some(vec![&**a, &**b]),
ExprKind::Binary(ref binop, ref a, ref b) if binop.node != BiAnd && binop.node != BiOr => { ExprKind::Binary(ref binop, ref a, ref b) if binop.node != BinOpKind::And && binop.node != BinOpKind::Or => {
Some(vec![&**a, &**b]) Some(vec![&**a, &**b])
}, },
ExprKind::Array(ref v) | ExprKind::Tup(ref v) => Some(v.iter().collect()), ExprKind::Array(ref v) | ExprKind::Tup(ref v) => Some(v.iter().collect()),

View file

@ -42,14 +42,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
if cx.tables.expr_ty(ident1).is_integral(); if cx.tables.expr_ty(ident1).is_integral();
if cx.tables.expr_ty(ident2).is_integral(); if cx.tables.expr_ty(ident2).is_integral();
then { then {
if let BinOp_::BiLt = op.node { if let BinOpKind::Lt = op.node {
if let BinOp_::BiAdd = op2.node { if let BinOpKindAdd = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
"You are trying to use classic C overflow conditions that will fail in Rust."); "You are trying to use classic C overflow conditions that will fail in Rust.");
} }
} }
if let BinOp_::BiGt = op.node { if let BinOpKind::Gt = op.node {
if let BinOp_::BiSub = op2.node { if let BinOpKind::Sub = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
"You are trying to use classic C underflow conditions that will fail in Rust."); "You are trying to use classic C underflow conditions that will fail in Rust.");
} }
@ -67,14 +67,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OverflowCheckConditional {
if cx.tables.expr_ty(ident1).is_integral(); if cx.tables.expr_ty(ident1).is_integral();
if cx.tables.expr_ty(ident2).is_integral(); if cx.tables.expr_ty(ident2).is_integral();
then { then {
if let BinOp_::BiGt = op.node { if let BinOpKind::Gt = op.node {
if let BinOp_::BiAdd = op2.node { if let BinOpKindAdd = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
"You are trying to use classic C overflow conditions that will fail in Rust."); "You are trying to use classic C overflow conditions that will fail in Rust.");
} }
} }
if let BinOp_::BiLt = op.node { if let BinOpKind::Lt = op.node {
if let BinOp_::BiSub = op2.node { if let BinOpKind::Sub = op2.node {
span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span, span_lint(cx, OVERFLOW_CHECK_CONDITIONAL, expr.span,
"You are trying to use classic C underflow conditions that will fail in Rust."); "You are trying to use classic C underflow conditions that will fail in Rust.");
} }

View file

@ -132,7 +132,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PointerPass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if let ExprKind::Binary(ref op, ref l, ref r) = expr.node { if let ExprKind::Binary(ref op, ref l, ref r) = expr.node {
if (op.node == BiEq || op.node == BiNe) && (is_null_path(l) || is_null_path(r)) { if (op.node == BinOpKind::Eq || op.node == BinOpKind::Ne) && (is_null_path(l) || is_null_path(r)) {
span_lint( span_lint(
cx, cx,
CMP_NULL, CMP_NULL,

View file

@ -184,7 +184,7 @@ fn has_step_by(cx: &LateContext, expr: &Expr) -> bool {
fn y_plus_one(expr: &Expr) -> Option<&Expr> { fn y_plus_one(expr: &Expr) -> Option<&Expr> {
match expr.node { match expr.node {
ExprKind::Binary(Spanned { node: BiAdd, .. }, ref lhs, ref rhs) => if is_integer_literal(lhs, 1) { ExprKind::Binary(Spanned { node: BinOpKind::Add, .. }, ref lhs, ref rhs) => if is_integer_literal(lhs, 1) {
Some(rhs) Some(rhs)
} else if is_integer_literal(rhs, 1) { } else if is_integer_literal(rhs, 1) {
Some(lhs) Some(lhs)
@ -197,7 +197,7 @@ fn y_plus_one(expr: &Expr) -> Option<&Expr> {
fn y_minus_one(expr: &Expr) -> Option<&Expr> { fn y_minus_one(expr: &Expr) -> Option<&Expr> {
match expr.node { match expr.node {
ExprKind::Binary(Spanned { node: BiSub, .. }, ref lhs, ref rhs) if is_integer_literal(rhs, 1) => Some(lhs), ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, ref lhs, ref rhs) if is_integer_literal(rhs, 1) => Some(lhs),
_ => None, _ => None,
} }
} }

View file

@ -82,7 +82,7 @@ impl LintPass for StringAdd {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if let ExprKind::Binary(Spanned { node: BiAdd, .. }, ref left, _) = e.node { if let ExprKind::Binary(Spanned { node: BinOpKind::Add, .. }, ref left, _) = e.node {
if is_string(cx, left) { if is_string(cx, left) {
if !is_allowed(cx, STRING_ADD_ASSIGN, e.id) { if !is_allowed(cx, STRING_ADD_ASSIGN, e.id) {
let parent = get_parent_expr(cx, e); let parent = get_parent_expr(cx, e);
@ -122,7 +122,7 @@ fn is_string(cx: &LateContext, e: &Expr) -> bool {
fn is_add(cx: &LateContext, src: &Expr, target: &Expr) -> bool { fn is_add(cx: &LateContext, src: &Expr, target: &Expr) -> bool {
match src.node { match src.node {
ExprKind::Binary(Spanned { node: BiAdd, .. }, ref left, _) => SpanlessEq::new(cx).eq_expr(target, left), ExprKind::Binary(Spanned { node: BinOpKind::Add, .. }, ref left, _) => SpanlessEq::new(cx).eq_expr(target, left),
ExprKind::Block(ref block, _) => { ExprKind::Block(ref block, _) => {
block.stmts.is_empty() block.stmts.is_empty()
&& block && block

View file

@ -59,10 +59,10 @@ impl LintPass for SuspiciousImpl {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
use rustc::hir::BinOp_::*; use rustc::hir::BinOpKind::*;
if let hir::ExprKind::Binary(binop, _, _) = expr.node { if let hir::ExprKind::Binary(binop, _, _) = expr.node {
match binop.node { match binop.node {
BiEq | BiLt | BiLe | BiNe | BiGe | BiGt => return, BinOpKind::Eq | BinOpKind::Lt | BinOpKind::Le | BinOpKind::Ne | BinOpKind::Ge | BinOpKind::Gt => return,
_ => {}, _ => {},
} }
// Check if the binary expression is part of another bi/unary expression // Check if the binary expression is part of another bi/unary expression
@ -94,7 +94,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
expr, expr,
binop.node, binop.node,
&["Add", "Sub", "Mul", "Div"], &["Add", "Sub", "Mul", "Div"],
&[BiAdd, BiSub, BiMul, BiDiv], &[BinOpKind::Add, BinOpKind::Sub, BinOpKind::Mul, BinOpKind::Div],
) { ) {
span_lint( span_lint(
cx, cx,
@ -124,7 +124,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
"ShrAssign", "ShrAssign",
], ],
&[ &[
BiAdd, BiSub, BiMul, BiDiv, BiBitAnd, BiBitOr, BiBitXor, BiRem, BiShl, BiShr BinOpKind::Add, BinOpKind::Sub, BinOpKind::Mul, BinOpKind::Div, BinOpKind::BitAnd, BinOpKind::BitOr, BinOpKind::BitXor, BinOpKind::Rem, BinOpKind::Shl, BinOpKind::Shr
], ],
) { ) {
span_lint( span_lint(
@ -144,9 +144,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
fn check_binop<'a>( fn check_binop<'a>(
cx: &LateContext, cx: &LateContext,
expr: &hir::Expr, expr: &hir::Expr,
binop: hir::BinOp_, binop: hir::BinOpKind,
traits: &[&'a str], traits: &[&'a str],
expected_ops: &[hir::BinOp_], expected_ops: &[hir::BinOpKind],
) -> Option<&'a str> { ) -> Option<&'a str> {
let mut trait_ids = vec![]; let mut trait_ids = vec![];
let [krate, module] = crate::utils::paths::OPS_MODULE; let [krate, module] = crate::utils::paths::OPS_MODULE;

View file

@ -450,7 +450,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitCmp {
let op = cmp.node; let op = cmp.node;
if op.is_comparison() && is_unit(cx.tables.expr_ty(left)) { if op.is_comparison() && is_unit(cx.tables.expr_ty(left)) {
let result = match op { let result = match op {
BiEq | BiLe | BiGe => "true", BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
_ => "false", _ => "false",
}; };
span_lint( span_lint(
@ -1374,7 +1374,7 @@ fn is_cast_between_fixed_and_target<'a, 'tcx>(
fn detect_absurd_comparison<'a, 'tcx>( fn detect_absurd_comparison<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>, cx: &LateContext<'a, 'tcx>,
op: BinOp_, op: BinOpKind,
lhs: &'tcx Expr, lhs: &'tcx Expr,
rhs: &'tcx Expr, rhs: &'tcx Expr,
) -> Option<(ExtremeExpr<'tcx>, AbsurdComparisonResult)> { ) -> Option<(ExtremeExpr<'tcx>, AbsurdComparisonResult)> {

View file

@ -80,7 +80,7 @@ fn collect_unwrap_info<'a, 'tcx: 'a>(
) -> Vec<UnwrapInfo<'tcx>> { ) -> Vec<UnwrapInfo<'tcx>> {
if let ExprKind::Binary(op, left, right) = &expr.node { if let ExprKind::Binary(op, left, right) = &expr.node {
match (invert, op.node) { match (invert, op.node) {
(false, BinOp_::BiAnd) | (false, BinOp_::BiBitAnd) | (true, BinOp_::BiOr) | (true, BinOp_::BiBitOr) => { (false, BinOpKind::And) | (false, BinOpKind::BitAnd) | (true, BinOpKind::Or) | (true, BinOpKind::BitOr) => {
let mut unwrap_info = collect_unwrap_info(cx, left, invert); let mut unwrap_info = collect_unwrap_info(cx, left, invert);
unwrap_info.append(&mut collect_unwrap_info(cx, right, invert)); unwrap_info.append(&mut collect_unwrap_info(cx, right, invert));
return unwrap_info; return unwrap_info;

View file

@ -240,7 +240,7 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
let left_pat = self.next("left"); let left_pat = self.next("left");
let right_pat = self.next("right"); let right_pat = self.next("right");
println!("Binary(ref {}, ref {}, ref {}) = {};", op_pat, left_pat, right_pat, current); println!("Binary(ref {}, ref {}, ref {}) = {};", op_pat, left_pat, right_pat, current);
println!(" if BinOp_::{:?} == {}.node;", op.node, op_pat); println!(" if BinOpKind::{:?} == {}.node;", op.node, op_pat);
self.current = left_pat; self.current = left_pat;
self.visit_expr(left); self.visit_expr(left);
self.current = right_pat; self.current = right_pat;
@ -385,7 +385,7 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
let target_pat = self.next("target"); let target_pat = self.next("target");
let value_pat = self.next("value"); let value_pat = self.next("value");
println!("AssignOp(ref {}, ref {}, ref {}) = {};", op_pat, target_pat, value_pat, current); println!("AssignOp(ref {}, ref {}, ref {}) = {};", op_pat, target_pat, value_pat, current);
println!(" if BinOp_::{:?} == {}.node;", op.node, op_pat); println!(" if BinOpKind::{:?} == {}.node;", op.node, op_pat);
self.current = target_pat; self.current = target_pat;
self.visit_expr(target); self.visit_expr(target);
self.current = value_pat; self.current = value_pat;

View file

@ -2,7 +2,7 @@
#![deny(missing_docs_in_private_items)] #![deny(missing_docs_in_private_items)]
use rustc::hir::{BinOp_, Expr}; use rustc::hir::{BinOpKind, Expr};
#[derive(PartialEq, Eq, Debug, Copy, Clone)] #[derive(PartialEq, Eq, Debug, Copy, Clone)]
/// Represent a normalized comparison operator. /// Represent a normalized comparison operator.
@ -19,14 +19,14 @@ pub enum Rel {
/// Put the expression in the form `lhs < rhs`, `lhs <= rhs`, `lhs == rhs` or /// Put the expression in the form `lhs < rhs`, `lhs <= rhs`, `lhs == rhs` or
/// `lhs != rhs`. /// `lhs != rhs`.
pub fn normalize_comparison<'a>(op: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(Rel, &'a Expr, &'a Expr)> { pub fn normalize_comparison<'a>(op: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(Rel, &'a Expr, &'a Expr)> {
match op { match op {
BinOp_::BiLt => Some((Rel::Lt, lhs, rhs)), BinOpKind::Lt => Some((Rel::Lt, lhs, rhs)),
BinOp_::BiLe => Some((Rel::Le, lhs, rhs)), BinOpKind::Le => Some((Rel::Le, lhs, rhs)),
BinOp_::BiGt => Some((Rel::Lt, rhs, lhs)), BinOpKind::Gt => Some((Rel::Lt, rhs, lhs)),
BinOp_::BiGe => Some((Rel::Le, rhs, lhs)), BinOpKind::Ge => Some((Rel::Le, rhs, lhs)),
BinOp_::BiEq => Some((Rel::Eq, rhs, lhs)), BinOpKind::Eq => Some((Rel::Eq, rhs, lhs)),
BinOp_::BiNe => Some((Rel::Ne, rhs, lhs)), BinOpKind::Ne => Some((Rel::Ne, rhs, lhs)),
_ => None, _ => None,
} }
} }

View file

@ -9,7 +9,7 @@ use syntax::ast;
use crate::utils::{is_expn_of, match_def_path, match_qpath, opt_def_id, paths, resolve_node}; use crate::utils::{is_expn_of, match_def_path, match_qpath, opt_def_id, paths, resolve_node};
/// Convert a hir binary operator to the corresponding `ast` type. /// Convert a hir binary operator to the corresponding `ast` type.
pub fn binop(op: hir::BinOp_) -> ast::BinOpKind { pub fn binop(op: hir::BinOpKind) -> ast::BinOpKind {
match op { match op {
hir::BinOpKind::Eq => ast::BinOpKind::Eq, hir::BinOpKind::Eq => ast::BinOpKind::Eq,
hir::BinOpKind::Ge => ast::BinOpKind::Ge, hir::BinOpKind::Ge => ast::BinOpKind::Ge,

View file

@ -280,14 +280,26 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
} }
} }
fn swap_binop<'a>(binop: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOp_, &'a Expr, &'a Expr)> { fn swap_binop<'a>(binop: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOpKind, &'a Expr, &'a Expr)> {
match binop { match binop {
BiAdd | BiMul | BiBitXor | BiBitAnd | BiEq | BiNe | BiBitOr => Some((binop, rhs, lhs)), BinOpKind::Add |
BiLt => Some((BiGt, rhs, lhs)), BinOpKind::Mul |
BiLe => Some((BiGe, rhs, lhs)), BinOpKind::Eq |
BiGe => Some((BiLe, rhs, lhs)), BinOpKind::Ne |
BiGt => Some((BiLt, rhs, lhs)), BinOpKind::BitAnd |
BiShl | BiShr | BiRem | BiSub | BiDiv | BiAnd | BiOr => None, BinOpKind::BitXor |
BinOpKind::BitOr => Some((binop, rhs, lhs)),
BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
BinOpKind::Shl |
BinOpKind::Shr |
BinOpKind::Rem |
BinOpKind::Sub |
BinOpKind::Div |
BinOpKind::And |
BinOpKind::Or => None,
} }
} }

View file

@ -382,21 +382,21 @@ fn associativity(op: &AssocOp) -> Associativity {
/// Convert a `hir::BinOp` to the corresponding assigning binary operator. /// Convert a `hir::BinOp` to the corresponding assigning binary operator.
fn hirbinop2assignop(op: hir::BinOp) -> AssocOp { fn hirbinop2assignop(op: hir::BinOp) -> AssocOp {
use rustc::hir::BinOp_::*; use rustc::hir::BinOpKind::*;
use syntax::parse::token::BinOpToken::*; use syntax::parse::token::BinOpToken::*;
AssocOp::AssignOp(match op.node { AssocOp::AssignOp(match op.node {
BiAdd => Plus, BinOpKind::Add => Plus,
BiBitAnd => And, BinOpKind::BitAnd => And,
BiBitOr => Or, BinOpKind::BitOr => Or,
BiBitXor => Caret, BinOpKind::BitXor => Caret,
BiDiv => Slash, BinOpKind::Div => Slash,
BiMul => Star, BinOpKind::Mul => Star,
BiRem => Percent, BinOpKind::Rem => Percent,
BiShl => Shl, BinOpKind::Shl => Shl,
BiShr => Shr, BinOpKind::Shr => Shr,
BiSub => Minus, BinOpKind::Sub => Minus,
BiAnd | BiEq | BiGe | BiGt | BiLe | BiLt | BiNe | BiOr => panic!("This operator does not exist"), BinOpKind::And | BinOpKind::Eq | BinOpKind::Ge | BinOpKind::Gt | BinOpKind::Le | BinOpKind::Lt | BinOpKind::Ne | BinOpKind::Or => panic!("This operator does not exist"),
}) })
} }

View file

@ -33,7 +33,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
// check for instances of 0.0/0.0 // check for instances of 0.0/0.0
if_chain! { if_chain! {
if let ExprKind::Binary(ref op, ref left, ref right) = expr.node; if let ExprKind::Binary(ref op, ref left, ref right) = expr.node;
if let BinOp_::BiDiv = op.node; if let BinOpKind::Div = op.node;
// TODO - constant_simple does not fold many operations involving floats. // TODO - constant_simple does not fold many operations involving floats.
// That's probably fine for this lint - it's pretty unlikely that someone would // That's probably fine for this lint - it's pretty unlikely that someone would
// do something like 0.0/(2.0 - 2.0), but it would be nice to warn on that case too. // do something like 0.0/(2.0 - 2.0), but it would be nice to warn on that case too.

View file

@ -25,7 +25,7 @@ impl Mul for Foo {
type Output = Foo; type Output = Foo;
fn mul(self, other: Foo) -> Foo { fn mul(self, other: Foo) -> Foo {
Foo(self.0 * other.0 % 42) // OK: BiRem part of BiExpr as parent node Foo(self.0 * other.0 % 42) // OK: BinOpKind::Rem part of BiExpr as parent node
} }
} }
@ -33,7 +33,7 @@ impl Sub for Foo {
type Output = Foo; type Output = Foo;
fn sub(self, other: Self) -> Self { fn sub(self, other: Self) -> Self {
Foo(self.0 * other.0 - 42) // OK: BiMul part of BiExpr as child node Foo(self.0 * other.0 - 42) // OK: BinOpKind::Mul part of BiExpr as child node
} }
} }
@ -41,7 +41,7 @@ impl Div for Foo {
type Output = Foo; type Output = Foo;
fn div(self, other: Self) -> Self { fn div(self, other: Self) -> Self {
Foo(do_nothing(self.0 + other.0) / 42) // OK: BiAdd part of BiExpr as child node Foo(do_nothing(self.0 + other.0) / 42) // OK: BinOpKind::Add part of BiExpr as child node
} }
} }

View file

@ -1,8 +1,8 @@
if_chain! { if_chain! {
if let ExprKind::Binary(ref op, ref left, ref right) = expr.node; if let ExprKind::Binary(ref op, ref left, ref right) = expr.node;
if BinOp_::BiEq == op.node; if BinOpKind::Eq == op.node;
if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node; if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node;
if BinOp_::BiBitAnd == op1.node; if BinOpKind::BitAnd == op1.node;
if let ExprKind::Path(ref path) = left1.node; if let ExprKind::Path(ref path) = left1.node;
if match_qpath(path, &["x"]); if match_qpath(path, &["x"]);
if let ExprKind::Lit(ref lit) = right1.node; if let ExprKind::Lit(ref lit) = right1.node;