]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Fixed spelling and indentation issues in neg_cmp_op_on_partial_ord related files.
[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_clippy_lint! {
41     pub NEG_CMP_OP_ON_PARTIAL_ORD,
42     complexity,
43     "The use of negated comparision operators on partially orded types may produce confusing code."
44 }
45
46 pub struct NoNegCompOpForPartialOrd;
47
48 impl LintPass for NoNegCompOpForPartialOrd {
49     fn get_lints(&self) -> LintArray {
50         lint_array!(NEG_CMP_OP_ON_PARTIAL_ORD)
51     }
52 }
53
54 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
55
56     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
57         if_chain! {
58
59             if let Expr_::ExprUnary(UnOp::UnNot, ref inner) = expr.node;
60             if let Expr_::ExprBinary(ref op, ref left, _) = inner.node;
61             if let BinOp_::BiLe | BinOp_::BiGe | BinOp_::BiLt | BinOp_::BiGt = 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, &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, &PARTIAL_ORD) {
77                         utils::implements_trait(cx, ty, id, &[])
78                     } else {
79                         return;
80                     }
81                 };
82
83                 if implements_partial_ord && !implements_ord {
84                     cx.span_lint(
85                         NEG_CMP_OP_ON_PARTIAL_ORD,
86                         expr.span,
87                         "The use of negated comparision operators on partially orded \
88                         types produces code that is hard to read and refactor. Please \
89                         consider to use the `partial_cmp` instead, to make it clear \
90                         that the two values could be incomparable."
91                     )
92                 }
93             }
94         }
95     }
96 }