about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorclubby789 <jamie@hill-daniel.co.uk>2023-05-21 23:06:06 +0000
committerclubby789 <jamie@hill-daniel.co.uk>2023-05-25 17:08:49 +0000
commitace794c6d78ca2d1a8740b07f5e7f81fc09b4a02 (patch)
treed8fb21fcde38401658bb757025a4a8989f6e2ed6 /compiler
parent9d871b0617a4b3d6610b7cee0ab5310dcb542c62 (diff)
downloadrust-ace794c6d78ca2d1a8740b07f5e7f81fc09b4a02.tar.gz
rust-ace794c6d78ca2d1a8740b07f5e7f81fc09b4a02.zip
Always capture slice when pattern requires checking the length
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir_typeck/src/expr_use_visitor.rs19
1 files changed, 13 insertions, 6 deletions
diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs
index 94b6a0f8f47..e14e8ac2ce0 100644
--- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs
+++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs
@@ -438,12 +438,19 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
                         // to borrow discr.
                         needs_to_be_read = true;
                     }
-                    PatKind::Or(_)
-                    | PatKind::Box(_)
-                    | PatKind::Slice(..)
-                    | PatKind::Ref(..)
-                    | PatKind::Wild => {
-                        // If the PatKind is Or, Box, Slice or Ref, the decision is made later
+                    PatKind::Slice(lhs, wild, rhs) => {
+                        // We don't need to test the length if the pattern is `[..]`
+                        if matches!((lhs, wild, rhs), (&[], Some(_), &[]))
+                            // Arrays have a statically known size, so
+                            // there is no need to read their length
+                            || discr_place.place.base_ty.is_array()
+                        {
+                        } else {
+                            needs_to_be_read = true;
+                        }
+                    }
+                    PatKind::Or(_) | PatKind::Box(_) | PatKind::Ref(..) | PatKind::Wild => {
+                        // If the PatKind is Or, Box, or Ref, the decision is made later
                         // as these patterns contains subpatterns
                         // If the PatKind is Wild, the decision is made based on the other patterns being
                         // examined