]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/int_plus_one.rs
Fix lines that exceed max width manually
[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 syntax::ast::*;
5
6 use utils::{snippet_opt, span_lint_and_then};
7
8 /// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
9 ///
10 ///
11 /// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// x >= y + 1
18 /// ```
19 ///
20 /// Could be written:
21 ///
22 /// ```rust
23 /// x > y
24 /// ```
25 declare_lint! {
26     pub INT_PLUS_ONE,
27     Allow,
28     "instead of using x >= y + 1, use x > y"
29 }
30
31 pub struct IntPlusOne;
32
33 impl LintPass for IntPlusOne {
34     fn get_lints(&self) -> LintArray {
35         lint_array!(INT_PLUS_ONE)
36     }
37 }
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 {
55     #[allow(cast_sign_loss)]
56     fn check_lit(&self, lit: &Lit, target_value: i128) -> bool {
57         if let LitKind::Int(value, ..) = lit.node {
58             return value == (target_value as u128);
59         }
60         false
61     }
62
63     fn check_binop(&self, cx: &EarlyContext, binop: BinOpKind, lhs: &Expr, rhs: &Expr) -> Option<String> {
64         match (binop, &lhs.node, &rhs.node) {
65             // case where `x - 1 >= ...` or `-1 + x >= ...`
66             (BinOpKind::Ge, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _) => {
67                 match (lhskind.node, &lhslhs.node, &lhsrhs.node) {
68                     // `-1 + x`
69                     (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
70                         self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
71                     },
72                     // `x - 1`
73                     (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
74                         self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
75                     },
76                     _ => None,
77                 }
78             },
79             // case where `... >= y + 1` or `... >= 1 + y`
80             (BinOpKind::Ge, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs))
81                 if rhskind.node == BinOpKind::Add =>
82             {
83                 match (&rhslhs.node, &rhsrhs.node) {
84                     // `y + 1` and `1 + y`
85                     (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
86                         self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
87                     },
88                     (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
89                         self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
90                     },
91                     _ => None,
92                 }
93             }
94             // case where `x + 1 <= ...` or `1 + x <= ...`
95             (BinOpKind::Le, &ExprKind::Binary(ref lhskind, ref lhslhs, ref lhsrhs), _)
96                 if lhskind.node == BinOpKind::Add =>
97             {
98                 match (&lhslhs.node, &lhsrhs.node) {
99                     // `1 + x` and `x + 1`
100                     (&ExprKind::Lit(ref lit), _) if self.check_lit(lit, 1) => {
101                         self.generate_recommendation(cx, binop, lhsrhs, rhs, Side::LHS)
102                     },
103                     (_, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
104                         self.generate_recommendation(cx, binop, lhslhs, rhs, Side::LHS)
105                     },
106                     _ => None,
107                 }
108             }
109             // case where `... >= y - 1` or `... >= -1 + y`
110             (BinOpKind::Le, _, &ExprKind::Binary(ref rhskind, ref rhslhs, ref rhsrhs)) => {
111                 match (rhskind.node, &rhslhs.node, &rhsrhs.node) {
112                     // `-1 + y`
113                     (BinOpKind::Add, &ExprKind::Lit(ref lit), _) if self.check_lit(lit, -1) => {
114                         self.generate_recommendation(cx, binop, rhsrhs, lhs, Side::RHS)
115                     },
116                     // `y - 1`
117                     (BinOpKind::Sub, _, &ExprKind::Lit(ref lit)) if self.check_lit(lit, 1) => {
118                         self.generate_recommendation(cx, binop, rhslhs, lhs, Side::RHS)
119                     },
120                     _ => None,
121                 }
122             },
123             _ => None,
124         }
125     }
126
127     fn generate_recommendation(
128         &self,
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(&self, cx: &EarlyContext, block: &Expr, recommendation: String) {
153         span_lint_and_then(cx, INT_PLUS_ONE, block.span, "Unnecessary `>= y + 1` or `x - 1 >=`", |db| {
154             db.span_suggestion(block.span, "change `>= y + 1` to `> y` as shown", recommendation);
155         });
156     }
157 }
158
159 impl EarlyLintPass for IntPlusOne {
160     fn check_expr(&mut self, cx: &EarlyContext, item: &Expr) {
161         if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = item.node {
162             if let Some(ref rec) = self.check_binop(cx, kind.node, lhs, rhs) {
163                 self.emit_warning(cx, item, rec.clone());
164             }
165         }
166     }
167 }