]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/int_plus_one.rs
Auto merge of #4880 - daxpedda:string-add, r=phansch
[rust.git] / clippy_lints / src / int_plus_one.rs
1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
2
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;
7 use syntax::ast::*;
8
9 use crate::utils::{snippet_opt, span_lint_and_then};
10
11 declare_clippy_lint! {
12     /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
13     ///
14     ///
15     /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     /// ```rust
21     /// # let x = 1;
22     /// # let y = 1;
23     /// if x >= y + 1 {}
24     /// ```
25     ///
26     /// Could be written as:
27     ///
28     /// ```rust
29     /// # let x = 1;
30     /// # let y = 1;
31     /// if x > y {}
32     /// ```
33     pub INT_PLUS_ONE,
34     complexity,
35     "instead of using x >= y + 1, use x > y"
36 }
37
38 declare_lint_pass!(IntPlusOne => [INT_PLUS_ONE]);
39
40 // cases:
41 // BinOpKind::Ge
42 // x >= y + 1
43 // x - 1 >= y
44 //
45 // BinOpKind::Le
46 // x + 1 <= y
47 // x <= y - 1
48
49 #[derive(Copy, Clone)]
50 enum Side {
51     LHS,
52     RHS,
53 }
54
55 impl IntPlusOne {
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);
60         }
61         false
62     }
63
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) {
69                     // `-1 + x`
70                     (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
71                         Self::generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
72                     },
73                     // `x - 1`
74                     (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
75                         Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
76                     },
77                     _ => None,
78                 }
79             },
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 =>
83             {
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)
88                     },
89                     (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
90                         Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
91                     },
92                     _ => None,
93                 }
94             }
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 =>
98             {
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)
103                     },
104                     (_, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
105                         Self::generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
106                     },
107                     _ => None,
108                 }
109             }
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) {
113                     // `-1 + y`
114                     (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if Self::check_lit(lit, -1) => {
115                         Self::generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
116                     },
117                     // `y - 1`
118                     (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if Self::check_lit(lit, 1) => {
119                         Self::generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
120                     },
121                     _ => None,
122                 }
123             },
124             _ => None,
125         }
126     }
127
128     fn generate_recommendation(
129         cx: &EarlyContext<'_>,
130         binop: BinOpKind,
131         node: &Expr,
132         other_side: &Expr,
133         side: Side,
134     ) -> Option<String> {
135         let binop_string = match binop {
136             BinOpKind::Ge => ">",
137             BinOpKind::Le => "<",
138             _ => return None,
139         };
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)),
145                 };
146                 return rec;
147             }
148         }
149         None
150     }
151
152     fn emit_warning(cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
153         span_lint_and_then(
154             cx,
155             INT_PLUS_ONE,
156             block.span,
157             "Unnecessary `>= y + 1` or `x - 1 >=`",
158             |db| {
159                 db.span_suggestion(
160                     block.span,
161                     "change it to",
162                     recommendation,
163                     Applicability::MachineApplicable, // snippet
164                 );
165             },
166         );
167     }
168 }
169
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());
175             }
176         }
177     }
178 }