fix absurd extreme comparisons false positive

fixes #1387
This commit is contained in:
Oliver Schneider 2016-12-21 16:49:53 +01:00
parent cb861a1bd1
commit 0b10a41ef3
No known key found for this signature in database
GPG Key ID: 56D6EEA0FC67AC46
2 changed files with 27 additions and 0 deletions

View File

@ -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

View File

@ -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<u32> for U {
fn eq(&self, other: &u32) -> bool {
self.eq(&U(*other as u64))
}
}
impl PartialOrd<u32> for U {
fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
self.partial_cmp(&U(*other as u64))
}
}
pub fn foo(val: U) -> bool {
val > std::u32::MAX
}