style: linting IsNil

This commit is contained in:
Samuel Berthe
2023-12-02 15:14:23 +01:00
parent ad7eae34cc
commit 21395c58ff
2 changed files with 16 additions and 16 deletions
+6 -6
View File
@@ -2,17 +2,17 @@ package lo
import "reflect"
// IsNil checks if a value is nil or if it's a reference type with a nil underlying value.
func IsNil(x any) bool {
defer func() { recover() }() // nolint:errcheck
return x == nil || reflect.ValueOf(x).IsNil()
}
// ToPtr returns a pointer copy of value.
func ToPtr[T any](x T) *T {
return &x
}
// IsNil checks if a value is nil or if it's a reference type with a nil underlying value.
func IsNil(x any) bool {
defer func() { recover() }()
return x == nil || reflect.ValueOf(x).IsNil()
}
// EmptyableToPtr returns a pointer copy of value if it's nonzero.
// Otherwise, returns nil pointer.
func EmptyableToPtr[T any](x T) *T {
+10 -10
View File
@@ -6,15 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)
func TestToPtr(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := ToPtr([]int{1, 2})
is.Equal(*result1, []int{1, 2})
}
func TestIsNil(t *testing.T) {
t.Parallel()
is := assert.New(t)
@@ -36,7 +27,16 @@ func TestIsNil(t *testing.T) {
var ifaceWithNilValue interface{} = (*string)(nil)
is.True(IsNil(ifaceWithNilValue))
is.True(ifaceWithNilValue != nil)
is.False(ifaceWithNilValue == nil) // nolint:staticcheck
}
func TestToPtr(t *testing.T) {
t.Parallel()
is := assert.New(t)
result1 := ToPtr([]int{1, 2})
is.Equal(*result1, []int{1, 2})
}
func TestEmptyableToPtr(t *testing.T) {