Skip to content

Read ReplaceWith's new items before clearing the collection - #133

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/extensions-132-replacewith-self
Sep 26, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/extensions-132-replacewith-self

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #132

Problem

CollectionExtensions.ReplaceWith called Clear() before it enumerated newItems. When newItems was the collection itself, or a lazy query over it, the enumeration happened after the clear and yielded nothing. The collection was silently emptied:

  • list.ReplaceWith(list.Where(x => x > 2)) gave [] where [3, 4] was expected.
  • list.ReplaceWith(list) gave [] where [1, 2, 3] was expected.

Fix

ReplaceWith now copies newItems into an array first, then clears the collection and adds from the array. The argument validation is unchanged.

Tests

New tests in CollectionExtensionsTests:

  • ReplaceWithLazyQueryOverSameCollectionKeepsMatchingItems
  • ReplaceWithSameCollectionKeepsItems

With the fix reverted, both fail. With it, the full suite passes (136/136). The library also builds cleanly in Release for all of its target frameworks.

This PR is independent of #131; both branch from main.

🤖 Generated with Claude Code

https://claude.ai/code/session_012Hhb8knddbu4LRSs2uu9jR


Generated by Claude Code

ReplaceWith cleared the collection and then enumerated newItems, so a
lazy query over the collection (items.Where(...)) or the collection
itself yielded nothing and the collection ended up empty. Snapshot
newItems into an array before clearing.

Fixes #132

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Hhb8knddbu4LRSs2uu9jR
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 0acf746 into main Sep 26, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the claude/extensions-132-replacewith-self branch September 26, 2026 09:53
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.

ReplaceWith empties the collection when the new items are derived from the collection itself

2 participants