Skip to content

fix(mutable.Filter): clear stale elements to prevent leak#850

Open
Emil-Ka wants to merge 1 commit into
samber:masterfrom
Emil-Ka:fix/mutable-filter-duplicate
Open

fix(mutable.Filter): clear stale elements to prevent leak#850
Emil-Ka wants to merge 1 commit into
samber:masterfrom
Emil-Ka:fix/mutable-filter-duplicate

Conversation

@Emil-Ka
Copy link
Copy Markdown

@Emil-Ka Emil-Ka commented Mar 28, 2026

Fixes #842

mutable.Filter shifts elements left but leaves the tail of the original slice untouched, causing ghost duplicates when iterating over the original variable.

Before:

doc := []el{{"foo", 234}, {"bar", 345}, {"baz", 111}}
mutable.Filter(doc, func(e el) bool { return e.Key != "bar" })
fmt.Println(doc) // [{foo 234} {baz 111} {baz 111}] ← duplicate baz

After:

fmt.Println(doc) // [{foo 234} {baz 111} {Key: Val:<nil>}] ← tail zeroed

Fix zeros out the tail after filtering (same approach as slices.Delete in stdlib). Applied to both Filter and FilterI.

@d-enk
Copy link
Copy Markdown
Contributor

d-enk commented Mar 30, 2026

Because there is no problem with duplicates (just wrong usage)

In my opinion, title a unfortunate

Something like this would be better

fix(mutable.Filter): clear stale elements to prevent leak

@Emil-Ka Emil-Ka changed the title fix mutable.Filter duplicates element in tail fix(mutable.Filter): clear stale elements to prevent leak Mar 31, 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.

mutable.Filter duplicates an element

2 participants