1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
3 use rustc::declare_lint_pass;
4 use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
5 use rustc_errors::Applicability;
6 use rustc_session::declare_tool_lint;
9 use crate::utils::{snippet_opt, span_lint_and_then};
11 declare_clippy_lint! {
12 /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
15 /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
17 /// **Known problems:** None.
26 /// Could be written as:
35 "instead of using x >= y + 1, use x > y"
38 declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
49 #[derive(Copy, Clone)]
56 #[allow(clippy::cast_sign_loss)]
57 fn check_lit(lit: &Lit, target_value: i128) -> bool {
58 if let LitKind::Int(value, ..) = lit.kind {
59 return value == (target_value as u128);
64 fn check_binop(cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
65 match (binop, &lhs.kind, &rhs.kind) {
66 // case where `x - 1 >= ...` or `-1 + x >= ...`
67 (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
68 match (lhskind.node, &lhslhs.kind, &lhsrhs.kind) {
70 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
71 Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
74 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
75 Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
80 // case where `... >= y + 1` or `... >= 1 + y`
81 (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
82 if rhskind.node == BinOpKind::Add =>
84 match (&rhslhs.kind, &rhsrhs.kind) {
85 // `y + 1` and `1 + y`
86 (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
87 Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
89 (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
90 Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
95 // case where `x + 1 <= ...` or `1 + x <= ...`
96 (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
97 if lhskind.node == BinOpKind::Add =>
99 match (&lhslhs.kind, &lhsrhs.kind) {
100 // `1 + x` and `x + 1`
101 (&ExprKind::Lit(ref lit), _) if Self::check_lit(lit, 1) => {
102 Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
104 (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
105 Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
110 // case where `... >= y - 1` or `... >= -1 + y`
111 (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
112 match (rhskind.node, &rhslhs.kind, &rhsrhs.kind) {
114 (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
115 Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
118 (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
119 Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
128 fn generate_recommendation(
129 cx: &EarlyContext<'_>,
134 ) -> Option<String> {
135 let binop_string = match binop {
136 BinOpKind::Ge => ">",
137 BinOpKind::Le => "<",
140 if let Some(snippet) = snippet_opt(cx, node.span) {
141 if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
142 let rec = match side {
143 Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
144 Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
152 fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
157 "Unnecessary `>= y + 1` or `x - 1 >=`",
163 Applicability::MachineApplicable, // snippet
170 impl EarlyLintPass for IntPlusOne {
171 fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
172 if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.kind {
173 if let Some(ref rec) = Self::check_binop(cx, kind.node, lhs, rhs) {
174 Self::emit_warning(cx, item, rec.clone());