about summary refs log tree commit diff
path: root/clippy_lints/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-12-28 22:01:57 +0000
committerbors <bors@rust-lang.org>2021-12-28 22:01:57 +0000
commit16ef044e72ef8db08307c4fdaa1c0dd8b24d0861 (patch)
tree2c52aa6c38a3aacef70e5a55f4cb70ee8e4d9b31 /clippy_lints/src
parenta139949eadb99bb34ca1ac30965b498147dec773 (diff)
parentee6d5c5cdae8b7cb3198800a615855169fc0b3de (diff)
downloadrust-16ef044e72ef8db08307c4fdaa1c0dd8b24d0861.tar.gz
rust-16ef044e72ef8db08307c4fdaa1c0dd8b24d0861.zip
Auto merge of #8183 - alex-ozdemir:limit-ident, r=camsteffen
Limit the ``[`identity_op`]`` lint to integral operands.

changelog: limit ``[`identity_op`]`` to integral operands

In the ``[`identity_op`]`` lint, if the operands are non-integers, then the lint is likely
wrong.
Diffstat (limited to 'clippy_lints/src')
-rw-r--r--clippy_lints/src/identity_op.rs15
1 files changed, 9 insertions, 6 deletions
diff --git a/clippy_lints/src/identity_op.rs b/clippy_lints/src/identity_op.rs
index b4e7bbc7671..f824f20ca40 100644
--- a/clippy_lints/src/identity_op.rs
+++ b/clippy_lints/src/identity_op.rs
@@ -61,15 +61,18 @@ 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).peel_refs().is_integral()
+        || !cx.typeck_results().expr_ty(right).peel_refs().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) {
-    if let Some(Constant::Int(v)) = constant_simple(cx, cx.typeck_results(), e) {
-        let check = match *cx.typeck_results().expr_ty(e).kind() {
+    if let Some(Constant::Int(v)) = constant_simple(cx, cx.typeck_results(), e).map(Constant::peel_refs) {
+        let check = match *cx.typeck_results().expr_ty(e).peel_refs().kind() {
             ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
             ty::Uint(uty) => clip(cx.tcx, !0, uty),
             _ => return,