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