Skip to content

Commit f571cf0

Browse files
author
Michael Recachinas
committed
Change rtype of int_plus_one detection to Option<String>
1 parent 9437d29 commit f571cf0

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

clippy_lints/src/int_plus_one.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,59 +59,43 @@ impl IntPlusOne {
5959
false
6060
}
6161

62-
fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<(bool, Option<String>)> {
62+
fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
6363
match (binop, &lhs.node, &rhs.node) {
6464
// case where `x - 1 >= ...` or `-1 + x >= ...`
6565
(BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
6666
match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
6767
// `-1 + x`
68-
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) => {
69-
Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)))
70-
},
68+
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS),
7169
// `x - 1`
72-
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => {
73-
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)))
74-
}
70+
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS),
7571
_ => None
7672
}
7773
},
7874
// case where `... >= y + 1` or `... >= 1 + y`
7975
(BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) if rhskind.node == BinOpKind::Add => {
8076
match (&rhslhs.node, &rhsrhs.node) {
8177
// `y + 1` and `1 + y`
82-
(&ExprKind::Lit(ref lit), _) => {
83-
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)))
84-
},
85-
(_, &ExprKind::Lit(ref lit)) => {
86-
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)))
87-
},
78+
(&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS),
79+
(_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS),
8880
_ => None
8981
}
9082
},
9183
// case where `x + 1 <= ...` or `1 + x <= ...`
9284
(BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) if lhskind.node == BinOpKind::Add => {
9385
match (&lhslhs.node, &lhsrhs.node) {
9486
// `1 + x` and `x + 1`
95-
(&ExprKind::Lit(ref lit), _) => {
96-
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)))
97-
},
98-
(_, &ExprKind::Lit(ref lit)) => {
99-
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)))
100-
},
87+
(&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS),
88+
(_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS),
10189
_ => None
10290
}
10391
},
10492
// case where `... >= y - 1` or `... >= -1 + y`
10593
(BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
10694
match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
10795
// `-1 + y`
108-
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) => {
109-
Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)))
110-
},
96+
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS),
11197
// `y - 1`
112-
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => {
113-
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)))
114-
},
98+
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS),
11599
_ => None
116100
}
117101
},
@@ -151,9 +135,8 @@ impl IntPlusOne {
151135
impl EarlyLintPass for IntPlusOne {
152136
fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
153137
if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
154-
match self.check_binop(cx, kind.node, lhs, rhs) {
155-
Some((should_emit, Some(ref rec))) if should_emit => self.emit_warning(cx, item, rec.clone()),
156-
_ => ()
138+
if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
139+
self.emit_warning(cx, item, rec.clone());
157140
}
158141
}
159142
}

0 commit comments

Comments
 (0)