From f571cf0b5e56ff2f7651fdd809410d35edba09bd Mon Sep 17 00:00:00 2001 From: Michael Recachinas Date: Sun, 24 Sep 2017 12:31:12 +0100 Subject: [PATCH] Change rtype of int_plus_one detection to Option --- clippy_lints/src/int_plus_one.rs | 39 +++++++++----------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/clippy_lints/src/int_plus_one.rs b/clippy_lints/src/int_plus_one.rs index 54142b924ae..420427e7d0a 100644 --- a/clippy_lints/src/int_plus_one.rs +++ b/clippy_lints/src/int_plus_one.rs @@ -59,19 +59,15 @@ fn check_lit(&self, lit: &Lit, target_value: i128) -> bool { false } - fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<(bool, Option)> { + fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option { match (binop, &lhs.node, &rhs.node) { // case where `x - 1 >= ...` or `-1 + x >= ...` (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => { match (lhskind.node, &lhslhs.node, &lhsrhs.node) { // `-1 + x` - (BinOpKind::Add, &ExprKind::Lit(ref lit), _) => { - Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS))) - }, + (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS), // `x - 1` - (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => { - Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS))) - } + (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS), _ => None } }, @@ -79,12 +75,8 @@ fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Exp (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) if rhskind.node == BinOpKind::Add => { match (&rhslhs.node, &rhsrhs.node) { // `y + 1` and `1 + y` - (&ExprKind::Lit(ref lit), _) => { - Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS))) - }, - (_, &ExprKind::Lit(ref lit)) => { - Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS))) - }, + (&ExprKind::Lit(ref lit), _) if 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), _ => None } }, @@ -92,12 +84,8 @@ fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Exp (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) if lhskind.node == BinOpKind::Add => { match (&lhslhs.node, &lhsrhs.node) { // `1 + x` and `x + 1` - (&ExprKind::Lit(ref lit), _) => { - Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS))) - }, - (_, &ExprKind::Lit(ref lit)) => { - Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS))) - }, + (&ExprKind::Lit(ref lit), _) if 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), _ => None } }, @@ -105,13 +93,9 @@ fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Exp (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => { match (rhskind.node, &rhslhs.node, &rhsrhs.node) { // `-1 + y` - (BinOpKind::Add, &ExprKind::Lit(ref lit), _) => { - Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS))) - }, + (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS), // `y - 1` - (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => { - Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS))) - }, + (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS), _ => None } }, @@ -151,9 +135,8 @@ fn emit_warning(&self, cx: &EarlyContext, block: &Expr, recommendation: String) impl EarlyLintPass for IntPlusOne { fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) { if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node { - match self.check_binop(cx, kind.node, lhs, rhs) { - Some((should_emit, Some(ref rec))) if should_emit => self.emit_warning(cx, item, rec.clone()), - _ => () + if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) { + self.emit_warning(cx, item, rec.clone()); } } }