Merge pull request #1394 from Manishearth/absurd_extreme_comparisons_false_positive

fix absurd extreme comparisons false positive
This commit is contained in:
Martin Carton 2016-12-25 17:47:42 +01:00 committed by GitHub
commit 7c480d0db7
2 changed files with 27 additions and 0 deletions

View file

@ -825,6 +825,12 @@ fn detect_absurd_comparison<'a>(
use types::AbsurdComparisonResult::*; use types::AbsurdComparisonResult::*;
use utils::comparisons::*; 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 normalized = normalize_comparison(op, lhs, rhs);
let (rel, normalized_lhs, normalized_rhs) = if let Some(val) = normalized { let (rel, normalized_lhs, normalized_rhs) = if let Some(val) = normalized {
val val

View file

@ -3,6 +3,7 @@
#![deny(absurd_extreme_comparisons)] #![deny(absurd_extreme_comparisons)]
#![allow(unused, eq_op, no_effect, unnecessary_operation)] #![allow(unused, eq_op, no_effect, unnecessary_operation)]
fn main() { fn main() {
const Z: u32 = 0; const Z: u32 = 0;
@ -70,3 +71,23 @@ fn main() {
// this is handled by unit_cmp // this is handled by unit_cmp
() < {}; //~WARNING <-comparison of unit values detected. () < {}; //~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
}