]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/int_plus_one.rs
Auto merge of #3824 - phansch:adding_lints, 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::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
5 use rustc_errors::Applicability;
6 use syntax::ast::*;
7
8 use crate::utils::{snippet_opt, span_lint_and_then};
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
12     ///
13     ///
14     /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Example:**
19     /// ```rust
20     /// x >= y + 1
21     /// ```
22     ///
23     /// Could be written:
24     ///
25     /// ```rust
26     /// x > y
27     /// ```
28     pub INT_PLUS_ONE,
29     complexity,
30     "instead of using x >= y + 1, use x > y"
31 }
32
33 pub struct IntPlusOne;
34
35 impl LintPass for IntPlusOne {
36     fn get_lints(&self) -> LintArray {
37         lint_array!(INT_PLUS_ONE)
38     }
39
40     fn name(&self) -> &'static str {
41         "IntPlusOne"
42     }
43 }
44
45 // cases:
46 // BinOpKind::Ge
47 // x >= y + 1
48 // x - 1 >= y
49 //
50 // BinOpKind::Le
51 // x + 1 <= y
52 // x <= y - 1
53
54 #[derive(Copy, Clone)]
55 enum Side {
56     LHS,
57     RHS,
58 }
59
60 impl IntPlusOne {
61     #[allow(clippy::cast_sign_loss)]
62     fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
63         if let LitKind::Int(value, ..) = lit.node {
64             return value == (target_value as u128);
65         }
66         false
67     }
68
69     fn check_binop(&self, cx: &EarlyContext<'_>, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
70         match (binop, &lhs.node, &rhs.node) {
71             // case where `x - 1 >= ...` or `-1 + x >= ...`
72             (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
73                 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
74                     // `-1 + x`
75                     (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
76                         self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
77                     },
78                     // `x - 1`
79                     (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
80                         self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
81                     },
82                     _ => None,
83                 }
84             },
85             // case where `... >= y + 1` or `... >= 1 + y`
86             (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
87                 if rhskind.node == BinOpKind::Add =>
88             {
89                 match (&rhslhs.node, &rhsrhs.node) {
90                     // `y + 1` and `1 + y`
91                     (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
92                         self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
93                     },
94                     (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
95                         self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
96                     },
97                     _ => None,
98                 }
99             }
100             // case where `x + 1 <= ...` or `1 + x <= ...`
101             (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
102                 if lhskind.node == BinOpKind::Add =>
103             {
104                 match (&lhslhs.node, &lhsrhs.node) {
105                     // `1 + x` and `x + 1`
106                     (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
107                         self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
108                     },
109                     (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
110                         self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
111                     },
112                     _ => None,
113                 }
114             }
115             // case where `... >= y - 1` or `... >= -1 + y`
116             (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
117                 match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
118                     // `-1 + y`
119                     (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
120                         self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
121                     },
122                     // `y - 1`
123                     (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
124                         self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
125                     },
126                     _ => None,
127                 }
128             },
129             _ => None,
130         }
131     }
132
133     fn generate_recommendation(
134         &self,
135         cx: &EarlyContext<'_>,
136         binop: BinOpKind,
137         node: &Expr,
138         other_side: &Expr,
139         side: Side,
140     ) -> Option<String> {
141         let binop_string = match binop {
142             BinOpKind::Ge => ">",
143             BinOpKind::Le => "<",
144             _ => return None,
145         };
146         if let Some(snippet) = snippet_opt(cx, node.span) {
147             if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
148                 let rec = match side {
149                     Side::LHS => Some(format!("{} {} {}", snippet, binop_string, other_side_snippet)),
150                     Side::RHS => Some(format!("{} {} {}", other_side_snippet, binop_string, snippet)),
151                 };
152                 return rec;
153             }
154         }
155         None
156     }
157
158     fn emit_warning(&self, cx: &EarlyContext<'_>, block: &Expr, recommendation: String) {
159         span_lint_and_then(
160             cx,
161             INT_PLUS_ONE,
162             block.span,
163             "Unnecessary `>= y + 1` or `x - 1 >=`",
164             |db| {
165                 db.span_suggestion(
166                     block.span,
167                     "change `>= y + 1` to `> y` as shown",
168                     recommendation,
169                     Applicability::MachineApplicable, // snippet
170                 );
171             },
172         );
173     }
174 }
175
176 impl EarlyLintPass for IntPlusOne {
177     fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
178         if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
179             if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
180                 self.emit_warning(cx, item, rec.clone());
181             }
182         }
183     }
184 }