Merge pull request #2859 from 0ndorio/fix/false_positive_on_assert_in_neg_cmp_partial

Allows neg_cmp_op_on_partial_ord for external macros (fixes #2856).
This commit is contained in:
Oliver Schneider 2018-06-20 13:03:37 +02:00 committed by GitHub
commit a73d552f3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View 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 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
};
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 \

View 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);
}