]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Merge pull request #2827 from 0ndorio/lint/cmp_operators_on_partial_cmp
[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};
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 let Expr_::ExprUnary(UnOp::UnNot, ref inner) = expr.node;
57             if let Expr_::ExprBinary(ref op, ref left, _) = inner.node;
58             if let BinOp_::BiLe | BinOp_::BiGe | BinOp_::BiLt | BinOp_::BiGt = op.node;
59
60             then {
61
62                 let ty = cx.tables.expr_ty(left);
63
64                 let implements_ord = {
65                     if let Some(id) = utils::get_trait_def_id(cx, &paths::ORD) {
66                         utils::implements_trait(cx, ty, id, &[])
67                     } else {
68                         return;
69                     }
70                 };
71
72                 let implements_partial_ord = {
73                     if let Some(id) = utils::get_trait_def_id(cx, &paths::PARTIAL_ORD) {
74                         utils::implements_trait(cx, ty, id, &[])
75                     } else {
76                         return;
77                     }
78                 };
79
80                 if implements_partial_ord && !implements_ord {
81                     cx.span_lint(
82                         NEG_CMP_OP_ON_PARTIAL_ORD,
83                         expr.span,
84                         "The use of negated comparision operators on partially orded \
85                         types produces code that is hard to read and refactor. Please \
86                         consider to use the `partial_cmp` instead, to make it clear \
87                         that the two values could be incomparable."
88                     )
89                 }
90             }
91         }
92     }
93 }