2015-04-30 04:48:43 -05:00
|
|
|
use rustc::plugin::Registry;
|
|
|
|
use rustc::lint::*;
|
2015-04-30 08:17:06 -05:00
|
|
|
use rustc::middle::const_eval::lookup_const_by_id;
|
|
|
|
use rustc::middle::def::*;
|
2015-04-30 04:48:43 -05:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::ast_util::{is_comparison_binop, binop_to_string};
|
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax::codemap::Span;
|
2015-07-26 09:53:11 -05:00
|
|
|
use utils::span_lint;
|
2015-04-30 04:48:43 -05:00
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub BAD_BIT_MASK,
|
|
|
|
Deny,
|
2015-05-22 17:49:13 -05:00
|
|
|
"Deny the use of incompatible bit masks in comparisons, e.g. \
|
2015-08-11 13:22:20 -05:00
|
|
|
'(a & 1) == 2'"
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
|
2015-05-15 07:09:29 -05:00
|
|
|
declare_lint! {
|
2015-08-11 13:22:20 -05:00
|
|
|
pub INEFFECTIVE_BIT_MASK,
|
|
|
|
Warn,
|
|
|
|
"Warn on the use of an ineffective bit mask in comparisons, e.g. \
|
|
|
|
'(a & 1) > 2'"
|
2015-05-15 07:09:29 -05:00
|
|
|
}
|
|
|
|
|
2015-08-11 13:22:20 -05:00
|
|
|
/// Checks for incompatible bit masks in comparisons, e.g. `x & 1 == 2`.
|
2015-05-22 17:49:13 -05:00
|
|
|
/// This cannot work because the bit that makes up the value two was
|
|
|
|
/// zeroed out by the bit-and with 1. So the formula for detecting if an
|
2015-08-11 13:22:20 -05:00
|
|
|
/// expression of the type `_ <bit_op> m <cmp_op> c` (where `<bit_op>`
|
|
|
|
/// is one of {`&`, '|'} and `<cmp_op>` is one of {`!=`, `>=`, `>` ,
|
2015-05-22 17:49:13 -05:00
|
|
|
/// `!=`, `>=`, `>`}) can be determined from the following table:
|
2015-08-11 13:22:20 -05:00
|
|
|
///
|
2015-05-22 17:49:13 -05:00
|
|
|
/// |Comparison |Bit-Op|Example |is always|Formula |
|
|
|
|
/// |------------|------|------------|---------|----------------------|
|
|
|
|
/// |`==` or `!=`| `&` |`x & 2 == 3`|`false` |`c & m != c` |
|
|
|
|
/// |`<` or `>=`| `&` |`x & 2 < 3` |`true` |`m < c` |
|
|
|
|
/// |`>` or `<=`| `&` |`x & 1 > 1` |`false` |`m <= c` |
|
|
|
|
/// |`==` or `!=`| `|` |`x | 1 == 0`|`false` |`c | m != c` |
|
2015-05-23 05:32:29 -05:00
|
|
|
/// |`<` or `>=`| `|` |`x | 1 < 1` |`false` |`m >= c` |
|
2015-05-22 17:49:13 -05:00
|
|
|
/// |`<=` or `>` | `|` |`x | 1 > 0` |`true` |`m > c` |
|
2015-08-11 13:22:20 -05:00
|
|
|
///
|
2015-05-22 17:49:13 -05:00
|
|
|
/// This lint is **deny** by default
|
|
|
|
///
|
|
|
|
/// There is also a lint that warns on ineffective masks that is *warn*
|
|
|
|
/// by default
|
2015-04-30 04:48:43 -05:00
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct BitMask;
|
|
|
|
|
|
|
|
impl LintPass for BitMask {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2015-05-15 07:09:29 -05:00
|
|
|
lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK)
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
2015-08-11 13:22:20 -05:00
|
|
|
|
2015-04-30 04:48:43 -05:00
|
|
|
fn check_expr(&mut self, cx: &Context, e: &Expr) {
|
|
|
|
if let ExprBinary(ref cmp, ref left, ref right) = e.node {
|
2015-08-11 13:22:20 -05:00
|
|
|
if is_comparison_binop(cmp.node) {
|
|
|
|
fetch_int_literal(cx, right).map_or_else(||
|
|
|
|
fetch_int_literal(cx, left).map_or((), |cmp_val|
|
|
|
|
check_compare(cx, right, invert_cmp(cmp.node),
|
|
|
|
cmp_val, &e.span)),
|
|
|
|
|cmp_opt| check_compare(cx, left, cmp.node, cmp_opt,
|
|
|
|
&e.span))
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:17:15 -05:00
|
|
|
fn invert_cmp(cmp : BinOp_) -> BinOp_ {
|
2015-08-11 13:22:20 -05:00
|
|
|
match cmp {
|
|
|
|
BiEq => BiEq,
|
|
|
|
BiNe => BiNe,
|
|
|
|
BiLt => BiGt,
|
|
|
|
BiGt => BiLt,
|
|
|
|
BiLe => BiGe,
|
|
|
|
BiGe => BiLe,
|
|
|
|
_ => BiOr // Dummy
|
|
|
|
}
|
2015-05-04 00:17:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-30 04:48:43 -05:00
|
|
|
fn check_compare(cx: &Context, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u64, span: &Span) {
|
2015-08-11 13:22:20 -05:00
|
|
|
match &bit_op.node {
|
|
|
|
&ExprParen(ref subexp) => check_compare(cx, subexp, cmp_op, cmp_value, span),
|
|
|
|
&ExprBinary(ref op, ref left, ref right) => {
|
|
|
|
if op.node != BiBitAnd && op.node != BiBitOr { return; }
|
|
|
|
fetch_int_literal(cx, right).or_else(|| fetch_int_literal(
|
|
|
|
cx, left)).map_or((), |mask| check_bit_mask(cx, op.node,
|
|
|
|
cmp_op, mask, cmp_value, span))
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
|
2015-08-11 13:22:20 -05:00
|
|
|
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 {
|
|
|
|
if cmp_value != 0 {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"incompatible bit mask: `_ & {}` can never be equal to `{}`",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if mask_value == 0 {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span,
|
|
|
|
&format!("&-masking with zero"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BiBitOr => if mask_value | cmp_value != cmp_value {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"incompatible bit mask: `_ | {}` can never be equal to `{}`",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
},
|
|
|
|
BiLt | BiGe => match bit_op {
|
|
|
|
BiBitAnd => if mask_value < cmp_value {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"incompatible bit mask: `_ & {}` will always be lower than `{}`",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
} else {
|
|
|
|
if mask_value == 0 {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span,
|
|
|
|
&format!("&-masking with zero"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BiBitOr => if mask_value >= cmp_value {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"incompatible bit mask: `_ | {}` will never be lower than `{}`",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
} else {
|
|
|
|
if mask_value < cmp_value {
|
|
|
|
span_lint(cx, INEFFECTIVE_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"ineffective bit mask: `x | {}` compared to `{}` is the same as x compared directly",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
},
|
|
|
|
BiLe | BiGt => match bit_op {
|
|
|
|
BiBitAnd => if mask_value <= cmp_value {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"incompatible bit mask: `_ & {}` will never be higher than `{}`",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
} else {
|
|
|
|
if mask_value == 0 {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span,
|
|
|
|
&format!("&-masking with zero"));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BiBitOr => if mask_value > cmp_value {
|
|
|
|
span_lint(cx, BAD_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"incompatible bit mask: `_ | {}` will always be higher than `{}`",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
} else {
|
|
|
|
if mask_value < cmp_value {
|
|
|
|
span_lint(cx, INEFFECTIVE_BIT_MASK, *span, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"ineffective bit mask: `x | {}` compared to `{}` is the same as x compared directly",
|
2015-08-11 13:22:20 -05:00
|
|
|
mask_value, cmp_value));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
|
2015-04-30 08:17:06 -05:00
|
|
|
fn fetch_int_literal(cx: &Context, lit : &Expr) -> Option<u64> {
|
2015-08-11 13:22:20 -05:00
|
|
|
match &lit.node {
|
|
|
|
&ExprLit(ref lit_ptr) => {
|
|
|
|
if let &LitInt(value, _) = &lit_ptr.node {
|
|
|
|
Option::Some(value) //TODO: Handle sign
|
|
|
|
} else { Option::None }
|
|
|
|
},
|
|
|
|
&ExprPath(_, _) => {
|
|
|
|
// Important to let the borrow expire before the const lookup to avoid double
|
|
|
|
// borrowing.
|
|
|
|
let def_map = cx.tcx.def_map.borrow();
|
|
|
|
match def_map.get(&lit.id) {
|
|
|
|
Some(&PathResolution { base_def: DefConst(def_id), ..}) => Some(def_id),
|
|
|
|
_ => None
|
2015-05-06 23:41:54 -05:00
|
|
|
}
|
2015-08-11 13:22:20 -05:00
|
|
|
}
|
|
|
|
.and_then(|def_id| lookup_const_by_id(cx.tcx, def_id, Option::None))
|
|
|
|
.and_then(|l| fetch_int_literal(cx, l)),
|
|
|
|
_ => Option::None
|
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|