1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
4 use rustc::{declare_lint, lint_array};
7 use crate::utils::{snippet_opt, span_lint_and_then};
9 /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
12 /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
14 /// **Known problems:** None.
26 declare_clippy_lint! {
29 "instead of using x >= y + 1, use x > y"
32 pub struct IntPlusOne;
34 impl LintPass for IntPlusOne {
35 fn get_lints(&self) -> LintArray {
36 lint_array!(INT_PLUS_ONE)
49 #[derive(Copy, Clone)]
56 #[allow(cast_sign_loss)]
57 fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
58 if let LitKind::Int(value, ..) = lit.node {
59 return value == (target_value as u128);
64 fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
65 match (binop, &lhs.node, &rhs.node) {
66 // case where `x - 1 >= ...` or `-1 + x >= ...`
67 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
68 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
70 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
71 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
74 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
75 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
80 // case where `... >= y + 1` or `... >= 1 + y`
81 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
82 if rhskind.node == BinOpKind::Add =>
84 match (&rhslhs.node, &rhsrhs.node) {
85 // `y + 1` and `1 + y`
86 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
87 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
89 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
90 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
95 // case where `x + 1 <= ...` or `1 + x <= ...`
96 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
97 if lhskind.node == BinOpKind::Add =>
99 match (&lhslhs.node, &lhsrhs.node) {
100 // `1 + x` and `x + 1`
101 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
102 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
104 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
105 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
110 // case where `... >= y - 1` or `... >= -1 + y`
111 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
112 match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
114 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
115 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
118 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
119 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
128 fn generate_recommendation(
135 ) -> Option<String> {
136 let binop_string = match binop {
137 BinOpKind::Ge => ">",
138 BinOpKind::Le => "<",
141 if let Some(snippet) = snippet_opt(cx, node.span) {
142 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
143 let rec = match side {
144 Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
145 Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
153 fn emit_warning(&self, cx: &EarlyContext, block: &Expr, recommendation: String) {
154 span_lint_and_then(cx, INT_PLUS_ONE, block.span, "Unnecessary `>= y + 1` or `x - 1 >=`", |db| {
155 db.span_suggestion(block.span, "change `>= y + 1` to `> y` as shown", recommendation);
160 impl EarlyLintPass for IntPlusOne {
161 fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
162 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
163 if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
164 self.emit_warning(cx, item, rec.clone());