]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Rollup merge of #103117 - joshtriplett:use-is-terminal, r=eholk
[rust.git] / src / tools / clippy / 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 if_chain::if_chain;
4 use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
5 use rustc_lint::{LateContext, LateLintPass, LintContext};
6 use rustc_middle::lint::in_external_macro;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::sym;
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     /// ### Example
21     /// ```rust
22     /// let a = 1.0;
23     /// let b = f64::NAN;
24     ///
25     /// let not_less_or_equal = !(a <= b);
26     /// ```
27     ///
28     /// Use instead:
29     /// ```rust
30     /// use std::cmp::Ordering;
31     /// # let a = 1.0;
32     /// # let b = f64::NAN;
33     ///
34     /// let _not_less_or_equal = match a.partial_cmp(&b) {
35     ///     None | Some(Ordering::Greater) => true,
36     ///     _ => false,
37     /// };
38     /// ```
39     #[clippy::version = "pre 1.29.0"]
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 declare_lint_pass!(NoNegCompOpForPartialOrd => [NEG_CMP_OP_ON_PARTIAL_ORD]);
46
47 impl<'tcx> LateLintPass<'tcx> for NoNegCompOpForPartialOrd {
48     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
49         if_chain! {
50             if !in_external_macro(cx.sess(), expr.span);
51             if let ExprKind::Unary(UnOp::Not, inner) = expr.kind;
52             if let ExprKind::Binary(ref op, left, _) = inner.kind;
53             if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;
54
55             then {
56                 let ty = cx.typeck_results().expr_ty(left);
57
58                 let implements_ord = {
59                     if let Some(id) = cx.tcx.get_diagnostic_item(sym::Ord) {
60                         implements_trait(cx, ty, id, &[])
61                     } else {
62                         return;
63                     }
64                 };
65
66                 let implements_partial_ord = {
67                     if let Some(id) = cx.tcx.lang_items().partial_ord_trait() {
68                         implements_trait(cx, ty, id, &[])
69                     } else {
70                         return;
71                     }
72                 };
73
74                 if implements_partial_ord && !implements_ord {
75                     span_lint(
76                         cx,
77                         NEG_CMP_OP_ON_PARTIAL_ORD,
78                         expr.span,
79                         "the use of negated comparison operators on partially ordered \
80                         types produces code that is hard to read and refactor, please \
81                         consider using the `partial_cmp` method instead, to make it \
82                         clear that the two values could be incomparable",
83                     );
84                 }
85             }
86         }
87     }
88 }