From: Brad Fitzpatrick Date: Sat, 19 Aug 2023 16:08:38 +0000 (-0700) Subject: slices: simplify Clone a bit X-Git-Tag: go1.22rc1~1192 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b581e447394b4ba7a08ea64b214781cae0f4ef6c;p=gostls13.git 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 --- 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.