From eb3b1e33d29431c8991f7290559f290871a798f6 Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Thu, 22 Oct 2026 05:55:55 +0300 Subject: [PATCH] fix(slice): return safely when DeleteAt receives no elements (#381) --- slice/slice.go | 3 +++ slice/slice_test.go | 2 ++ 2 files changed, 5 insertions(+) diff --git a/slice/slice.go b/slice/slice.go index 0a8611f..f5ff0e6 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -712,6 +712,9 @@ func IntSlice(slice any) []int { // Play: https://go.dev/play/p/800B1dPBYyd func DeleteAt[T any](slice []T, index int) []T { result := append([]T(nil), slice...) + if len(result) == 0 { + return result + } if index < 0 || index >= len(slice) { return result[:len(slice)-1] diff --git a/slice/slice_test.go b/slice/slice_test.go index e91c9c6..ef6db9c 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -801,6 +801,8 @@ func TestDeleteAt(t *testing.T) { t.Parallel() assert := internal.NewAssert(t, "TestDeleteAt") + assert.Equal(0, len(DeleteAt([]int(nil), 0))) + assert.Equal(0, len(DeleteAt([]int{}, -1))) tests := []struct { slice []int