Skip to content

fix: NotNil fails on non-nillable types#1920

Closed
happysnaker wants to merge 1 commit into
stretchr:masterfrom
happysnaker:fix-notnil-nonnillable
Closed

fix: NotNil fails on non-nillable types#1920
happysnaker wants to merge 1 commit into
stretchr:masterfrom
happysnaker:fix-notnil-nonnillable

Conversation

@happysnaker

Copy link
Copy Markdown

Summary

Fixes #570

Currently NotNil(t, 0), NotNil(t, ""), NotNil(t, mystruct{}) all pass without error because isNil returns false for non-nillable types, making NotNil always succeed for values like int, string, and struct.

Changes

  1. Added isNillableKind helper that checks if a reflect.Kind can hold a nil value
  2. Updated NotNil to fail when called with a non-nillable type
  3. Refactored isNil to use the new isNillableKind helper

Before

assert.NotNil(t, 0)     // passes (incorrectly)
assert.NotNil(t, "")   // passes (incorrectly)
assert.NotNil(t, MyStruct{}) // passes (incorrectly)

After

assert.NotNil(t, 0)     // fails: Expected value not to be nil, but got non-nillable type int
assert.NotNil(t, "")   // fails: Expected value not to be nil, but got non-nillable type string

@happysnaker

Copy link
Copy Markdown
Author

Tests fail because existing tests use NotNil with non-nillable types like true, 0, "true". This is a breaking change that requires updating those tests. Closing for now — will re-approach with a less breaking fix.

@happysnaker happysnaker closed this Jul 3, 2026
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.

assert: NotNil accepts non-nillable types and doesn't fail

1 participant