Skip to content

Fix Empty for pointers to interfaces - #2021

Open
draarivpatel-ui wants to merge 1 commit into
stretchr:masterfrom
draarivpatel-ui:fix-empty-pointer-to-interface
Open

draarivpatel-ui wants to merge 1 commit into
stretchr:masterfrom
draarivpatel-ui:fix-empty-pointer-to-interface

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #1955.

isEmptyValue recursed through reflect.Ptr but not reflect.Interface, so a non-nil pointer to an interface was always considered non-empty even when the interface held an empty concrete value. This regressed in v1.11.0 when isEmpty was rewritten to walk reflect.Value directly (#1761).

The fix adds reflect.Interface to the existing pointer-recursion case, making Empty(&i) agree with Empty(i). The IsZero fast path still handles nil interfaces and nil pointers before the switch.

Added a focused test covering pointers to interfaces holding empty and non-empty values. Full go test ./... and go vet ./... pass.

isEmptyValue now recurses through reflect.Interface as it does through
reflect.Ptr, so Empty(&i) agrees with Empty(i) when i holds an empty
concrete value. Restores the behavior from v1.10.0 before the IsZero
rewrite in stretchr#1761.

Fixes stretchr#1955
Copilot AI balanced review requested due to automatic review settings November 25, 2026 21:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran Empty / NotEmpty on pointers to interfaces holding "", 0, []int{}, a zero struct, a nil *int, nil and "x". The zero values are now Empty, the same as passing the value directly (master: NotEmpty). "x" stays NotEmpty and nil is Empty on both.

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.Empty regression in v1.11.0: a pointer to an interface is never empty

3 participants