From fedd3ef71182e0502c74ec9fa1e3b11a66832141 Mon Sep 17 00:00:00 2001 From: Bruno Kirschner Date: Wed, 20 Jun 2018 11:07:41 +0200 Subject: [PATCH] Allows neg_cmp_op_on_partial_ord for external macros (fixes #2856). 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 | 6 ++++-- tests/ui/neg_cmp_op_on_partial_ord.rs | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/neg_cmp_op_on_partial_ord.rs b/clippy_lints/src/neg_cmp_op_on_partial_ord.rs index 8e70d0eeba0..013bab69d79 100644 --- a/clippy_lints/src/neg_cmp_op_on_partial_ord.rs +++ b/clippy_lints/src/neg_cmp_op_on_partial_ord.rs @@ -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 \ diff --git a/tests/ui/neg_cmp_op_on_partial_ord.rs b/tests/ui/neg_cmp_op_on_partial_ord.rs index 214d627ba30..483972bb41b 100644 --- a/tests/ui/neg_cmp_op_on_partial_ord.rs +++ b/tests/ui/neg_cmp_op_on_partial_ord.rs @@ -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); +} -- 2.44.0