]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/double_comparison.rs
Rollup merge of #87759 - m-ou-se:linux-process-sealed, r=jyn514
[rust.git] / src / tools / clippy / clippy_lints / src / double_comparison.rs
1 //! Lint on unnecessary double comparisons. Some examples:
2
3 use clippy_utils::diagnostics::span_lint_and_sugg;
4 use clippy_utils::eq_expr_value;
5 use clippy_utils::source::snippet_with_applicability;
6 use rustc_errors::Applicability;
7 use rustc_hir::{BinOpKind, Expr, ExprKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 use rustc_span::source_map::Span;
11
12 declare_clippy_lint! {
13     /// ### What it does
14     /// Checks for double comparisons that could be simplified to a single expression.
15     ///
16     ///
17     /// ### Why is this bad?
18     /// Readability.
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<'tcx> DoubleComparisons {
42     #[allow(clippy::similar_names)]
43     fn check_binop(cx: &LateContext<'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         if !(eq_expr_value(cx, llhs, rlhs) && eq_expr_value(cx, lrhs, rrhs)) {
51             return;
52         }
53         macro_rules! lint_double_comparison {
54             ($op:tt) => {{
55                 let mut applicability = Applicability::MachineApplicable;
56                 let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
57                 let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
58                 let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
59                 span_lint_and_sugg(
60                     cx,
61                     DOUBLE_COMPARISONS,
62                     span,
63                     "this binary expression can be simplified",
64                     "try",
65                     sugg,
66                     applicability,
67                 );
68             }};
69         }
70         #[rustfmt::skip]
71         match (op, lkind, rkind) {
72             (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Lt) | (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Eq) => {
73                 lint_double_comparison!(<=);
74             },
75             (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Eq) => {
76                 lint_double_comparison!(>=);
77             },
78             (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Lt) => {
79                 lint_double_comparison!(!=);
80             },
81             (BinOpKind::And, BinOpKind::Le, BinOpKind::Ge) | (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => {
82                 lint_double_comparison!(==);
83             },
84             _ => (),
85         };
86     }
87 }
88
89 impl<'tcx> LateLintPass<'tcx> for DoubleComparisons {
90     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
91         if let ExprKind::Binary(ref kind, lhs, rhs) = expr.kind {
92             Self::check_binop(cx, kind.node, lhs, rhs, expr.span);
93         }
94     }
95 }