From 0b10a41ef396cc21d1526219ba81f3d0a57f17be Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 21 Dec 2016 16:49:53 +0100 Subject: [PATCH] fix absurd extreme comparisons false positive fixes #1387 --- clippy_lints/src/types.rs | 6 ++++++ .../absurd-extreme-comparisons.rs | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 5885d7863..9912c5249 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -825,6 +825,12 @@ fn detect_absurd_comparison<'a>( use types::AbsurdComparisonResult::*; use utils::comparisons::*; + // absurd comparison only makes sense on primitive types + // primitive types don't implement comparison operators with each other + if cx.tcx.tables().expr_ty(lhs) != cx.tcx.tables().expr_ty(rhs) { + return None; + } + let normalized = normalize_comparison(op, lhs, rhs); let (rel, normalized_lhs, normalized_rhs) = if let Some(val) = normalized { val diff --git a/tests/compile-fail/absurd-extreme-comparisons.rs b/tests/compile-fail/absurd-extreme-comparisons.rs index 81cb658df..cf69473bb 100644 --- a/tests/compile-fail/absurd-extreme-comparisons.rs +++ b/tests/compile-fail/absurd-extreme-comparisons.rs @@ -3,6 +3,7 @@ #![deny(absurd_extreme_comparisons)] #![allow(unused, eq_op, no_effect, unnecessary_operation)] + fn main() { const Z: u32 = 0; @@ -70,3 +71,23 @@ fn main() { // this is handled by unit_cmp () < {}; //~WARNING <-comparison of unit values detected. } + +use std::cmp::{Ordering, PartialEq, PartialOrd}; + +#[derive(PartialEq, PartialOrd)] +pub struct U(u64); + +impl PartialEq for U { + fn eq(&self, other: &u32) -> bool { + self.eq(&U(*other as u64)) + } +} +impl PartialOrd for U { + fn partial_cmp(&self, other: &u32) -> Option { + self.partial_cmp(&U(*other as u64)) + } +} + +pub fn foo(val: U) -> bool { + val > std::u32::MAX +}