From b581e447394b4ba7a08ea64b214781cae0f4ef6c Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 19 Aug 2023 09:08:38 -0700 Subject: [PATCH] slices: simplify Clone a bit No need for an explicit nil check. Slicing the input slice down to zero capacity also preserves nil. Change-Id: I1f53cc485373d0e65971cd87b6243650ac72612c Reviewed-on: https://go-review.googlesource.com/c/go/+/521037 Run-TryBot: Brad Fitzpatrick Reviewed-by: Dmitri Shuralyov TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- src/slices/slices.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/slices/slices.go b/src/slices/slices.go index a4d9f7e3f5..252a8eecfc 100644 --- a/src/slices/slices.go +++ b/src/slices/slices.go @@ -333,11 +333,8 @@ func Replace[S ~[]E, E any](s S, i, j int, v ...E) S { // Clone returns a copy of the slice. // The elements are copied using assignment, so this is a shallow clone. func Clone[S ~[]E, E any](s S) S { - // Preserve nil in case it matters. - if s == nil { - return nil - } - return append(S([]E{}), s...) + // The s[:0:0] preserves nil in case it matters. + return append(s[:0:0], s...) } // Compact replaces consecutive runs of equal elements with a single copy. -- 2.50.0