]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/double_comparison.rs
Merge pull request #3465 from flip1995/rustfmt
[rust.git] / clippy_lints / src / double_comparison.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 //! Lint on unnecessary double comparisons. Some examples:
11
12 use crate::rustc::hir::*;
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::rustc_errors::Applicability;
16 use crate::syntax::source_map::Span;
17
18 use crate::utils::{snippet_with_applicability, span_lint_and_sugg, SpanlessEq};
19
20 /// **What it does:** Checks for double comparions that could be simpified to a single expression.
21 ///
22 ///
23 /// **Why is this bad?** Readability.
24 ///
25 /// **Known problems:** None.
26 ///
27 /// **Example:**
28 /// ```rust
29 /// x == y || x < y
30 /// ```
31 ///
32 /// Could be written as:
33 ///
34 /// ```rust
35 /// x <= y
36 /// ```
37 declare_clippy_lint! {
38     pub DOUBLE_COMPARISONS,
39     complexity,
40     "unnecessary double comparisons that can be simplified"
41 }
42
43 pub struct Pass;
44
45 impl LintPass for Pass {
46     fn get_lints(&self) -> LintArray {
47         lint_array!(DOUBLE_COMPARISONS)
48     }
49 }
50
51 impl<'a, 'tcx> Pass {
52     #[allow(clippy::similar_names)]
53     fn check_binop(&self, cx: &LateContext<'a, 'tcx>, op: BinOpKind, lhs: &'tcx Expr, rhs: &'tcx Expr, span: Span) {
54         let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (lhs.node.clone(), rhs.node.clone()) {
55             (ExprKind::Binary(lb, llhs, lrhs), ExprKind::Binary(rb, rlhs, rrhs)) => {
56                 (lb.node, llhs, lrhs, rb.node, rlhs, rrhs)
57             },
58             _ => return,
59         };
60         let mut spanless_eq = SpanlessEq::new(cx).ignore_fn();
61         if !(spanless_eq.eq_expr(&llhs, &rlhs) && spanless_eq.eq_expr(&lrhs, &rrhs)) {
62             return;
63         }
64         macro_rules! lint_double_comparison {
65             ($op:tt) => {{
66                 let mut applicability = Applicability::MachineApplicable;
67                 let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
68                 let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
69                 let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
70                 span_lint_and_sugg(
71                     cx,
72                     DOUBLE_COMPARISONS,
73                     span,
74                     "This binary expression can be simplified",
75                     "try",
76                     sugg,
77                     applicability,
78                 );
79             }};
80         }
81         match (op, lkind, rkind) {
82             (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Lt) | (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Eq) => {
83                 lint_double_comparison!(<=)
84             },
85             (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Eq) => {
86                 lint_double_comparison!(>=)
87             },
88             (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Lt) => {
89                 lint_double_comparison!(!=)
90             },
91             (BinOpKind::And, BinOpKind::Le, BinOpKind::Ge) | (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => {
92                 lint_double_comparison!(==)
93             },
94             _ => (),
95         };
96     }
97 }
98
99 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
100     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
101         if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = expr.node {
102             self.check_binop(cx, kind.node, lhs, rhs, expr.span);
103         }
104     }
105 }