]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Added lint to avoid negated comparisions on partially ordered types.
[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;
5
6 const ORD: [&str; 3] = ["core", "cmp", "Ord"];
7 const PARTIAL_ORD: [&str; 3] = ["core", "cmp", "PartialOrd"];
8
9 /// **What it does:**
10 /// Checks for the usage of negated comparision operators on types which only implement
11 /// `PartialOrd` (e.g. `f64`).
12 ///
13 /// **Why is this bad?**
14 /// These operators make it easy to forget that the underlying types actually allow not only three
15 /// potential Orderings (Less, Equal, Greater) but also a forth one (Uncomparable). Escpeccially if
16 /// the operator based comparision result is negated it is easy to miss that fact.
17 ///
18 /// **Known problems:** None.
19 ///
20 /// **Example:**
21 ///
22 /// ```rust
23 /// use core::cmp::Ordering;
24 /// 
25 /// // Bad
26 /// let a = 1.0;
27 /// let b = std::f64::NAN;
28 /// 
29 /// let _not_less_or_equal = !(a <= b);
30 ///
31 /// // Good
32 /// let a = 1.0;
33 /// let b = std::f64::NAN;
34 /// 
35 /// let _not_less_or_equal = match a.partial_cmp(&b) {
36 ///     None | Some(Ordering::Greater) => true,
37 ///     _ => false, 
38 /// };
39 /// ```
40 declare_lint! {
41     pub NEG_CMP_OP_ON_PARTIAL_ORD, Warn,
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 let Expr_::ExprUnary(UnOp::UnNot, ref inner) = expr.node;
59             if let Expr_::ExprBinary(ref op, ref left, _) = inner.node;
60             if let BinOp_::BiLe | BinOp_::BiGe | BinOp_::BiLt | BinOp_::BiGt = op.node;
61
62             then {
63
64                 let ty = cx.tables.expr_ty(left);
65
66                 let implements_ord = {
67                     if let Some(id) = utils::get_trait_def_id(cx, &ORD) {
68                         utils::implements_trait(cx, ty, id, &[])
69                     } else {
70                         return;
71                     }
72                 };
73
74                 let implements_partial_ord = {
75                     if let Some(id) = utils::get_trait_def_id(cx, &PARTIAL_ORD) {
76                         utils::implements_trait(cx, ty, id, &[])
77                     } else {
78                         return;
79                     }
80                 };
81
82                 if implements_partial_ord && !implements_ord {
83                     cx.span_lint(
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 }