use rustc::hir::*;
-use rustc::hir::def::Def;
use rustc::lint::*;
-use rustc_const_eval::lookup_const_by_id;
+use rustc::{declare_lint, lint_array};
+use if_chain::if_chain;
use syntax::ast::LitKind;
use syntax::codemap::Span;
-use utils::{span_lint, span_lint_and_then};
-use utils::sugg::Sugg;
+use crate::utils::{span_lint, span_lint_and_then};
+use crate::utils::sugg::Sugg;
+use crate::consts::{constant, Constant};
/// **What it does:** Checks for incompatible bit masks in comparisons.
///
/// ```rust
/// if (x & 1 == 2) { … }
/// ```
-declare_lint! {
+declare_clippy_lint! {
pub BAD_BIT_MASK,
- Warn,
+ correctness,
"expressions of the form `_ & mask == select` that will only ever return `true` or `false`"
}
/// ```rust
/// if (x | 1 > 3) { … }
/// ```
-declare_lint! {
+declare_clippy_lint! {
pub INEFFECTIVE_BIT_MASK,
- Warn,
+ correctness,
"expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
}
/// ```rust
/// x & 0x1111 == 0
/// ```
-declare_lint! {
+declare_clippy_lint! {
pub VERBOSE_BIT_MASK,
- Warn,
+ style,
"expressions where a bit mask is less readable than the corresponding method call"
}
#[derive(Copy, Clone)]
-pub struct BitMask;
+pub struct BitMask {
+ verbose_bit_mask_threshold: u64,
+}
+
+impl BitMask {
+ pub fn new(verbose_bit_mask_threshold: u64) -> Self {
+ Self {
+ verbose_bit_mask_threshold,
+ }
+ }
+}
impl LintPass for BitMask {
fn get_lints(&self) -> LintArray {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
- if let ExprBinary(ref cmp, ref left, ref right) = e.node {
+ if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
if cmp.node.is_comparison() {
if let Some(cmp_opt) = fetch_int_literal(cx, right) {
- check_compare(cx, left, cmp.node, cmp_opt, &e.span)
+ check_compare(cx, left, cmp.node, cmp_opt, e.span)
} else if let Some(cmp_val) = fetch_int_literal(cx, left) {
- check_compare(cx, right, invert_cmp(cmp.node), cmp_val, &e.span)
+ check_compare(cx, right, invert_cmp(cmp.node), cmp_val, e.span)
}
}
}
- if_let_chain!{[
- let Expr_::ExprBinary(ref op, ref left, ref right) = e.node,
- BinOp_::BiEq == op.node,
- let Expr_::ExprBinary(ref op1, ref left1, ref right1) = left.node,
- BinOp_::BiBitAnd == op1.node,
- let Expr_::ExprLit(ref lit) = right1.node,
- let LitKind::Int(n, _) = lit.node,
- let Expr_::ExprLit(ref lit1) = right.node,
- let LitKind::Int(0, _) = lit1.node,
- n.leading_zeros() == n.count_zeros(),
- ], {
- 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()));
- });
- }}
+ if_chain! {
+ if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
+ if BinOpKind::Eq == op.node;
+ if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node;
+ if BinOpKind::BitAnd == op1.node;
+ if let ExprKind::Lit(ref lit) = right1.node;
+ if let LitKind::Int(n, _) = lit.node;
+ if let ExprKind::Lit(ref lit1) = right.node;
+ 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()));
+ });
+ }
+ }
}
}
-fn invert_cmp(cmp: BinOp_) -> BinOp_ {
+fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
match cmp {
- BiEq => BiEq,
- BiNe => BiNe,
- BiLt => BiGt,
- BiGt => BiLt,
- BiLe => BiGe,
- BiGe => BiLe,
- _ => BiOr, // Dummy
+ 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
}
}
-fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u128, span: &Span) {
- if let ExprBinary(ref op, ref left, ref right) = bit_op.node {
- if op.node != BiBitAnd && op.node != BiBitOr {
+fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
+ if let ExprKind::Binary(ref op, ref left, ref right) = bit_op.node {
+ if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
return;
}
fetch_int_literal(cx, right)
}
}
-fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u128, cmp_value: u128, span: &Span) {
+fn check_bit_mask(cx: &LateContext, bit_op: BinOpKind, cmp_op: BinOpKind, mask_value: u128, cmp_value: u128, span: Span) {
match cmp_op {
- BiEq | BiNe => match bit_op {
- BiBitAnd => if mask_value & cmp_value != cmp_value {
+ BinOpKind::Eq | BinOpKind::Ne => match bit_op {
+ BinOpKind::BitAnd => if mask_value & cmp_value != cmp_value {
if cmp_value != 0 {
span_lint(
cx,
BAD_BIT_MASK,
- *span,
+ span,
&format!(
"incompatible bit mask: `_ & {}` can never be equal to `{}`",
mask_value,
);
}
} else if mask_value == 0 {
- span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
+ span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
},
- BiBitOr => if mask_value | cmp_value != cmp_value {
+ BinOpKind::BitOr => if mask_value | cmp_value != cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
- *span,
+ span,
&format!(
"incompatible bit mask: `_ | {}` can never be equal to `{}`",
mask_value,
},
_ => (),
},
- BiLt | BiGe => match bit_op {
- BiBitAnd => if mask_value < cmp_value {
+ BinOpKind::Lt | BinOpKind::Ge => match bit_op {
+ BinOpKind::BitAnd => if mask_value < cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
- *span,
+ span,
&format!(
"incompatible bit mask: `_ & {}` will always be lower than `{}`",
mask_value,
),
);
} else if mask_value == 0 {
- span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
+ span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
},
- BiBitOr => if mask_value >= cmp_value {
+ BinOpKind::BitOr => if mask_value >= cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
- *span,
+ span,
&format!(
"incompatible bit mask: `_ | {}` will never be lower than `{}`",
mask_value,
),
);
} else {
- check_ineffective_lt(cx, *span, mask_value, cmp_value, "|");
+ check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
},
- BiBitXor => check_ineffective_lt(cx, *span, mask_value, cmp_value, "^"),
+ BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
_ => (),
},
- BiLe | BiGt => match bit_op {
- BiBitAnd => if mask_value <= cmp_value {
+ BinOpKind::Le | BinOpKind::Gt => match bit_op {
+ BinOpKind::BitAnd => if mask_value <= cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
- *span,
+ span,
&format!(
"incompatible bit mask: `_ & {}` will never be higher than `{}`",
mask_value,
),
);
} else if mask_value == 0 {
- span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
+ span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
},
- BiBitOr => if mask_value > cmp_value {
+ BinOpKind::BitOr => if mask_value > cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
- *span,
+ span,
&format!(
"incompatible bit mask: `_ | {}` will always be higher than `{}`",
mask_value,
),
);
} else {
- check_ineffective_gt(cx, *span, mask_value, cmp_value, "|");
+ check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
},
- BiBitXor => check_ineffective_gt(cx, *span, mask_value, cmp_value, "^"),
+ BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
_ => (),
},
_ => (),
}
fn fetch_int_literal(cx: &LateContext, lit: &Expr) -> Option<u128> {
- use rustc::ty::subst::Substs;
- match lit.node {
- ExprLit(ref lit_ptr) => {
- if let LitKind::Int(value, _) = lit_ptr.node {
- Some(value) // TODO: Handle sign
- } else {
- None
- }
- },
- ExprPath(ref qpath) => {
- let def = cx.tables.qpath_def(qpath, lit.hir_id);
- if let Def::Const(def_id) = def {
- lookup_const_by_id(cx.tcx, cx.param_env.and((def_id, Substs::empty()))).and_then(|(l, _ty)| {
- let body = if let Some(id) = cx.tcx.hir.as_local_node_id(l) {
- cx.tcx.mir_const_qualif(def_id);
- cx.tcx.hir.body(cx.tcx.hir.body_owned_by(id))
- } else {
- cx.tcx.sess.cstore.item_body(cx.tcx, def_id)
- };
- fetch_int_literal(cx, &body.value)
- })
- } else {
- None
- }
- },
+ match constant(cx, cx.tables, lit)?.0 {
+ Constant::Int(n) => Some(n),
_ => None,
}
}