diff options
| author | clubby789 <jamie@hill-daniel.co.uk> | 2023-05-21 23:06:06 +0000 |
|---|---|---|
| committer | clubby789 <jamie@hill-daniel.co.uk> | 2023-05-25 17:08:49 +0000 |
| commit | ace794c6d78ca2d1a8740b07f5e7f81fc09b4a02 (patch) | |
| tree | d8fb21fcde38401658bb757025a4a8989f6e2ed6 /compiler | |
| parent | 9d871b0617a4b3d6610b7cee0ab5310dcb542c62 (diff) | |
| download | rust-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.rs | 19 |
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 |
