Added eq and neq handling to invalid upcast comparisons

This commit is contained in:
Taylor Cramer 2016-03-28 22:06:57 -07:00 committed by mcarton
parent 90a6117729
commit d050d601fc
2 changed files with 12 additions and 1 deletions

View File

@ -679,6 +679,7 @@ fn detect_absurd_comparison<'a>(cx: &LateContext, op: BinOp_, lhs: &'a Expr, rhs
_ => return None,
}
}
Rel::Ne | Rel::Eq => return None,
})
}
@ -914,14 +915,20 @@ fn upcast_comparison_bounds_err(
if let Some(nlb) = lhs_bounds {
if let Some(norm_rhs_val) = node_as_const_fullint(cx, rhs) {
if match rel {
if rel == Rel::Eq || rel == Rel::Ne {
if norm_rhs_val < nlb.0 || norm_rhs_val > nlb.0 {
err_upcast_comparison(cx, &span, lhs, rel == Rel::Ne);
}
} else if match rel {
Rel::Lt => if invert { norm_rhs_val < nlb.0 } else { nlb.1 < norm_rhs_val },
Rel::Le => if invert { norm_rhs_val <= nlb.0 } else { nlb.1 <= norm_rhs_val },
Rel::Eq | Rel::Ne => unreachable!(),
} {
err_upcast_comparison(cx, &span, lhs, true)
} else if match rel {
Rel::Lt => if invert { norm_rhs_val >= nlb.1 } else { nlb.0 >= norm_rhs_val },
Rel::Le => if invert { norm_rhs_val > nlb.1 } else { nlb.0 > norm_rhs_val },
Rel::Eq | Rel::Ne => unreachable!(),
} {
err_upcast_comparison(cx, &span, lhs, false)
}

View File

@ -4,6 +4,8 @@ use rustc_front::hir::{BinOp_, Expr};
pub enum Rel {
Lt,
Le,
Eq,
Ne,
}
/// Put the expression in the form `lhs < rhs` or `lhs <= rhs`.
@ -14,6 +16,8 @@ pub fn normalize_comparison<'a>(op: BinOp_, lhs: &'a Expr, rhs: &'a Expr)
BinOp_::BiLe => Some((Rel::Le, lhs, rhs)),
BinOp_::BiGt => Some((Rel::Lt, rhs, lhs)),
BinOp_::BiGe => Some((Rel::Le, rhs, lhs)),
BinOp_::BiEq => Some((Rel::Eq, rhs, lhs)),
BinOp_::BiNe => Some((Rel::Ne, rhs, lhs)),
_ => None,
}
}