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! {
12 /// Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
14 /// ### Why is this bad?
15 /// Readability -- better to use `> y` instead of `>= y + 1`.
24 /// Could be written as:
31 #[clippy::version = "pre 1.29.0"]
34 "instead of using `x >= y + 1`, use `x > y`"
37 declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
48 #[derive(Copy, Clone)]
55 #[expect(clippy::cast_sign_loss)]
56 fn check_lit(lit: &Lit, target_value: i128) -> bool {
57 if let LitKind::Int(value, ..) = lit.kind {
58 return value == (target_value as u128);
63 fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
64 match (binop, &lhs.kind, &rhs.kind) {
65 // case where `x - 1 >= ...` or `-1 + x >= ...`
66 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
67 match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) {
69 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
70 Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
73 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
74 Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
79 // case where `... >= y + 1` or `... >= 1 + y`
80 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
81 if rhskind.node == BinOpKind::Add =>
83 match (&rhslhs.kind, &rhsrhs.kind) {
84 // `y + 1` and `1 + y`
85 (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
86 Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
88 (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
89 Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
94 // case where `x + 1 <= ...` or `1 + x <= ...`
95 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
96 if lhskind.node == BinOpKind::Add =>
98 match (&lhslhs.kind, &lhsrhs.kind) {
99 // `1 + x` and `x + 1`
100 (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
101 Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs)
103 (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
104 Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs)
109 // case where `... >= y - 1` or `... >= -1 + y`
110 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
111 match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) {
113 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
114 Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs)
117 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
118 Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs)
127 fn generate_recommendation(
128 cx: &EarlyContext<'_>,
133 ) -> Option<String> {
134 let binop_string = match binop {
135 BinOpKind::Ge => ">",
136 BinOpKind::Le => "<",
139 if let Some(snippet) = snippet_opt(cx, node.span) {
140 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
141 let rec = match side {
142 Side::Lhs => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
143 Side::Rhs => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
151 fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
156 "unnecessary `>= y + 1` or `x - 1 >=`",
159 Applicability::MachineApplicable, // snippet
164 impl EarlyLintPass for IntPlusOne {
165 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
166 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
167 if let Some(rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
168 Self::emit_warning(cx, item, rec);