about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-10-12 18:49:34 +0000
committerbors <bors@rust-lang.org>2022-10-12 18:49:34 +0000
commit45dd9f31f3b65c871dd4f494643fac472678348f (patch)
treedd55ea7e399634e378bd55a4927047f23f51f61b
parentb8a9a507bf9e3149d287841454842116c72d66c4 (diff)
parent0cc749296f9e932550ee6378946b53d54827e2a1 (diff)
downloadrust-45dd9f31f3b65c871dd4f494643fac472678348f.tar.gz
rust-45dd9f31f3b65c871dd4f494643fac472678348f.zip
Auto merge of #9627 - Jarcho:ice-9625, r=xFrednet
Use the correct type when comparing nested constants.

fixes #9625

changelog: `manual_range_contains`: fix ICE when the values are behind a reference
-rw-r--r--clippy_utils/src/consts.rs44
-rw-r--r--tests/ui/crashes/ice-9625.rs4
2 files changed, 42 insertions, 6 deletions
diff --git a/clippy_utils/src/consts.rs b/clippy_utils/src/consts.rs
index fa6766f7cfe..07e4ef6a2fe 100644
--- a/clippy_utils/src/consts.rs
+++ b/clippy_utils/src/consts.rs
@@ -136,17 +136,49 @@ impl Constant {
             (&Self::F64(l), &Self::F64(r)) => l.partial_cmp(&r),
             (&Self::F32(l), &Self::F32(r)) => l.partial_cmp(&r),
             (&Self::Bool(ref l), &Self::Bool(ref r)) => Some(l.cmp(r)),
-            (&Self::Tuple(ref l), &Self::Tuple(ref r)) | (&Self::Vec(ref l), &Self::Vec(ref r)) => iter::zip(l, r)
-                .map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
-                .find(|r| r.map_or(true, |o| o != Ordering::Equal))
-                .unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
+            (&Self::Tuple(ref l), &Self::Tuple(ref r)) if l.len() == r.len() => match *cmp_type.kind() {
+                ty::Tuple(tys) if tys.len() == l.len() => l
+                    .iter()
+                    .zip(r)
+                    .zip(tys)
+                    .map(|((li, ri), cmp_type)| Self::partial_cmp(tcx, cmp_type, li, ri))
+                    .find(|r| r.map_or(true, |o| o != Ordering::Equal))
+                    .unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
+                _ => None,
+            },
+            (&Self::Vec(ref l), &Self::Vec(ref r)) => {
+                let cmp_type = match *cmp_type.kind() {
+                    ty::Array(ty, _) | ty::Slice(ty) => ty,
+                    _ => return None,
+                };
+                iter::zip(l, r)
+                    .map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
+                    .find(|r| r.map_or(true, |o| o != Ordering::Equal))
+                    .unwrap_or_else(|| Some(l.len().cmp(&r.len())))
+            },
             (&Self::Repeat(ref lv, ref ls), &Self::Repeat(ref rv, ref rs)) => {
-                match Self::partial_cmp(tcx, cmp_type, lv, rv) {
+                match Self::partial_cmp(
+                    tcx,
+                    match *cmp_type.kind() {
+                        ty::Array(ty, _) => ty,
+                        _ => return None,
+                    },
+                    lv,
+                    rv,
+                ) {
                     Some(Equal) => Some(ls.cmp(rs)),
                     x => x,
                 }
             },
-            (&Self::Ref(ref lb), &Self::Ref(ref rb)) => Self::partial_cmp(tcx, cmp_type, lb, rb),
+            (&Self::Ref(ref lb), &Self::Ref(ref rb)) => Self::partial_cmp(
+                tcx,
+                match *cmp_type.kind() {
+                    ty::Ref(_, ty, _) => ty,
+                    _ => return None,
+                },
+                lb,
+                rb,
+            ),
             // TODO: are there any useful inter-type orderings?
             _ => None,
         }
diff --git a/tests/ui/crashes/ice-9625.rs b/tests/ui/crashes/ice-9625.rs
new file mode 100644
index 00000000000..a765882b5d8
--- /dev/null
+++ b/tests/ui/crashes/ice-9625.rs
@@ -0,0 +1,4 @@
+fn main() {
+    let x = &1;
+    let _ = &1 < x && x < &10;
+}