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