Limit the identity_op lint to integral operands.

If operands are being applied to non-integers, then the lint is likely
wrong.
This commit is contained in:
Alex Ozdemir 2021-12-27 16:06:27 -08:00
parent adba132411
commit ba70842c62

View file

@ -61,10 +61,13 @@ impl<'tcx> LateLintPass<'tcx> for IdentityOp {
}
fn is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_>) -> bool {
// `1 << 0` is a common pattern in bit manipulation code
cmp.node == BinOpKind::Shl
&& constant_simple(cx, cx.typeck_results(), right) == Some(Constant::Int(0))
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1))
// This lint applies to integers
!cx.typeck_results().expr_ty(left).is_integral()
|| !cx.typeck_results().expr_ty(right).is_integral()
// `1 << 0` is a common pattern in bit manipulation code
|| (cmp.node == BinOpKind::Shl
&& constant_simple(cx, cx.typeck_results(), right) == Some(Constant::Int(0))
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1)))
}
fn check(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span) {