From 8563ee60ecf02be54c39dad0c063763c9dce4e91 Mon Sep 17 00:00:00 2001 From: llogiq Date: Sat, 6 Jun 2015 02:27:48 +0200 Subject: [PATCH] fixed issue #88 in bit_mask --- src/bit_mask.rs | 24 ++++++++++++++++-------- tests/compile-fail/bit_masks.rs | 1 + 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/bit_mask.rs b/src/bit_mask.rs index 352826dff..f3f95f92d 100644 --- a/src/bit_mask.rs +++ b/src/bit_mask.rs @@ -89,20 +89,26 @@ fn check_compare(cx: &Context, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u64, sp } } -fn check_bit_mask(cx: &Context, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u64, cmp_value: u64, span: &Span) { +fn check_bit_mask(cx: &Context, bit_op: BinOp_, cmp_op: BinOp_, + mask_value: u64, cmp_value: u64, span: &Span) { match cmp_op { BiEq | BiNe => match bit_op { BiBitAnd => if mask_value & cmp_value != mask_value { - cx.span_lint(BAD_BIT_MASK, *span, &format!("incompatible bit mask: _ & {} can never be equal to {}", mask_value, - cmp_value)); + if cmp_value != 0 { + cx.span_lint(BAD_BIT_MASK, *span, &format!( + "incompatible bit mask: _ & {} can never be equal to {}", + mask_value, cmp_value)); + } } else { if mask_value == 0 { - cx.span_lint(BAD_BIT_MASK, *span, &format!("&-masking with zero")); + cx.span_lint(BAD_BIT_MASK, *span, + &format!("&-masking with zero")); } }, BiBitOr => if mask_value | cmp_value != cmp_value { - cx.span_lint(BAD_BIT_MASK, *span, &format!("incompatible bit mask: _ | {} can never be equal to {}", mask_value, - cmp_value)); + cx.span_lint(BAD_BIT_MASK, *span, &format!( + "incompatible bit mask: _ | {} can never be equal to {}", + mask_value, cmp_value)); }, _ => () }, @@ -113,7 +119,8 @@ fn check_bit_mask(cx: &Context, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u64, mask_value, cmp_value)); } else { if mask_value == 0 { - cx.span_lint(BAD_BIT_MASK, *span, &format!("&-masking with zero")); + cx.span_lint(BAD_BIT_MASK, *span, + &format!("&-masking with zero")); } }, BiBitOr => if mask_value >= cmp_value { @@ -136,7 +143,8 @@ fn check_bit_mask(cx: &Context, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u64, mask_value, cmp_value)); } else { if mask_value == 0 { - cx.span_lint(BAD_BIT_MASK, *span, &format!("&-masking with zero")); + cx.span_lint(BAD_BIT_MASK, *span, + &format!("&-masking with zero")); } }, BiBitOr => if mask_value > cmp_value { diff --git a/tests/compile-fail/bit_masks.rs b/tests/compile-fail/bit_masks.rs index e45b78980..e6b89b985 100644 --- a/tests/compile-fail/bit_masks.rs +++ b/tests/compile-fail/bit_masks.rs @@ -11,6 +11,7 @@ fn main() { x & 0 == 0; //~ERROR &-masking with zero x & 1 == 1; //ok, distinguishes bit 0 + x & 1 == 0; //ok, compared with zero x & 2 == 1; //~ERROR x | 0 == 0; //ok, equals x == 0 (maybe warn?) x | 1 == 3; //ok, equals x == 2 || x == 3