]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Auto merge of #3767 - alexreg:cosmetic-2, r=flip1995
[rust.git] / clippy_lints / src / neg_cmp_op_on_partial_ord.rs
1 use if_chain::if_chain;
2 use rustc::hir::*;
3 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
5
6 use crate::utils::{self, paths, span_lint};
7
8 declare_clippy_lint! {
9     /// **What it does:**
10     /// Checks for the usage of negated comparison 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 fourth one (Uncomparable). This is
16     /// especially easy to miss if the operator based comparison result is negated.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     ///
22     /// ```rust
23     /// use std::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     pub NEG_CMP_OP_ON_PARTIAL_ORD,
41     complexity,
42     "The use of negated comparison operators on partially ordered 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     fn name(&self) -> &'static str {
53         "NoNegCompOpForPartialOrd"
54     }
55 }
56
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
58     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
59         if_chain! {
60
61             if !in_external_macro(cx.sess(), expr.span);
62             if let ExprKind::Unary(UnOp::UnNot, ref inner) = expr.node;
63             if let ExprKind::Binary(ref op, ref left, _) = inner.node;
64             if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;
65
66             then {
67
68                 let ty = cx.tables.expr_ty(left);
69
70                 let implements_ord = {
71                     if let Some(id) = utils::get_trait_def_id(cx, &paths::ORD) {
72                         utils::implements_trait(cx, ty, id, &[])
73                     } else {
74                         return;
75                     }
76                 };
77
78                 let implements_partial_ord = {
79                     if let Some(id) = utils::get_trait_def_id(cx, &paths::PARTIAL_ORD) {
80                         utils::implements_trait(cx, ty, id, &[])
81                     } else {
82                         return;
83                     }
84                 };
85
86                 if implements_partial_ord && !implements_ord {
87                     span_lint(
88                         cx,
89                         NEG_CMP_OP_ON_PARTIAL_ORD,
90                         expr.span,
91                         "The use of negated comparison operators on partially ordered \
92                         types produces code that is hard to read and refactor. Please \
93                         consider using the `partial_cmp` method instead, to make it \
94                         clear that the two values could be incomparable."
95                     )
96                 }
97             }
98         }
99     }
100 }