2 use rustc::hir::def::Def;
4 use rustc_const_eval::lookup_const_by_id;
5 use syntax::ast::LitKind;
6 use syntax::codemap::Span;
9 /// **What it does:** Checks for incompatible bit masks in comparisons.
11 /// The formula for detecting if an expression of the type `_ <bit_op> m
12 /// <cmp_op> c` (where `<bit_op>` is one of {`&`, `|`} and `<cmp_op>` is one of
13 /// {`!=`, `>=`, `>`, `!=`, `>=`, `>`}) can be determined from the following
16 /// |Comparison |Bit Op|Example |is always|Formula |
17 /// |------------|------|------------|---------|----------------------|
18 /// |`==` or `!=`| `&` |`x & 2 == 3`|`false` |`c & m != c` |
19 /// |`<` or `>=`| `&` |`x & 2 < 3` |`true` |`m < c` |
20 /// |`>` or `<=`| `&` |`x & 1 > 1` |`false` |`m <= c` |
21 /// |`==` or `!=`| `|` |`x | 1 == 0`|`false` |`c | m != c` |
22 /// |`<` or `>=`| `|` |`x | 1 < 1` |`false` |`m >= c` |
23 /// |`<=` or `>` | `|` |`x | 1 > 0` |`true` |`m > c` |
25 /// **Why is this bad?** If the bits that the comparison cares about are always
26 /// set to zero or one by the bit mask, the comparison is constant `true` or
27 /// `false` (depending on mask, compared value, and operators).
29 /// So the code is actively misleading, and the only reason someone would write
30 /// this intentionally is to win an underhanded Rust contest or create a
31 /// test-case for this lint.
33 /// **Known problems:** None.
37 /// if (x & 1 == 2) { … }
42 "expressions of the form `_ & mask == select` that will only ever return `true` or `false`"
45 /// **What it does:** Checks for bit masks in comparisons which can be removed
46 /// without changing the outcome. The basic structure can be seen in the
49 /// |Comparison| Bit Op |Example |equals |
50 /// |----------|---------|-----------|-------|
51 /// |`>` / `<=`|`|` / `^`|`x | 2 > 3`|`x > 3`|
52 /// |`<` / `>=`|`|` / `^`|`x ^ 1 < 4`|`x < 4`|
54 /// **Why is this bad?** Not equally evil as [`bad_bit_mask`](#bad_bit_mask),
55 /// but still a bit misleading, because the bit mask is ineffective.
57 /// **Known problems:** False negatives: This lint will only match instances
58 /// where we have figured out the math (which is for a power-of-two compared
59 /// value). This means things like `x | 1 >= 7` (which would be better written
60 /// as `x >= 6`) will not be reported (but bit masks like this are fairly
65 /// if (x | 1 > 3) { … }
68 pub INEFFECTIVE_BIT_MASK,
70 "expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
76 impl LintPass for BitMask {
77 fn get_lints(&self) -> LintArray {
78 lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK)
82 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
83 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
84 if let ExprBinary(ref cmp, ref left, ref right) = e.node {
85 if cmp.node.is_comparison() {
86 if let Some(cmp_opt) = fetch_int_literal(cx, right) {
87 check_compare(cx, left, cmp.node, cmp_opt, &e.span)
88 } else if let Some(cmp_val) = fetch_int_literal(cx, left) {
89 check_compare(cx, right, invert_cmp(cmp.node), cmp_val, &e.span)
96 fn invert_cmp(cmp: BinOp_) -> BinOp_ {
109 fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u64, span: &Span) {
110 if let ExprBinary(ref op, ref left, ref right) = bit_op.node {
111 if op.node != BiBitAnd && op.node != BiBitOr {
114 fetch_int_literal(cx, right)
115 .or_else(|| fetch_int_literal(cx, left))
116 .map_or((), |mask| check_bit_mask(cx, op.node, cmp_op, mask, cmp_value, span))
120 fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u64, cmp_value: u64, span: &Span) {
125 if mask_value & cmp_value != cmp_value {
130 &format!("incompatible bit mask: `_ & {}` can never be equal to `{}`",
134 } else if mask_value == 0 {
135 span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
139 if mask_value | cmp_value != cmp_value {
143 &format!("incompatible bit mask: `_ | {}` can never be equal to `{}`",
154 if mask_value < cmp_value {
158 &format!("incompatible bit mask: `_ & {}` will always be lower than `{}`",
161 } else if mask_value == 0 {
162 span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
166 if mask_value >= cmp_value {
170 &format!("incompatible bit mask: `_ | {}` will never be lower than `{}`",
174 check_ineffective_lt(cx, *span, mask_value, cmp_value, "|");
177 BiBitXor => check_ineffective_lt(cx, *span, mask_value, cmp_value, "^"),
184 if mask_value <= cmp_value {
188 &format!("incompatible bit mask: `_ & {}` will never be higher than `{}`",
191 } else if mask_value == 0 {
192 span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
196 if mask_value > cmp_value {
200 &format!("incompatible bit mask: `_ | {}` will always be higher than `{}`",
204 check_ineffective_gt(cx, *span, mask_value, cmp_value, "|");
207 BiBitXor => check_ineffective_gt(cx, *span, mask_value, cmp_value, "^"),
215 fn check_ineffective_lt(cx: &LateContext, span: Span, m: u64, c: u64, op: &str) {
216 if c.is_power_of_two() && m < c {
218 INEFFECTIVE_BIT_MASK,
220 &format!("ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
227 fn check_ineffective_gt(cx: &LateContext, span: Span, m: u64, c: u64, op: &str) {
228 if (c + 1).is_power_of_two() && m <= c {
230 INEFFECTIVE_BIT_MASK,
232 &format!("ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
239 fn fetch_int_literal(cx: &LateContext, lit: &Expr) -> Option<u64> {
241 ExprLit(ref lit_ptr) => {
242 if let LitKind::Int(value, _) = lit_ptr.node {
243 Some(value) //TODO: Handle sign
248 ExprPath(ref qpath) => {
249 let def = cx.tcx.tables().qpath_def(qpath, lit.id);
250 if let Def::Const(def_id) = def {
251 lookup_const_by_id(cx.tcx, def_id, None).and_then(|(l, _ty)| fetch_int_literal(cx, l))