Skip to content

Empty a set when ExceptWith is passed the set itself - #67

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/containers-65-exceptwith-self
Sep 26, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/containers-65-exceptwith-self

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #65

Problem

ExceptWith in ContiguousSet, OrderedSet and InsertionOrderSet called Remove on each item of other while enumerating it. When other was the set itself:

  • ContiguousSet skipped every other element, so {1,2,3,4} became {2,4}.
  • OrderedSet and InsertionOrderSet threw InvalidOperationException.

HashSet<T> clears the set in this case.

Fix

After the null check, each ExceptWith now checks ReferenceEquals(other, this). If it is true, it calls Clear() and returns. The other set operations already copy other into a temporary set first, so they did not need changing.

Tests

I added ExceptWith_Self_EmptiesSet to ContiguousSetTests, OrderedSetTests and InsertionOrderSetTests.

With the fix reverted, all 3 fail. With it, the full suite passes (376/376).

This PR is independent of #63 and #64; all three branch from main.

🤖 Generated with Claude Code

https://claude.ai/code/session_012Hhb8knddbu4LRSs2uu9jR


Generated by Claude Code

ContiguousSet, OrderedSet and InsertionOrderSet removed each item of
`other` while enumerating it. When `other` was the set itself, the
ContiguousSet enumerator skipped every other element and the List-backed
sets threw InvalidOperationException. Clear the set instead, matching
HashSet<T>.

Fixes #65

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 f1b171e into main Sep 26, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the claude/containers-65-exceptwith-self branch September 26, 2026 09:52
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.

set.ExceptWith(set) leaves every other element in ContiguousSet and throws in OrderedSet/InsertionOrderSet

2 participants