Skip to content

Commit 9437d29

Browse files
author
Michael Recachinas
committed
Change to returning Option<(bool, Option<String>)>
1 parent fff3573 commit 9437d29

File tree

1 file changed

+26
-49
lines changed

1 file changed

+26
-49
lines changed

clippy_lints/src/int_plus_one.rs

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -59,87 +59,63 @@ impl IntPlusOne {
5959
false
6060
}
6161

62-
fn check_binop(&self, cx: &EarlyContext, block: &Expr, binop: BinOpKind, lhs: &Expr, rhs: &Expr) {
62+
fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<(bool, 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`
6868
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) => {
69-
let recommendation = self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS);
70-
if self.check_lit(lit, -1) {
71-
self.emit_warning(cx, block, recommendation)
72-
}
69+
Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)))
7370
},
7471
// `x - 1`
7572
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => {
76-
let recommendation = self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS);
77-
if self.check_lit(lit, 1) {
78-
self.emit_warning(cx, block, recommendation)
79-
}
73+
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)))
8074
}
81-
_ => ()
75+
_ => None
8276
}
8377
},
8478
// case where `... >= y + 1` or `... >= 1 + y`
8579
(BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) if rhskind.node == BinOpKind::Add => {
8680
match (&rhslhs.node, &rhsrhs.node) {
8781
// `y + 1` and `1 + y`
8882
(&ExprKind::Lit(ref lit), _) => {
89-
let recommendation = self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS);
90-
if self.check_lit(lit, 1) {
91-
self.emit_warning(cx, block, recommendation)
92-
}
83+
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)))
9384
},
9485
(_, &ExprKind::Lit(ref lit)) => {
95-
let recommendation = self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS);
96-
if self.check_lit(lit, 1) {
97-
self.emit_warning(cx, block, recommendation)
98-
}
86+
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)))
9987
},
100-
_ => ()
88+
_ => None
10189
}
10290
},
10391
// case where `x + 1 <= ...` or `1 + x <= ...`
10492
(BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) if lhskind.node == BinOpKind::Add => {
10593
match (&lhslhs.node, &lhsrhs.node) {
10694
// `1 + x` and `x + 1`
10795
(&ExprKind::Lit(ref lit), _) => {
108-
let recommendation = self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS);
109-
if self.check_lit(lit, 1) {
110-
self.emit_warning(cx, block, recommendation)
111-
}
96+
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)))
11297
},
11398
(_, &ExprKind::Lit(ref lit)) => {
114-
let recommendation = self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS);
115-
if self.check_lit(lit, 1) {
116-
self.emit_warning(cx, block, recommendation)
117-
}
99+
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)))
118100
},
119-
_ => ()
101+
_ => None
120102
}
121103
},
122104
// case where `... >= y - 1` or `... >= -1 + y`
123105
(BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
124106
match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
125107
// `-1 + y`
126108
(BinOpKind::Add, &ExprKind::Lit(ref lit), _) => {
127-
let recommendation = self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS);
128-
if self.check_lit(lit, -1) {
129-
self.emit_warning(cx, block, recommendation)
130-
}
109+
Some((self.check_lit(lit, -1), self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)))
131110
},
132111
// `y - 1`
133112
(BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) => {
134-
let recommendation = self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS);
135-
if self.check_lit(lit, 1) {
136-
self.emit_warning(cx, block, recommendation)
137-
}
113+
Some((self.check_lit(lit, 1), self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)))
138114
},
139-
_ => ()
115+
_ => None
140116
}
141117
},
142-
_ => ()
118+
_ => None
143119
}
144120
}
145121

@@ -161,23 +137,24 @@ impl IntPlusOne {
161137
None
162138
}
163139

164-
fn emit_warning(&self, cx: &EarlyContext, block: &Expr, recommendation: Option<String>) {
165-
if let Some(rec) = recommendation {
166-
span_lint_and_then(cx,
167-
INT_PLUS_ONE,
168-
block.span,
169-
"Unnecessary `>= y + 1` or `x - 1 >=`",
170-
|db| {
171-
db.span_suggestion(block.span, "change `>= y + 1` to `> y` as shown", rec);
172-
});
173-
}
140+
fn emit_warning(&self, cx: &EarlyContext, block: &Expr, recommendation: String) {
141+
span_lint_and_then(cx,
142+
INT_PLUS_ONE,
143+
block.span,
144+
"Unnecessary `>= y + 1` or `x - 1 >=`",
145+
|db| {
146+
db.span_suggestion(block.span, "change `>= y + 1` to `> y` as shown", recommendation);
147+
});
174148
}
175149
}
176150

177151
impl EarlyLintPass for IntPlusOne {
178152
fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
179153
if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
180-
self.check_binop(cx, item, kind.node, lhs, rhs);
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+
_ => ()
157+
}
181158
}
182159
}
183160
}

0 commit comments

Comments
 (0)