]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/int_plus_one.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / int_plus_one.rs
1 //! lint on blocks unnecessarily using >= with a + 1 or - 1
2
3 use rustc::lint::*;
4 use rustc::{declare_lint, lint_array};
5 use syntax::ast::*;
6
7 use crate::utils::{snippet_opt, span_lint_and_then};
8
9 /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
10 ///
11 ///
12 /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// x >= y + 1
19 /// ```
20 ///
21 /// Could be written:
22 ///
23 /// ```rust
24 /// x > y
25 /// ```
26 declare_clippy_lint! {
27     pub INT_PLUS_ONE,
28     complexity,
29     "instead of using x >= y + 1, use x > y"
30 }
31
32 pub struct IntPlusOne;
33
34 impl LintPass for IntPlusOne {
35     fn get_lints(&self) -> LintArray {
36         lint_array!(INT_PLUS_ONE)
37     }
38 }
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(cast_sign_loss)]
57     fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
58         if let LitKind::Int(value, ..) = lit.node {
59             return value == (target_value as u128);
60         }
61         false
62     }
63
64     fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
65         match (binop, &lhs.node, &rhs.node) {
66             // case where `x - 1 >= ...` or `-1 + x >= ...`
67             (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
68                 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
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.node, &rhsrhs.node) {
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.node, &lhsrhs.node) {
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.node, &rhsrhs.node) {
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         &self,
130         cx: &EarlyContext,
131         binop: BinOpKind,
132         node: &Expr,
133         other_side: &Expr,
134         side: Side,
135     ) -> Option<String> {
136         let binop_string = match binop {
137             BinOpKind::Ge => ">",
138             BinOpKind::Le => "<",
139             _ => return None,
140         };
141         if let Some(snippet) = snippet_opt(cx, node.span) {
142             if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
143                 let rec = match side {
144                     Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
145                     Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
146                 };
147                 return rec;
148             }
149         }
150         None
151     }
152
153     fn emit_warning(&self, cx: &EarlyContext, block: &Expr, recommendation: String) {
154         span_lint_and_then(cx, INT_PLUS_ONE, block.span, "Unnecessary `>= y + 1` or `x - 1 >=`", |db| {
155             db.span_suggestion(block.span, "change `>= y + 1` to `> y` as shown", recommendation);
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.node {
163             if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
164                 self.emit_warning(cx, item, rec.clone());
165             }
166         }
167     }
168 }