1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
3 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
4 use crate::rustc::{declare_tool_lint, lint_array};
5 use crate::rustc_errors::Applicability;
6 use crate::syntax::ast::*;
8 use crate::utils::{snippet_opt, span_lint_and_then};
10 /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
13 /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
15 /// **Known problems:** None.
27 declare_clippy_lint! {
30 "instead of using x >= y + 1, use x > y"
33 pub struct IntPlusOne;
35 impl LintPass for IntPlusOne {
36 fn get_lints(&self) -> LintArray {
37 lint_array!(INT_PLUS_ONE)
50 #[derive(Copy, Clone)]
57 #[allow(clippy::cast_sign_loss)]
58 fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
59 if let LitKind::Int(value, ..) = lit.node {
60 return value == (target_value as u128);
65 fn check_binop(&self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
66 match (binop, &lhs.node, &rhs.node) {
67 // case where `x - 1 >= ...` or `-1 + x >= ...`
68 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
69 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
71 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
72 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
75 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
76 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
81 // case where `... >= y + 1` or `... >= 1 + y`
82 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
83 if rhskind.node == BinOpKind::Add =>
85 match (&rhslhs.node, &rhsrhs.node) {
86 // `y + 1` and `1 + y`
87 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
88 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
90 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
91 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
96 // case where `x + 1 <= ...` or `1 + x <= ...`
97 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
98 if lhskind.node == BinOpKind::Add =>
100 match (&lhslhs.node, &lhsrhs.node) {
101 // `1 + x` and `x + 1`
102 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
103 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
105 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
106 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
111 // case where `... >= y - 1` or `... >= -1 + y`
112 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
113 match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
115 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
116 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
119 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
120 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
129 fn generate_recommendation(
131 cx: &EarlyContext<'_>,
136 ) -> Option<String> {
137 let binop_string = match binop {
138 BinOpKind::Ge => ">",
139 BinOpKind::Le => "<",
142 if let Some(snippet) = snippet_opt(cx, node.span) {
143 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
144 let rec = match side {
145 Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
146 Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
154 fn emit_warning(&self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
155 span_lint_and_then(cx, INT_PLUS_ONE, block.span, "Unnecessary `>= y + 1` or `x - 1 >=`", |db| {
156 db.span_suggestion_with_applicability(
158 "change `>= y + 1` to `> y` as shown",
160 Applicability::MachineApplicable, // snippet
166 impl EarlyLintPass for IntPlusOne {
167 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
168 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
169 if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
170 self.emit_warning(cx, item, rec.clone());