Skip to content

Commit fedd3ef

Browse files
committed
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.
1 parent e4d31ab commit fedd3ef

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

clippy_lints/src/neg_cmp_op_on_partial_ord.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::hir::*;
22
use rustc::lint::*;
33

4-
use crate::utils::{self, paths};
4+
use crate::utils::{self, paths, span_lint, in_external_macro};
55

66
/// **What it does:**
77
/// 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 {
5353
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
5454
if_chain! {
5555

56+
if !in_external_macro(cx, expr.span);
5657
if let Expr_::ExprUnary(UnOp::UnNot, ref inner) = expr.node;
5758
if let Expr_::ExprBinary(ref op, ref left, _) = inner.node;
5859
if let BinOp_::BiLe | BinOp_::BiGe | BinOp_::BiLt | BinOp_::BiGt = op.node;
@@ -78,7 +79,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
7879
};
7980

8081
if implements_partial_ord && !implements_ord {
81-
cx.span_lint(
82+
span_lint(
83+
cx,
8284
NEG_CMP_OP_ON_PARTIAL_ORD,
8385
expr.span,
8486
"The use of negated comparision operators on partially orded \

tests/ui/neg_cmp_op_on_partial_ord.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/// This test case utilizes `f64` an easy example for `PartialOrd` only types
2-
/// but the lint itself actually validates any expression where the left
3-
/// operand implements `PartialOrd` but not `Ord`.
1+
//! This test case utilizes `f64` an easy example for `PartialOrd` only types
2+
//! but the lint itself actually validates any expression where the left
3+
//! operand implements `PartialOrd` but not `Ord`.
44
55
use std::cmp::Ordering;
66

@@ -54,5 +54,14 @@ fn main() {
5454
let _ = a_value <= another_value;
5555
let _ = a_value > another_value;
5656
let _ = a_value >= another_value;
57-
}
5857

58+
// --- regression tests ---
59+
60+
// Issue 2856: False positive on assert!()
61+
//
62+
// The macro always negates the result of the given comparision in its
63+
// internal check which automatically triggered the lint. As it's an
64+
// external macro there was no chance to do anything about it which lead
65+
// to a whitelisting of all external macros.
66+
assert!(a_value < another_value);
67+
}

0 commit comments

Comments
 (0)