]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/neg_cmp_op_on_partial_ord.rs
Auto merge of #3598 - xfix:apply-cargo-fix-edition-idioms, r=phansch
[rust.git] / clippy_lints / src / neg_cmp_op_on_partial_ord.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use if_chain::if_chain;
11 use rustc::hir::*;
12 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
13 use rustc::{declare_tool_lint, lint_array};
14
15 use crate::utils::{self, paths, span_lint};
16
17 /// **What it does:**
18 /// Checks for the usage of negated comparison operators on types which only implement
19 /// `PartialOrd` (e.g. `f64`).
20 ///
21 /// **Why is this bad?**
22 /// These operators make it easy to forget that the underlying types actually allow not only three
23 /// potential Orderings (Less, Equal, Greater) but also a fourth one (Uncomparable). This is
24 /// especially easy to miss if the operator based comparison result is negated.
25 ///
26 /// **Known problems:** None.
27 ///
28 /// **Example:**
29 ///
30 /// ```rust
31 /// use std::cmp::Ordering;
32 ///
33 /// // Bad
34 /// let a = 1.0;
35 /// let b = std::f64::NAN;
36 ///
37 /// let _not_less_or_equal = !(a <= b);
38 ///
39 /// // Good
40 /// let a = 1.0;
41 /// let b = std::f64::NAN;
42 ///
43 /// let _not_less_or_equal = match a.partial_cmp(&b) {
44 ///     None | Some(Ordering::Greater) => true,
45 ///     _ => false,
46 /// };
47 /// ```
48 declare_clippy_lint! {
49     pub NEG_CMP_OP_ON_PARTIAL_ORD,
50     complexity,
51     "The use of negated comparison operators on partially ordered types may produce confusing code."
52 }
53
54 pub struct NoNegCompOpForPartialOrd;
55
56 impl LintPass for NoNegCompOpForPartialOrd {
57     fn get_lints(&self) -> LintArray {
58         lint_array!(NEG_CMP_OP_ON_PARTIAL_ORD)
59     }
60 }
61
62 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
63     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
64         if_chain! {
65
66             if !in_external_macro(cx.sess(), expr.span);
67             if let ExprKind::Unary(UnOp::UnNot, ref inner) = expr.node;
68             if let ExprKind::Binary(ref op, ref left, _) = inner.node;
69             if let BinOpKind::Le | BinOpKind::Ge | BinOpKind::Lt | BinOpKind::Gt = op.node;
70
71             then {
72
73                 let ty = cx.tables.expr_ty(left);
74
75                 let implements_ord = {
76                     if let Some(id) = utils::get_trait_def_id(cx, &paths::ORD) {
77                         utils::implements_trait(cx, ty, id, &[])
78                     } else {
79                         return;
80                     }
81                 };
82
83                 let implements_partial_ord = {
84                     if let Some(id) = utils::get_trait_def_id(cx, &paths::PARTIAL_ORD) {
85                         utils::implements_trait(cx, ty, id, &[])
86                     } else {
87                         return;
88                     }
89                 };
90
91                 if implements_partial_ord && !implements_ord {
92                     span_lint(
93                         cx,
94                         NEG_CMP_OP_ON_PARTIAL_ORD,
95                         expr.span,
96                         "The use of negated comparison operators on partially ordered \
97                         types produces code that is hard to read and refactor. Please \
98                         consider using the `partial_cmp` method instead, to make it \
99                         clear that the two values could be incomparable."
100                     )
101                 }
102             }
103         }
104     }
105 }