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