2016-04-07 10:46:48 -05:00
|
|
|
use rustc::hir::*;
|
2015-04-30 04:48:43 -05:00
|
|
|
use rustc::lint::*;
|
2018-07-19 02:53:23 -05:00
|
|
|
use rustc::{declare_lint, lint_array};
|
2016-02-12 11:35:44 -06:00
|
|
|
use syntax::ast::LitKind;
|
2017-04-27 07:00:35 -05:00
|
|
|
use syntax::codemap::Span;
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{span_lint, span_lint_and_then};
|
|
|
|
use crate::utils::sugg::Sugg;
|
|
|
|
use crate::consts::{constant, Constant};
|
2015-04-30 04:48:43 -05:00
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for incompatible bit masks in comparisons.
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// The formula for detecting if an expression of the type `_ <bit_op> m
|
|
|
|
/// <cmp_op> c` (where `<bit_op>` is one of {`&`, `|`} and `<cmp_op>` is one of
|
|
|
|
/// {`!=`, `>=`, `>`, `!=`, `>=`, `>`}) can be determined from the following
|
|
|
|
/// table:
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-03-28 14:23:21 -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` |
|
|
|
|
/// |`<` or `>=`| `|` |`x | 1 < 1` |`false` |`m >= c` |
|
|
|
|
/// |`<=` or `>` | `|` |`x | 1 > 0` |`true` |`m > c` |
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Why is this bad?** If the bits that the comparison cares about are always
|
|
|
|
/// set to zero or one by the bit mask, the comparison is constant `true` or
|
|
|
|
/// `false` (depending on mask, compared value, and operators).
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// So the code is actively misleading, and the only reason someone would write
|
|
|
|
/// this intentionally is to win an underhanded Rust contest or create a
|
|
|
|
/// test-case for this lint.
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Known problems:** None.
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// if (x & 1 == 2) { … }
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2015-04-30 04:48:43 -05:00
|
|
|
pub BAD_BIT_MASK,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2016-08-06 03:18:36 -05:00
|
|
|
"expressions of the form `_ & mask == select` that will only ever return `true` or `false`"
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for bit masks in comparisons which can be removed
|
|
|
|
/// without changing the outcome. The basic structure can be seen in the
|
|
|
|
/// following table:
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-03-19 11:48:29 -05:00
|
|
|
/// |Comparison| Bit Op |Example |equals |
|
2015-12-10 18:22:27 -06:00
|
|
|
/// |----------|---------|-----------|-------|
|
|
|
|
/// |`>` / `<=`|`|` / `^`|`x | 2 > 3`|`x > 3`|
|
|
|
|
/// |`<` / `>=`|`|` / `^`|`x ^ 1 < 4`|`x < 4`|
|
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Why is this bad?** Not equally evil as [`bad_bit_mask`](#bad_bit_mask),
|
|
|
|
/// but still a bit misleading, because the bit mask is ineffective.
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Known problems:** False negatives: This lint will only match instances
|
|
|
|
/// where we have figured out the math (which is for a power-of-two compared
|
|
|
|
/// value). This means things like `x | 1 >= 7` (which would be better written
|
|
|
|
/// as `x >= 6`) will not be reported (but bit masks like this are fairly
|
|
|
|
/// uncommon).
|
2015-12-10 18:22:27 -06:00
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// if (x | 1 > 3) { … }
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2015-08-11 13:22:20 -05:00
|
|
|
pub INEFFECTIVE_BIT_MASK,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2015-08-13 03:32:35 -05:00
|
|
|
"expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
|
2015-05-15 07:09:29 -05:00
|
|
|
}
|
|
|
|
|
2017-07-03 09:07:04 -05:00
|
|
|
/// **What it does:** Checks for bit masks that can be replaced by a call
|
|
|
|
/// to `trailing_zeros`
|
|
|
|
///
|
2017-08-09 02:30:56 -05:00
|
|
|
/// **Why is this bad?** `x.trailing_zeros() > 4` is much clearer than `x & 15
|
|
|
|
/// == 0`
|
2017-07-03 09:07:04 -05:00
|
|
|
///
|
2017-07-10 02:50:36 -05:00
|
|
|
/// **Known problems:** llvm generates better code for `x & 15 == 0` on x86
|
2017-07-03 09:07:04 -05:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// x & 0x1111 == 0
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2017-07-03 09:07:04 -05:00
|
|
|
pub VERBOSE_BIT_MASK,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2017-07-03 09:07:04 -05:00
|
|
|
"expressions where a bit mask is less readable than the corresponding method call"
|
|
|
|
}
|
|
|
|
|
2017-08-09 02:30:56 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2017-09-25 15:38:49 -05:00
|
|
|
pub struct BitMask {
|
|
|
|
verbose_bit_mask_threshold: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BitMask {
|
|
|
|
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
|
|
|
|
Self {
|
2018-03-15 10:07:15 -05:00
|
|
|
verbose_bit_mask_threshold,
|
2017-09-25 15:38:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
|
|
|
|
impl LintPass for BitMask {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2017-07-03 09:07:04 -05:00
|
|
|
lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK, VERBOSE_BIT_MASK)
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
2015-09-18 21:53:04 -05:00
|
|
|
}
|
2015-08-11 13:22:20 -05:00
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
|
2016-04-07 10:46:48 -05:00
|
|
|
if cmp.node.is_comparison() {
|
2016-06-05 18:16:16 -05:00
|
|
|
if let Some(cmp_opt) = fetch_int_literal(cx, right) {
|
2018-05-31 13:15:48 -05:00
|
|
|
check_compare(cx, left, cmp.node, cmp_opt, e.span)
|
2016-06-05 18:16:16 -05:00
|
|
|
} else if let Some(cmp_val) = fetch_int_literal(cx, left) {
|
2018-05-31 13:15:48 -05:00
|
|
|
check_compare(cx, right, invert_cmp(cmp.node), cmp_val, e.span)
|
2016-06-05 18:16:16 -05:00
|
|
|
}
|
2015-08-11 13:22:20 -05:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
|
2018-07-12 02:50:09 -05:00
|
|
|
if BinOpKind::Eq == op.node;
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node;
|
2018-07-12 02:50:09 -05:00
|
|
|
if BinOpKind::BitAnd == op1.node;
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Lit(ref lit) = right1.node;
|
2017-10-23 14:18:02 -05:00
|
|
|
if let LitKind::Int(n, _) = lit.node;
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Lit(ref lit1) = right.node;
|
2017-10-23 14:18:02 -05:00
|
|
|
if let LitKind::Int(0, _) = lit1.node;
|
|
|
|
if n.leading_zeros() == n.count_zeros();
|
|
|
|
if n > u128::from(self.verbose_bit_mask_threshold);
|
|
|
|
then {
|
|
|
|
span_lint_and_then(cx,
|
|
|
|
VERBOSE_BIT_MASK,
|
|
|
|
e.span,
|
|
|
|
"bit mask could be simplified with a call to `trailing_zeros`",
|
|
|
|
|db| {
|
|
|
|
let sugg = Sugg::hir(cx, left1, "...").maybe_par();
|
|
|
|
db.span_suggestion(e.span, "try", format!("{}.trailing_zeros() >= {}", sugg, n.count_ones()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 02:50:09 -05:00
|
|
|
fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
|
2015-08-11 13:22:20 -05:00
|
|
|
match cmp {
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::Eq => BinOpKind::Eq,
|
|
|
|
BinOpKind::Ne => BinOpKind::Ne,
|
|
|
|
BinOpKind::Lt => BinOpKind::Gt,
|
|
|
|
BinOpKind::Gt => BinOpKind::Lt,
|
|
|
|
BinOpKind::Le => BinOpKind::Ge,
|
|
|
|
BinOpKind::Ge => BinOpKind::Le,
|
|
|
|
_ => BinOpKind::Or, // Dummy
|
2015-08-11 13:22:20 -05:00
|
|
|
}
|
2015-05-04 00:17:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-12 02:50:09 -05:00
|
|
|
fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Binary(ref op, ref left, ref right) = bit_op.node {
|
2018-07-12 02:50:09 -05:00
|
|
|
if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
|
2015-09-18 22:02:56 -05:00
|
|
|
return;
|
|
|
|
}
|
2016-01-03 22:26:12 -06:00
|
|
|
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-08-11 13:22:20 -05:00
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 02:50:09 -05:00
|
|
|
fn check_bit_mask(cx: &LateContext, bit_op: BinOpKind, cmp_op: BinOpKind, mask_value: u128, cmp_value: u128, span: Span) {
|
2015-08-11 13:22:20 -05:00
|
|
|
match cmp_op {
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::Eq | BinOpKind::Ne => match bit_op {
|
|
|
|
BinOpKind::BitAnd => if mask_value & cmp_value != cmp_value {
|
2017-09-05 04:33:04 -05:00
|
|
|
if cmp_value != 0 {
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
BAD_BIT_MASK,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
&format!(
|
|
|
|
"incompatible bit mask: `_ & {}` can never be equal to `{}`",
|
|
|
|
mask_value,
|
|
|
|
cmp_value
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if mask_value == 0 {
|
2018-05-31 13:15:48 -05:00
|
|
|
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::BitOr => if mask_value | cmp_value != cmp_value {
|
2017-09-05 04:33:04 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
BAD_BIT_MASK,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
&format!(
|
|
|
|
"incompatible bit mask: `_ | {}` can never be equal to `{}`",
|
|
|
|
mask_value,
|
|
|
|
cmp_value
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
_ => (),
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::Lt | BinOpKind::Ge => match bit_op {
|
|
|
|
BinOpKind::BitAnd => if mask_value < cmp_value {
|
2017-09-05 04:33:04 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
BAD_BIT_MASK,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
&format!(
|
|
|
|
"incompatible bit mask: `_ & {}` will always be lower than `{}`",
|
|
|
|
mask_value,
|
|
|
|
cmp_value
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else if mask_value == 0 {
|
2018-05-31 13:15:48 -05:00
|
|
|
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::BitOr => if mask_value >= cmp_value {
|
2017-09-05 04:33:04 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
BAD_BIT_MASK,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
&format!(
|
|
|
|
"incompatible bit mask: `_ | {}` will never be lower than `{}`",
|
|
|
|
mask_value,
|
|
|
|
cmp_value
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
2018-05-31 13:15:48 -05:00
|
|
|
check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
|
2017-09-05 04:33:04 -05:00
|
|
|
_ => (),
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::Le | BinOpKind::Gt => match bit_op {
|
|
|
|
BinOpKind::BitAnd => if mask_value <= cmp_value {
|
2017-09-05 04:33:04 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
BAD_BIT_MASK,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
&format!(
|
|
|
|
"incompatible bit mask: `_ & {}` will never be higher than `{}`",
|
|
|
|
mask_value,
|
|
|
|
cmp_value
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else if mask_value == 0 {
|
2018-05-31 13:15:48 -05:00
|
|
|
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::BitOr => if mask_value > cmp_value {
|
2017-09-05 04:33:04 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
BAD_BIT_MASK,
|
2018-05-31 13:15:48 -05:00
|
|
|
span,
|
2017-09-05 04:33:04 -05:00
|
|
|
&format!(
|
|
|
|
"incompatible bit mask: `_ | {}` will always be higher than `{}`",
|
|
|
|
mask_value,
|
|
|
|
cmp_value
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
2018-05-31 13:15:48 -05:00
|
|
|
check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
|
2017-09-05 04:33:04 -05:00
|
|
|
},
|
2018-07-12 02:50:09 -05:00
|
|
|
BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
|
2017-09-05 04:33:04 -05:00
|
|
|
_ => (),
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-01-03 22:26:12 -06:00
|
|
|
_ => (),
|
2015-08-11 13:22:20 -05:00
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|
|
|
|
|
2017-01-04 11:05:33 -06:00
|
|
|
fn check_ineffective_lt(cx: &LateContext, span: Span, m: u128, c: u128, op: &str) {
|
2015-08-19 02:07:50 -05:00
|
|
|
if c.is_power_of_two() && m < c {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INEFFECTIVE_BIT_MASK,
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
|
|
|
|
op,
|
|
|
|
m,
|
|
|
|
c
|
|
|
|
),
|
|
|
|
);
|
2015-08-19 02:07:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 11:05:33 -06:00
|
|
|
fn check_ineffective_gt(cx: &LateContext, span: Span, m: u128, c: u128, op: &str) {
|
2015-08-19 02:07:50 -05:00
|
|
|
if (c + 1).is_power_of_two() && m <= c {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
INEFFECTIVE_BIT_MASK,
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"ineffective bit mask: `x {} {}` compared to `{}`, is the same as x compared directly",
|
|
|
|
op,
|
|
|
|
m,
|
|
|
|
c
|
|
|
|
),
|
|
|
|
);
|
2015-08-19 02:07:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 11:05:33 -06:00
|
|
|
fn fetch_int_literal(cx: &LateContext, lit: &Expr) -> Option<u128> {
|
2018-05-13 06:16:31 -05:00
|
|
|
match constant(cx, cx.tables, lit)?.0 {
|
2018-03-13 05:38:11 -05:00
|
|
|
Constant::Int(n) => Some(n),
|
2016-01-03 22:26:12 -06:00
|
|
|
_ => None,
|
2015-08-11 13:22:20 -05:00
|
|
|
}
|
2015-04-30 04:48:43 -05:00
|
|
|
}
|