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