1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use rustc::lint::*;
use rustc::middle::const_eval::lookup_const_by_id;
use rustc::middle::def::*;
use rustc_front::hir::*;
use rustc_front::util::is_comparison_binop;
use syntax::codemap::Span;
use syntax::ast::Lit_::*;
use utils::span_lint;
declare_lint! {
pub BAD_BIT_MASK,
Warn,
"expressions of the form `_ & mask == select` that will only ever return `true` or `false` \
(because in the example `select` containing bits that `mask` doesn't have)"
}
declare_lint! {
pub INEFFECTIVE_BIT_MASK,
Warn,
"expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
}
#[derive(Copy,Clone)]
pub struct BitMask;
impl LintPass for BitMask {
fn get_lints(&self) -> LintArray {
lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK)
}
}
impl LateLintPass for BitMask {
fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
if let ExprBinary(ref cmp, ref left, ref right) = e.node {
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))
}
}
}
}
fn invert_cmp(cmp : BinOp_) -> BinOp_ {
match cmp {
BiEq => BiEq,
BiNe => BiNe,
BiLt => BiGt,
BiGt => BiLt,
BiLe => BiGe,
BiGe => BiLe,
_ => BiOr
}
}
fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u64, span: &Span) {
if let ExprBinary(ref op, ref left, ref right) = bit_op.node {
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))
}
}
fn check_bit_mask(cx: &LateContext, 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 != cmp_value {
if cmp_value != 0 {
span_lint(cx, BAD_BIT_MASK, *span, &format!(
"incompatible bit mask: `_ & {}` can never be equal to `{}`",
mask_value, cmp_value));
}
} else {
if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
}
},
BiBitOr => if mask_value | cmp_value != cmp_value {
span_lint(cx, BAD_BIT_MASK, *span, &format!(
"incompatible bit mask: `_ | {}` can never be equal to `{}`",
mask_value, cmp_value));
},
_ => ()
},
BiLt | BiGe => match bit_op {
BiBitAnd => if mask_value < cmp_value {
span_lint(cx, BAD_BIT_MASK, *span, &format!(
"incompatible bit mask: `_ & {}` will always be lower than `{}`",
mask_value, cmp_value));
} else {
if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
}
},
BiBitOr => if mask_value >= cmp_value {
span_lint(cx, BAD_BIT_MASK, *span, &format!(
"incompatible bit mask: `_ | {}` will never be lower than `{}`",
mask_value, cmp_value));
} else {
check_ineffective_lt(cx, *span, mask_value, cmp_value, "|");
},
BiBitXor =>
check_ineffective_lt(cx, *span, mask_value, cmp_value, "^"),
_ => ()
},
BiLe | BiGt => match bit_op {
BiBitAnd => if mask_value <= cmp_value {
span_lint(cx, BAD_BIT_MASK, *span, &format!(
"incompatible bit mask: `_ & {}` will never be higher than `{}`",
mask_value, cmp_value));
} else {
if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
}
},
BiBitOr => if mask_value > cmp_value {
span_lint(cx, BAD_BIT_MASK, *span, &format!(
"incompatible bit mask: `_ | {}` will always be higher than `{}`",
mask_value, cmp_value));
} else {
check_ineffective_gt(cx, *span, mask_value, cmp_value, "|");
},
BiBitXor =>
check_ineffective_gt(cx, *span, mask_value, cmp_value, "^"),
_ => ()
},
_ => ()
}
}
fn check_ineffective_lt(cx: &LateContext, span: Span, m: u64, c: u64, op: &str) {
if c.is_power_of_two() && m < c {
span_lint(cx, INEFFECTIVE_BIT_MASK, span, &format!(
"ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
op, m, c));
}
}
fn check_ineffective_gt(cx: &LateContext, span: Span, m: u64, c: u64, op: &str) {
if (c + 1).is_power_of_two() && m <= c {
span_lint(cx, INEFFECTIVE_BIT_MASK, span, &format!(
"ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
op, m, c));
}
}
fn fetch_int_literal(cx: &LateContext, lit : &Expr) -> Option<u64> {
match lit.node {
ExprLit(ref lit_ptr) => {
if let &LitInt(value, _) = &lit_ptr.node {
Option::Some(value)
} else { Option::None }
},
ExprPath(_, _) => {
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
}
}
.and_then(|def_id| lookup_const_by_id(cx.tcx, def_id, Option::None))
.and_then(|l| fetch_int_literal(cx, l)),
_ => Option::None
}
}