Skip to content

empty: equal empty slice support#269

Open
NathanBaulch wants to merge 1 commit into
Antonboom:masterfrom
NathanBaulch:emptyarray
Open

empty: equal empty slice support#269
NathanBaulch wants to merge 1 commit into
Antonboom:masterfrom
NathanBaulch:emptyarray

Conversation

@NathanBaulch

Copy link
Copy Markdown

I'm completely new to Go code analysis but thought I'd give this a try.

I have a project where contributors often assert values are equal to empty slices. I'd like these to use assert.Empty for more meaningful failure messages and general consistency.

assert.Equal(t, []foo{}, actual) -> assert.Empty(actual)

There are a couple of complications to consider however. If the actual value is a nil slice then assert.Empty is more permissive since assert.Equal would have failed. This is not a distinction I personally care about, but I'm not sure how conservative this linter aims to be. See stretchr/testify#1141.

actual := ([]string)(nil)
assert.Equal(t, []string{}, actual)  // fail
assert.Empty(t, actual)              // success

Also, if the types don't match then that would also produce a false-success.

var actual any = []int{}
assert.Equal(t, []string{}, actual)  // fail
assert.Empty(t, actual)              // success

It looks easy enough to add a type check with something like types.Identical(pass.TypesInfo.TypeOf(a), pass.TypesInfo.TypeOf(b)) but the other equals based empty checks don't do this so I left it out. Perhaps they should?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant