mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
style: linting IsNil
This commit is contained in:
@@ -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
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user