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