Skip to content

Commit

Permalink
style: linting IsNil
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Dec 2, 2023
1 parent ad7eae3 commit 21395c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions type_manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package lo

import "reflect"

// 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() }()
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
}

// EmptyableToPtr returns a pointer copy of value if it's nonzero.
// Otherwise, returns nil pointer.
func EmptyableToPtr[T any](x T) *T {
Expand Down
20 changes: 10 additions & 10 deletions type_manipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 21395c5

Please sign in to comment.