1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! lint on blocks unnecessarily using >= with a + 1 or - 1
12 use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::rustc_errors::Applicability;
15 use crate::syntax::ast::*;
17 use crate::utils::{snippet_opt, span_lint_and_then};
19 /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
22 /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
24 /// **Known problems:** None.
36 declare_clippy_lint! {
39 "instead of using x >= y + 1, use x > y"
42 pub struct IntPlusOne;
44 impl LintPass for IntPlusOne {
45 fn get_lints(&self) -> LintArray {
46 lint_array!(INT_PLUS_ONE)
59 #[derive(Copy, Clone)]
66 #[allow(clippy::cast_sign_loss)]
67 fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
68 if let LitKind::Int(value, ..) = lit.node {
69 return value == (target_value as u128);
74 fn check_binop(&self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
75 match (binop, &lhs.node, &rhs.node) {
76 // case where `x - 1 >= ...` or `-1 + x >= ...`
77 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
78 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
80 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
81 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
84 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
85 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
90 // case where `... >= y + 1` or `... >= 1 + y`
91 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
92 if rhskind.node == BinOpKind::Add =>
94 match (&rhslhs.node, &rhsrhs.node) {
95 // `y + 1` and `1 + y`
96 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
97 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
99 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
100 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
105 // case where `x + 1 <= ...` or `1 + x <= ...`
106 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
107 if lhskind.node == BinOpKind::Add =>
109 match (&lhslhs.node, &lhsrhs.node) {
110 // `1 + x` and `x + 1`
111 (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
112 self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
114 (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
115 self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
120 // case where `... >= y - 1` or `... >= -1 + y`
121 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
122 match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
124 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
125 self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
128 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
129 self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
138 fn generate_recommendation(
140 cx: &EarlyContext<'_>,
145 ) -> Option<String> {
146 let binop_string = match binop {
147 BinOpKind::Ge => ">",
148 BinOpKind::Le => "<",
151 if let Some(snippet) = snippet_opt(cx, node.span) {
152 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
153 let rec = match side {
154 Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
155 Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
163 fn emit_warning(&self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
168 "Unnecessary `>= y + 1` or `x - 1 >=`",
170 db.span_suggestion_with_applicability(
172 "change `>= y + 1` to `> y` as shown",
174 Applicability::MachineApplicable, // snippet
181 impl EarlyLintPass for IntPlusOne {
182 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
183 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
184 if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
185 self.emit_warning(cx, item, rec.clone());