1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
3 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
5 use rustc_errors::Applicability;
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)
40 fn name(&self) -> &'static str {
54 #[derive(Copy, Clone)]
61 #[allow(clippy::cast_sign_loss)]
62 fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
63 if let LitKind::Int(value, ..) = lit.node {
64 return value == (target_value as u128);
69 fn check_binop(&self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
70 match (binop, &lhs.node, &rhs.node) {
71 // case where `x - 1 >= ...` or `-1 + x >= ...`
72 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
73 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
75 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
76 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
79 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
80 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
85 // case where `... >= y + 1` or `... >= 1 + y`
86 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
87 if rhskind.node == BinOpKind::Add =>
89 match (&rhslhs.node, &rhsrhs.node) {
90 // `y + 1` and `1 + y`
91 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
92 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
94 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
95 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
100 // case where `x + 1 <= ...` or `1 + x <= ...`
101 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
102 if lhskind.node == BinOpKind::Add =>
104 match (&lhslhs.node, &lhsrhs.node) {
105 // `1 + x` and `x + 1`
106 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
107 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
109 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
110 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
115 // case where `... >= y - 1` or `... >= -1 + y`
116 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
117 match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
119 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
120 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
123 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
124 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
133 fn generate_recommendation(
135 cx: &EarlyContext<'_>,
140 ) -> Option<String> {
141 let binop_string = match binop {
142 BinOpKind::Ge => ">",
143 BinOpKind::Le => "<",
146 if let Some(snippet) = snippet_opt(cx, node.span) {
147 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
148 let rec = match side {
149 Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
150 Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
158 fn emit_warning(&self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
163 "Unnecessary `>= y + 1` or `x - 1 >=`",
167 "change `>= y + 1` to `> y` as shown",
169 Applicability::MachineApplicable, // snippet
176 impl EarlyLintPass for IntPlusOne {
177 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
178 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
179 if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
180 self.emit_warning(cx, item, rec.clone());