]> git.lizzy.rs Git - rust.git/commitdiff
Allows neg_cmp_op_on_partial_ord for external macros (fixes #2856).
authorBruno Kirschner <bruno@p3ki.com>
Wed, 20 Jun 2018 09:07:41 +0000 (11:07 +0200)
committerBruno Kirschner <bruno@p3ki.com>
Wed, 20 Jun 2018 09:58:15 +0000 (11:58 +0200)
The macro always negates the result of the given comparison in its
internal check which automatically triggered the lint. As its an
external macro there was no chance to do anything about it which lead
to a white listing of all external macros to prevent further issues.

clippy_lints/src/neg_cmp_op_on_partial_ord.rs
tests/ui/neg_cmp_op_on_partial_ord.rs

index 8e70d0eeba0a14a7e48d39e93920f774be9b5697..013bab69d798ba0657ea347639e7d6a6dc6c2a97 100644 (file)
@@ -1,7 +1,7 @@
 use rustc::hir::*;
 use rustc::lint::*;
 
-use crate::utils::{self, paths};
+use crate::utils::{self, paths, span_lint, in_external_macro};
 
 /// **What it does:**
 /// Checks for the usage of negated comparision operators on types which only implement
@@ -53,6 +53,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if_chain! {
 
+            if !in_external_macro(cx, expr.span);
             if let Expr_::ExprUnary(UnOp::UnNot, ref inner) = expr.node;
             if let Expr_::ExprBinary(ref op, ref left, _) = inner.node;
             if let BinOp_::BiLe | BinOp_::BiGe | BinOp_::BiLt | BinOp_::BiGt = op.node;
@@ -78,7 +79,8 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
                 };
 
                 if implements_partial_ord && !implements_ord {
-                    cx.span_lint(
+                    span_lint(
+                        cx,
                         NEG_CMP_OP_ON_PARTIAL_ORD,
                         expr.span,
                         "The use of negated comparision operators on partially orded \
index 214d627ba308e1249af2b587d3d8531c8f4e5a51..483972bb41b3ba982128898f497b5db55725b3bc 100644 (file)
@@ -1,6 +1,6 @@
-/// This test case utilizes `f64` an easy example for `PartialOrd` only types
-/// but the lint itself actually validates any expression where the left
-/// operand implements `PartialOrd` but not `Ord`.
+//! This test case utilizes `f64` an easy example for `PartialOrd` only types
+//! but the lint itself actually validates any expression where the left
+//! operand implements `PartialOrd` but not `Ord`.
 
 use std::cmp::Ordering;
 
@@ -54,5 +54,14 @@ fn main() {
     let _ = a_value <= another_value;
     let _ = a_value > another_value;
     let _ = a_value >= another_value;
-}
 
+    // --- regression tests ---
+
+    // Issue 2856: False positive on assert!()
+    //
+    // The macro always negates the result of the given comparision in its
+    // internal check which automatically triggered the lint. As it's an
+    // external macro there was no chance to do anything about it which lead
+    // to a whitelisting of all external macros.
+    assert!(a_value < another_value);
+}