Change rtype of int_plus_one detection to Option<String>

This commit is contained in:
Michael Recachinas 2017-09-24 12:31:12 +01:00
parent 9437d2909c
commit f571cf0b5e

View File

@ -59,19 +59,15 @@ impl IntPlusOne {
false false
} }
fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<(bool, Option<String>)> { fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
match (binop, &lhs.node, &rhs.node) { match (binop, &lhs.node, &rhs.node) {
// case where `x - 1 >= ...` or `-1 + x >= ...` // case where `x - 1 >= ...` or `-1 + x >= ...`
(BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => { (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
match (lhskind.node, &lhslhs.node, &lhsrhs.node) { match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
// `-1 + x` // `-1 + x`
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) => { (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS),
Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)))
},
// `x - 1` // `x - 1`
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => { (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS),
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)))
}
_ => None _ => None
} }
}, },
@ -79,12 +75,8 @@ impl IntPlusOne {
(BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) if rhskind.node == BinOpKind::Add => { (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) if rhskind.node == BinOpKind::Add => {
match (&rhslhs.node, &rhsrhs.node) { match (&rhslhs.node, &rhsrhs.node) {
// `y + 1` and `1 + y` // `y + 1` and `1 + y`
(&ExprKind::Lit(ref lit), _) => { (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS),
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS))) (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS),
},
(_, &ExprKind::Lit(ref lit)) => {
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)))
},
_ => None _ => None
} }
}, },
@ -92,12 +84,8 @@ impl IntPlusOne {
(BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) if lhskind.node == BinOpKind::Add => { (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) if lhskind.node == BinOpKind::Add => {
match (&lhslhs.node, &lhsrhs.node) { match (&lhslhs.node, &lhsrhs.node) {
// `1 + x` and `x + 1` // `1 + x` and `x + 1`
(&ExprKind::Lit(ref lit), _) => { (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS),
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS))) (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS),
},
(_, &ExprKind::Lit(ref lit)) => {
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)))
},
_ => None _ => None
} }
}, },
@ -105,13 +93,9 @@ impl IntPlusOne {
(BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => { (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
match (rhskind.node, &rhslhs.node, &rhsrhs.node) { match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
// `-1 + y` // `-1 + y`
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) => { (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS),
Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)))
},
// `y - 1` // `y - 1`
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => { (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS),
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)))
},
_ => None _ => None
} }
}, },
@ -151,9 +135,8 @@ impl IntPlusOne {
impl EarlyLintPass for IntPlusOne { impl EarlyLintPass for IntPlusOne {
fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) { fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node { if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
match self.check_binop(cx, kind.node, lhs, rhs) { if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
Some((should_emit, Some(ref rec))) if should_emit => self.emit_warning(cx, item, rec.clone()), self.emit_warning(cx, item, rec.clone());
_ => ()
} }
} }
} }