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