Merge pull request #2347 from kimsnj/extrem_comp

Fix #1159: avoid comparing fixed and target sized types in lint
This commit is contained in:
Oliver Schneider 2018-01-13 12:27:36 +01:00 committed by GitHub
commit cc9008b7d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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
}