Fix #1159: avoid comparing fixed and target sized types in lint

This commit is contained in:
kimsnj 2018-01-12 18:24:24 +01:00
parent 1245de1e46
commit 53c0ae0169
2 changed files with 24 additions and 0 deletions

View file

@ -1106,6 +1106,20 @@ enum AbsurdComparisonResult {
}
fn is_cast_between_fixed_and_target<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
expr: &'tcx Expr
) -> bool {
if let ExprCast(ref cast_exp, _) = expr.node {
let precast_ty = cx.tables.expr_ty(cast_exp);
let cast_ty = cx.tables.expr_ty(expr);
return is_isize_or_usize(precast_ty) != is_isize_or_usize(cast_ty)
}
return false;
}
fn detect_absurd_comparison<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
@ -1123,6 +1137,11 @@ fn detect_absurd_comparison<'a, 'tcx>(
return None;
}
// comparisons between fix sized types and target sized types are considered unanalyzable
if is_cast_between_fixed_and_target(cx, lhs) || is_cast_between_fixed_and_target(cx, rhs) {
return None;
}
let normalized = normalize_comparison(op, lhs, rhs);
let (rel, normalized_lhs, normalized_rhs) = if let Some(val) = normalized {
val

View file

@ -50,3 +50,8 @@ impl PartialOrd<u32> for U {
pub fn foo(val: U) -> bool {
val > std::u32::MAX
}
pub fn bar(len: u64) -> bool {
// This is OK as we are casting from target sized to fixed size
len >= std::usize::MAX as u64
}