174 lines
5.9 KiB
Rust
Raw Normal View History

use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint;
2020-02-21 09:39:38 +01:00
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_span::source_map::Span;
use super::{BAD_BIT_MASK, INEFFECTIVE_BIT_MASK};
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
e: &'tcx Expr<'_>,
op: BinOpKind,
left: &'tcx Expr<'_>,
right: &'tcx Expr<'_>,
) {
if op.is_comparison() {
if let Some(cmp_opt) = fetch_int_literal(cx, right) {
check_compare(cx, left, op, cmp_opt, e.span);
} else if let Some(cmp_val) = fetch_int_literal(cx, left) {
check_compare(cx, right, invert_cmp(op), cmp_val, e.span);
}
}
}
#[must_use]
2018-07-12 15:50:09 +08:00
fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
match cmp {
2018-07-12 15:50:09 +08: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
}
}
fn check_compare(cx: &LateContext<'_>, bit_op: &Expr<'_>, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
2019-09-27 17:16:06 +02:00
if let ExprKind::Binary(op, left, right) = &bit_op.kind {
2018-07-12 15:50:09 +08:00
if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
2015-09-19 08:32:56 +05:30
return;
}
2016-01-04 09:56:12 +05:30
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));
}
}
2019-01-13 10:19:02 -05:00
#[allow(clippy::too_many_lines)]
2018-11-27 21:14:15 +01:00
fn check_bit_mask(
cx: &LateContext<'_>,
2018-11-27 21:14:15 +01:00
bit_op: BinOpKind,
cmp_op: BinOpKind,
mask_value: u128,
cmp_value: u128,
span: Span,
) {
match cmp_op {
2018-07-12 15:50:09 +08:00
BinOpKind::Eq | BinOpKind::Ne => match bit_op {
2018-11-27 21:14:15 +01:00
BinOpKind::BitAnd => {
if mask_value & cmp_value != cmp_value {
if cmp_value != 0 {
span_lint(
cx,
BAD_BIT_MASK,
span,
&format!("incompatible bit mask: `_ & {mask_value}` can never be equal to `{cmp_value}`"),
2018-11-27 21:14:15 +01:00
);
}
} else if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
}
},
BinOpKind::BitOr => {
if mask_value | cmp_value != cmp_value {
2017-09-05 11:33:04 +02:00
span_lint(
cx,
BAD_BIT_MASK,
span,
&format!("incompatible bit mask: `_ | {mask_value}` can never be equal to `{cmp_value}`"),
2017-09-05 11:33:04 +02:00
);
}
},
_ => (),
2016-12-20 18:21:30 +01:00
},
2018-07-12 15:50:09 +08:00
BinOpKind::Lt | BinOpKind::Ge => match bit_op {
2018-11-27 21:14:15 +01:00
BinOpKind::BitAnd => {
if mask_value < cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
span,
&format!("incompatible bit mask: `_ & {mask_value}` will always be lower than `{cmp_value}`"),
2018-11-27 21:14:15 +01:00
);
} else if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
}
2017-09-05 11:33:04 +02:00
},
2018-11-27 21:14:15 +01:00
BinOpKind::BitOr => {
if mask_value >= cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
span,
&format!("incompatible bit mask: `_ | {mask_value}` will never be lower than `{cmp_value}`"),
2018-11-27 21:14:15 +01:00
);
} else {
check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
}
2017-09-05 11:33:04 +02:00
},
2018-07-12 15:50:09 +08:00
BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
2017-09-05 11:33:04 +02:00
_ => (),
2016-12-20 18:21:30 +01:00
},
2018-07-12 15:50:09 +08:00
BinOpKind::Le | BinOpKind::Gt => match bit_op {
2018-11-27 21:14:15 +01:00
BinOpKind::BitAnd => {
if mask_value <= cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
span,
&format!("incompatible bit mask: `_ & {mask_value}` will never be higher than `{cmp_value}`"),
2018-11-27 21:14:15 +01:00
);
} else if mask_value == 0 {
span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
}
2017-09-05 11:33:04 +02:00
},
2018-11-27 21:14:15 +01:00
BinOpKind::BitOr => {
if mask_value > cmp_value {
span_lint(
cx,
BAD_BIT_MASK,
span,
&format!("incompatible bit mask: `_ | {mask_value}` will always be higher than `{cmp_value}`"),
2018-11-27 21:14:15 +01:00
);
} else {
check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
}
2017-09-05 11:33:04 +02:00
},
2018-07-12 15:50:09 +08:00
BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
2017-09-05 11:33:04 +02:00
_ => (),
2016-12-20 18:21:30 +01:00
},
2016-01-04 09:56:12 +05:30
_ => (),
}
}
fn check_ineffective_lt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) {
2015-08-19 09:07:50 +02:00
if c.is_power_of_two() && m < c {
2017-08-09 09:30:56 +02:00
span_lint(
cx,
INEFFECTIVE_BIT_MASK,
span,
&format!("ineffective bit mask: `x {op} {m}` compared to `{c}`, is the same as x compared directly"),
2017-08-09 09:30:56 +02:00
);
2015-08-19 09:07:50 +02:00
}
}
fn check_ineffective_gt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) {
2015-08-19 09:07:50 +02:00
if (c + 1).is_power_of_two() && m <= c {
2017-08-09 09:30:56 +02:00
span_lint(
cx,
INEFFECTIVE_BIT_MASK,
span,
&format!("ineffective bit mask: `x {op} {m}` compared to `{c}`, is the same as x compared directly"),
2017-08-09 09:30:56 +02:00
);
2015-08-19 09:07:50 +02:00
}
}
fn fetch_int_literal(cx: &LateContext<'_>, lit: &Expr<'_>) -> Option<u128> {
match constant(cx, cx.typeck_results(), lit)? {
2018-03-13 11:38:11 +01:00
Constant::Int(n) => Some(n),
2016-01-04 09:56:12 +05:30
_ => None,
}
}