]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/double_comparison.rs
Merge pull request #2813 from terry90/master
[rust.git] / clippy_lints / src / double_comparison.rs
1 //! Lint on unnecessary double comparisons. Some examples:
2
3 use rustc::hir::*;
4 use rustc::lint::*;
5 use syntax::codemap::Span;
6
7 use crate::utils::{snippet, span_lint_and_sugg, SpanlessEq};
8
9 /// **What it does:** Checks for double comparions that could be simpified to a single expression.
10 ///
11 ///
12 /// **Why is this bad?** Readability.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// x == y || x < y
19 /// ```
20 ///
21 /// Could be written as:
22 ///
23 /// ```rust
24 /// x <= y
25 /// ```
26 declare_clippy_lint! {
27     pub DOUBLE_COMPARISONS,
28     complexity,
29     "unnecessary double comparisons that can be simplified"
30 }
31
32 pub struct DoubleComparisonPass;
33
34 impl LintPass for DoubleComparisonPass {
35     fn get_lints(&self) -> LintArray {
36         lint_array!(DOUBLE_COMPARISONS)
37     }
38 }
39
40 impl<'a, 'tcx> DoubleComparisonPass {
41     fn check_binop(
42         &self,
43         cx: &LateContext<'a, 'tcx>,
44         op: BinOp_,
45         lhs: &'tcx Expr,
46         rhs: &'tcx Expr,
47         span: Span,
48     ) {
49         let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (lhs.node.clone(), rhs.node.clone()) {
50             (ExprBinary(lb, llhs, lrhs), ExprBinary(rb, rlhs, rrhs)) => {
51                 (lb.node, llhs, lrhs, rb.node, rlhs, rrhs)
52             }
53             _ => return,
54         };
55         let mut spanless_eq = SpanlessEq::new(cx).ignore_fn();
56         if !(spanless_eq.eq_expr(&llhs, &rlhs) && spanless_eq.eq_expr(&lrhs, &rrhs)) {
57             return;
58         }
59         macro_rules! lint_double_comparison {
60             ($op:tt) => {{
61                 let lhs_str = snippet(cx, llhs.span, "");
62                 let rhs_str = snippet(cx, lrhs.span, "");
63                 let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
64                 span_lint_and_sugg(cx, DOUBLE_COMPARISONS, span,
65                                    "This binary expression can be simplified",
66                                    "try", sugg);
67             }}
68         }
69         match (op, lkind, rkind) {
70             (BiOr, BiEq, BiLt) | (BiOr, BiLt, BiEq) => lint_double_comparison!(<=),
71             (BiOr, BiEq, BiGt) | (BiOr, BiGt, BiEq) => lint_double_comparison!(>=),
72             (BiOr, BiLt, BiGt) | (BiOr, BiGt, BiLt) => lint_double_comparison!(!=),
73             (BiAnd, BiLe, BiGe) | (BiAnd, BiGe, BiLe) => lint_double_comparison!(==),
74             _ => (),
75         };
76     }
77 }
78
79 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DoubleComparisonPass {
80     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
81         if let ExprBinary(ref kind, ref lhs, ref rhs) = expr.node {
82             self.check_binop(cx, kind.node, lhs, rhs, expr.span);
83         }
84     }
85 }