]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | //! lint on blocks unnecessarily using >= with a + 1 or - 1 |
2 | ||
cdc7bbd5 XL |
3 | use clippy_utils::diagnostics::span_lint_and_sugg; |
4 | use clippy_utils::source::snippet_opt; | |
487cf647 FG |
5 | use rustc_ast::ast::{BinOpKind, Expr, ExprKind, LitKind}; |
6 | use rustc_ast::token; | |
f20569fa XL |
7 | use rustc_errors::Applicability; |
8 | use rustc_lint::{EarlyContext, EarlyLintPass}; | |
4b012472 | 9 | use rustc_session::declare_lint_pass; |
f20569fa | 10 | |
f20569fa | 11 | declare_clippy_lint! { |
94222f64 XL |
12 | /// ### What it does |
13 | /// Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block | |
f20569fa | 14 | /// |
94222f64 XL |
15 | /// ### Why is this bad? |
16 | /// Readability -- better to use `> y` instead of `>= y + 1`. | |
f20569fa | 17 | /// |
94222f64 | 18 | /// ### Example |
ed00b5ec | 19 | /// ```no_run |
f20569fa XL |
20 | /// # let x = 1; |
21 | /// # let y = 1; | |
22 | /// if x >= y + 1 {} | |
23 | /// ``` | |
24 | /// | |
923072b8 | 25 | /// Use instead: |
ed00b5ec | 26 | /// ```no_run |
f20569fa XL |
27 | /// # let x = 1; |
28 | /// # let y = 1; | |
29 | /// if x > y {} | |
30 | /// ``` | |
a2a8927a | 31 | #[clippy::version = "pre 1.29.0"] |
f20569fa XL |
32 | pub INT_PLUS_ONE, |
33 | complexity, | |
34 | "instead of using `x >= y + 1`, use `x > y`" | |
35 | } | |
36 | ||
37 | declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]); | |
38 | ||
39 | // cases: | |
40 | // BinOpKind::Ge | |
41 | // x >= y + 1 | |
42 | // x - 1 >= y | |
43 | // | |
44 | // BinOpKind::Le | |
45 | // x + 1 <= y | |
46 | // x <= y - 1 | |
47 | ||
48 | #[derive(Copy, Clone)] | |
49 | enum Side { | |
50 | Lhs, | |
51 | Rhs, | |
52 | } | |
53 | ||
54 | impl IntPlusOne { | |
923072b8 | 55 | #[expect(clippy::cast_sign_loss)] |
487cf647 FG |
56 | fn check_lit(token_lit: token::Lit, target_value: i128) -> bool { |
57 | if let Ok(LitKind::Int(value, ..)) = LitKind::from_token_lit(token_lit) { | |
f20569fa XL |
58 | return value == (target_value as u128); |
59 | } | |
60 | false | |
61 | } | |
62 | ||
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 >= ...` | |
487cf647 | 66 | (BinOpKind::Ge, ExprKind::Binary(lhskind, lhslhs, lhsrhs), _) => { |
f20569fa XL |
67 | match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) { |
68 | // `-1 + x` | |
487cf647 | 69 | (BinOpKind::Add, ExprKind::Lit(lit), _) if Self::check_lit(*lit, -1) => { |
f20569fa XL |
70 | Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs) |
71 | }, | |
72 | // `x - 1` | |
487cf647 | 73 | (BinOpKind::Sub, _, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { |
f20569fa XL |
74 | Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs) |
75 | }, | |
76 | _ => None, | |
77 | } | |
78 | }, | |
79 | // case where `... >= y + 1` or `... >= 1 + y` | |
487cf647 | 80 | (BinOpKind::Ge, _, ExprKind::Binary(rhskind, rhslhs, rhsrhs)) if rhskind.node == BinOpKind::Add => { |
f20569fa XL |
81 | match (&rhslhs.kind, &rhsrhs.kind) { |
82 | // `y + 1` and `1 + y` | |
487cf647 | 83 | (ExprKind::Lit(lit), _) if Self::check_lit(*lit, 1) => { |
f20569fa XL |
84 | Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs) |
85 | }, | |
487cf647 | 86 | (_, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { |
f20569fa XL |
87 | Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs) |
88 | }, | |
89 | _ => None, | |
90 | } | |
c295e0f8 | 91 | }, |
f20569fa | 92 | // case where `x + 1 <= ...` or `1 + x <= ...` |
487cf647 | 93 | (BinOpKind::Le, ExprKind::Binary(lhskind, lhslhs, lhsrhs), _) if lhskind.node == BinOpKind::Add => { |
f20569fa XL |
94 | match (&lhslhs.kind, &lhsrhs.kind) { |
95 | // `1 + x` and `x + 1` | |
487cf647 | 96 | (ExprKind::Lit(lit), _) if Self::check_lit(*lit, 1) => { |
f20569fa XL |
97 | Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::Lhs) |
98 | }, | |
487cf647 | 99 | (_, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { |
f20569fa XL |
100 | Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::Lhs) |
101 | }, | |
102 | _ => None, | |
103 | } | |
c295e0f8 | 104 | }, |
f20569fa | 105 | // case where `... >= y - 1` or `... >= -1 + y` |
487cf647 | 106 | (BinOpKind::Le, _, ExprKind::Binary(rhskind, rhslhs, rhsrhs)) => { |
f20569fa XL |
107 | match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) { |
108 | // `-1 + y` | |
487cf647 | 109 | (BinOpKind::Add, ExprKind::Lit(lit), _) if Self::check_lit(*lit, -1) => { |
f20569fa XL |
110 | Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::Rhs) |
111 | }, | |
112 | // `y - 1` | |
487cf647 | 113 | (BinOpKind::Sub, _, ExprKind::Lit(lit)) if Self::check_lit(*lit, 1) => { |
f20569fa XL |
114 | Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::Rhs) |
115 | }, | |
116 | _ => None, | |
117 | } | |
118 | }, | |
119 | _ => None, | |
120 | } | |
121 | } | |
122 | ||
123 | fn generate_recommendation( | |
124 | cx: &EarlyContext<'_>, | |
125 | binop: BinOpKind, | |
126 | node: &Expr, | |
127 | other_side: &Expr, | |
128 | side: Side, | |
129 | ) -> Option<String> { | |
130 | let binop_string = match binop { | |
131 | BinOpKind::Ge => ">", | |
132 | BinOpKind::Le => "<", | |
133 | _ => return None, | |
134 | }; | |
135 | if let Some(snippet) = snippet_opt(cx, node.span) { | |
136 | if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) { | |
137 | let rec = match side { | |
2b03887a FG |
138 | Side::Lhs => Some(format!("{snippet} {binop_string} {other_side_snippet}")), |
139 | Side::Rhs => Some(format!("{other_side_snippet} {binop_string} {snippet}")), | |
f20569fa XL |
140 | }; |
141 | return rec; | |
142 | } | |
143 | } | |
144 | None | |
145 | } | |
146 | ||
147 | fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) { | |
148 | span_lint_and_sugg( | |
149 | cx, | |
150 | INT_PLUS_ONE, | |
151 | block.span, | |
152 | "unnecessary `>= y + 1` or `x - 1 >=`", | |
153 | "change it to", | |
154 | recommendation, | |
155 | Applicability::MachineApplicable, // snippet | |
156 | ); | |
157 | } | |
158 | } | |
159 | ||
160 | impl EarlyLintPass for IntPlusOne { | |
161 | fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) { | |
162 | if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind { | |
163 | if let Some(rec) = Self::check_binop(cx, kind.node, lhs, rhs) { | |
164 | Self::emit_warning(cx, item, rec); | |
165 | } | |
166 | } | |
167 | } | |
168 | } |