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