]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Inline utils::in_external_macro
[rust.git] / clippy_lints / src / neg_cmp_op_on_partial_ord.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use rustc::{declare_lint, lint_array};
4 use if_chain::if_chain;
5
6 use crate::utils::{self, paths, span_lint};
7
8 /// **What it does:**
9 /// Checks for the usage of negated comparision operators on types which only implement
10 /// `PartialOrd` (e.g. `f64`).
11 ///
12 /// **Why is this bad?**
13 /// These operators make it easy to forget that the underlying types actually allow not only three
14 /// potential Orderings (Less, Equal, Greater) but also a forth one (Uncomparable). Escpeccially if
15 /// the operator based comparision result is negated it is easy to miss that fact.
16 ///
17 /// **Known problems:** None.
18 ///
19 /// **Example:**
20 ///
21 /// ```rust
22 /// use core::cmp::Ordering;
23 ///
24 /// // Bad
25 /// let a = 1.0;
26 /// let b = std::f64::NAN;
27 ///
28 /// let _not_less_or_equal = !(a <= b);
29 ///
30 /// // Good
31 /// let a = 1.0;
32 /// let b = std::f64::NAN;
33 ///
34 /// let _not_less_or_equal = match a.partial_cmp(&b) {
35 ///     None | Some(Ordering::Greater) => true,
36 ///     _ => false,
37 /// };
38 /// ```
39 declare_clippy_lint! {
40     pub NEG_CMP_OP_ON_PARTIAL_ORD,
41     complexity,
42     "The use of negated comparision operators on partially orded types may produce confusing code."
43 }
44
45 pub struct NoNegCompOpForPartialOrd;
46
47 impl LintPass for NoNegCompOpForPartialOrd {
48     fn get_lints(&self) -> LintArray {
49         lint_array!(NEG_CMP_OP_ON_PARTIAL_ORD)
50     }
51 }
52
53 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
54
55     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
56         if_chain! {
57
58             if !in_external_macro(cx.sess(), expr.span);
59             if let ExprKind::Unary(UnOp::UnNot, ref inner) = expr.node;
60             if let ExprKind::Binary(ref op, ref left, _) = inner.node;
61             if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;
62
63             then {
64
65                 let ty = cx.tables.expr_ty(left);
66
67                 let implements_ord = {
68                     if let Some(id) = utils::get_trait_def_id(cx, &paths::ORD) {
69                         utils::implements_trait(cx, ty, id, &[])
70                     } else {
71                         return;
72                     }
73                 };
74
75                 let implements_partial_ord = {
76                     if let Some(id) = utils::get_trait_def_id(cx, &paths::PARTIAL_ORD) {
77                         utils::implements_trait(cx, ty, id, &[])
78                     } else {
79                         return;
80                     }
81                 };
82
83                 if implements_partial_ord && !implements_ord {
84                     span_lint(
85                         cx,
86                         NEG_CMP_OP_ON_PARTIAL_ORD,
87                         expr.span,
88                         "The use of negated comparision operators on partially orded \
89                         types produces code that is hard to read and refactor. Please \
90                         consider to use the `partial_cmp` instead, to make it clear \
91                         that the two values could be incomparable."
92                     )
93                 }
94             }
95         }
96     }
97 }