1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
3 use clippy_utils::diagnostics::span_lint_and_sugg;
4 use clippy_utils::source::snippet_opt;
5 use rustc_ast::ast::{BinOpKind, Expr, ExprKind, Lit, LitKind};
6 use rustc_errors::Applicability;
7 use rustc_lint::{EarlyContext, EarlyLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **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.
24 /// Could be written as:
33 "instead of using `x >= y + 1`, use `x > y`"
36 declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
47 #[derive(Copy, Clone)]
54 #[allow(clippy::cast_sign_loss)]
55 fn check_lit(lit: &Lit, target_value: i128) -> bool {
56 if let LitKind::Int(value, ..) = lit.kind {
57 return value == (target_value as u128);
62 fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
63 match (binop, &lhs.kind, &rhs.kind) {
64 // case where `x - 1 >= ...` or `-1 + x >= ...`
65 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
66 match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) {
68 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
69 Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
72 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
73 Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
78 // case where `... >= y + 1` or `... >= 1 + y`
79 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
80 if rhskind.node == BinOpKind::Add =>
82 match (&rhslhs.kind, &rhsrhs.kind) {
83 // `y + 1` and `1 + y`
84 (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
85 Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
87 (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
88 Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
93 // case where `x + 1 <= ...` or `1 + x <= ...`
94 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
95 if lhskind.node == BinOpKind::Add =>
97 match (&lhslhs.kind, &lhsrhs.kind) {
98 // `1 + x` and `x + 1`
99 (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
100 Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
102 (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
103 Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
108 // case where `... >= y - 1` or `... >= -1 + y`
109 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
110 match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) {
112 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
113 Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
116 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
117 Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
126 fn generate_recommendation(
127 cx: &EarlyContext<'_>,
132 ) -> Option<String> {
133 let binop_string = match binop {
134 BinOpKind::Ge => ">",
135 BinOpKind::Le => "<",
138 if let Some(snippet) = snippet_opt(cx, node.span) {
139 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
140 let rec = match side {
141 Side::Lhs => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
142 Side::Rhs => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
150 fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
155 "unnecessary `>= y + 1` or `x - 1 >=`",
158 Applicability::MachineApplicable, // snippet
163 impl EarlyLintPass for IntPlusOne {
164 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
165 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
166 if let Some(rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
167 Self::emit_warning(cx, item, rec);