]> git.lizzy.rs Git - rust.git/commitdiff
Fix #1159: avoid comparing fixed and target sized types in lint
authorkimsnj <karim.snj@gmail.com>
Fri, 12 Jan 2018 17:24:24 +0000 (18:24 +0100)
committerkimsnj <karim.snj@gmail.com>
Fri, 12 Jan 2018 17:33:25 +0000 (18:33 +0100)
clippy_lints/src/types.rs
tests/ui/absurd-extreme-comparisons.rs

index 7c536140f28f5a63589b08d8abd100c08ecdeb7a..c0c72408c7da572d80fc7114d43ff4547d8fbe73 100644 (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
index 1f88d94bd2ba683298fdd83ef471cace56507aa1..8c036e6c072557c3a4c257a39dc7098a19cf01c9 100644 (file)
@@ -50,3 +50,8 @@ fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
 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
+}