1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
6 use utils::{span_lint_and_then, snippet_opt};
8 /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
11 /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
13 /// **Known problems:** None.
28 "instead of using x >= y + 1, use x > y"
31 pub struct IntPlusOne;
33 impl LintPass for IntPlusOne {
34 fn get_lints(&self) -> LintArray {
35 lint_array!(INT_PLUS_ONE)
48 #[derive(Copy, Clone)]
55 #[allow(cast_sign_loss)]
56 fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
57 if let LitKind::Int(value, ..) = lit.node {
58 return value == (target_value as u128)
63 fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
64 match (binop, &lhs.node, &rhs.node) {
65 // case where `x - 1 >= ...` or `-1 + x >= ...`
66 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
67 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
69 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS),
71 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS),
75 // case where `... >= y + 1` or `... >= 1 + y`
76 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) if rhskind.node == BinOpKind::Add => {
77 match (&rhslhs.node, &rhsrhs.node) {
78 // `y + 1` and `1 + y`
79 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS),
80 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS),
84 // case where `x + 1 <= ...` or `1 + x <= ...`
85 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) if lhskind.node == BinOpKind::Add => {
86 match (&lhslhs.node, &lhsrhs.node) {
87 // `1 + x` and `x + 1`
88 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS),
89 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS),
93 // case where `... >= y - 1` or `... >= -1 + y`
94 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
95 match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
97 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS),
99 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS),
107 fn generate_recommendation(&self, cx: &EarlyContext, binop: BinOpKind, node: &Expr, other_side: &Expr, side: Side) -> Option<String> {
108 let binop_string = match binop {
109 BinOpKind::Ge => ">",
110 BinOpKind::Le => "<",
113 if let Some(snippet) = snippet_opt(cx, node.span) {
114 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
115 let rec = match side {
116 Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
117 Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
125 fn emit_warning(&self, cx: &EarlyContext, block: &Expr, recommendation: String) {
126 span_lint_and_then(cx,
129 "Unnecessary `>= y + 1` or `x - 1 >=`",
131 db.span_suggestion(block.span, "change `>= y + 1` to `> y` as shown", recommendation);
136 impl EarlyLintPass for IntPlusOne {
137 fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
138 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
139 if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
140 self.emit_warning(cx, item, rec.clone());