about summary refs log tree commit diff
path: root/clippy_utils
diff options
context:
space:
mode:
authorThibsG <thibsg@pm.me>2021-05-13 21:40:20 +0200
committerThibsG <thibsg@pm.me>2021-05-17 17:27:16 +0200
commit2fb35ce4f0ea8d33bbe207c8a1c8822ebb90c813 (patch)
treefbde407f144d91cc91078d431005a526190479da /clippy_utils
parentaa15a5442a975180a367373e563b7f8c626b5344 (diff)
downloadrust-2fb35ce4f0ea8d33bbe207c8a1c8822ebb90c813.tar.gz
rust-2fb35ce4f0ea8d33bbe207c8a1c8822ebb90c813.zip
Add generic args for comparison in `use_self` and `useless_conversion` lints
Diffstat (limited to 'clippy_utils')
-rw-r--r--clippy_utils/src/ty.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs
index 64a80f2554f..e1f8aff3740 100644
--- a/clippy_utils/src/ty.rs
+++ b/clippy_utils/src/ty.rs
@@ -322,3 +322,27 @@ pub fn walk_ptrs_ty_depth(ty: Ty<'_>) -> (Ty<'_>, usize) {
     }
     inner(ty, 0)
 }
+
+/// Returns `true` if types `a` and `b` are same types having same `Const` generic args,
+/// otherwise returns `false`
+pub fn same_type_and_consts(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
+    match (&a.kind(), &b.kind()) {
+        (&ty::Adt(did_a, substs_a), &ty::Adt(did_b, substs_b)) => {
+            if did_a != did_b {
+                return false;
+            }
+
+            substs_a
+                .iter()
+                .zip(substs_b.iter())
+                .all(|(arg_a, arg_b)| match (arg_a.unpack(), arg_b.unpack()) {
+                    (GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b,
+                    (GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => {
+                        same_type_and_consts(type_a, type_b)
+                    },
+                    _ => true,
+                })
+        },
+        _ => a == b,
+    }
+}