]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/double_comparison.rs
rustup https://github.com/rust-lang/rust/pull/67455
[rust.git] / clippy_lints / src / double_comparison.rs
1 //! Lint on unnecessary double comparisons. Some examples:
2
3 use rustc::declare_lint_pass;
4 use rustc::hir::*;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_errors::Applicability;
7 use rustc_session::declare_tool_lint;
8 use syntax::source_map::Span;
9
10 use crate::utils::{snippet_with_applicability, span_lint_and_sugg, SpanlessEq};
11
12 declare_clippy_lint! {
13     /// **What it does:** Checks for double comparisons that could be simplified to a single expression.
14     ///
15     ///
16     /// **Why is this bad?** Readability.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     /// ```rust
22     /// # let x = 1;
23     /// # let y = 2;
24     /// if x == y || x < y {}
25     /// ```
26     ///
27     /// Could be written as:
28     ///
29     /// ```rust
30     /// # let x = 1;
31     /// # let y = 2;
32     /// if x <= y {}
33     /// ```
34     pub DOUBLE_COMPARISONS,
35     complexity,
36     "unnecessary double comparisons that can be simplified"
37 }
38
39 declare_lint_pass!(DoubleComparisons => [DOUBLE_COMPARISONS]);
40
41 impl<'a, 'tcx> DoubleComparisons {
42     #[allow(clippy::similar_names)]
43     fn check_binop(cx: &LateContext<'a, 'tcx>, op: BinOpKind, lhs: &'tcx Expr, rhs: &'tcx Expr, span: Span) {
44         let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (&lhs.kind, &rhs.kind) {
45             (ExprKind::Binary(lb, llhs, lrhs), ExprKind::Binary(rb, rlhs, rrhs)) => {
46                 (lb.node, llhs, lrhs, rb.node, rlhs, rrhs)
47             },
48             _ => return,
49         };
50         let mut spanless_eq = SpanlessEq::new(cx).ignore_fn();
51         if !(spanless_eq.eq_expr(&llhs, &rlhs) && spanless_eq.eq_expr(&lrhs, &rrhs)) {
52             return;
53         }
54         macro_rules! lint_double_comparison {
55             ($op:tt) => {{
56                 let mut applicability = Applicability::MachineApplicable;
57                 let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
58                 let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
59                 let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
60                 span_lint_and_sugg(
61                     cx,
62                     DOUBLE_COMPARISONS,
63                     span,
64                     "This binary expression can be simplified",
65                     "try",
66                     sugg,
67                     applicability,
68                 );
69             }};
70         }
71         #[rustfmt::skip]
72         match (op, lkind, rkind) {
73             (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Lt) | (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Eq) => {
74                 lint_double_comparison!(<=)
75             },
76             (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Eq) => {
77                 lint_double_comparison!(>=)
78             },
79             (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Lt) => {
80                 lint_double_comparison!(!=)
81             },
82             (BinOpKind::And, BinOpKind::Le, BinOpKind::Ge) | (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => {
83                 lint_double_comparison!(==)
84             },
85             _ => (),
86         };
87     }
88 }
89
90 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DoubleComparisons {
91     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
92         if let ExprKind::Binary(ref kind, ref lhs, ref rhs) = expr.kind {
93             Self::check_binop(cx, kind.node, lhs, rhs, expr.span);
94         }
95     }
96 }