Skip to content

Commit ba70842

Browse files
committed
Limit the identity_op lint to integral operands.
If operands are being applied to non-integers, then the lint is likely wrong.
1 parent adba132 commit ba70842

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

clippy_lints/src/identity_op.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ impl<'tcx> LateLintPass<'tcx> for IdentityOp {
6161
}
6262

6363
fn is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_>) -> bool {
64-
// `1 << 0` is a common pattern in bit manipulation code
65-
cmp.node == BinOpKind::Shl
66-
&& constant_simple(cx, cx.typeck_results(), right) == Some(Constant::Int(0))
67-
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1))
64+
// This lint applies to integers
65+
!cx.typeck_results().expr_ty(left).is_integral()
66+
|| !cx.typeck_results().expr_ty(right).is_integral()
67+
// `1 << 0` is a common pattern in bit manipulation code
68+
|| (cmp.node == BinOpKind::Shl
69+
&& constant_simple(cx, cx.typeck_results(), right) == Some(Constant::Int(0))
70+
&& constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1)))
6871
}
6972

7073
fn check(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span) {

0 commit comments

Comments
 (0)