Repository navigation
Use ThrowHelper.ThrowInvalidOperationException in ImmutableCollection - #129942
Merged
Merged
Conversation
Replace inline throw new InvalidOperationException statements with ThrowHelper.ThrowInvalidOperationException calls, following the pattern established in FrozenSet/FrozenDictionary.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors several System.Collections.Immutable implementation sites to use the library’s System.Collections.ThrowHelper.ThrowInvalidOperationException() helper instead of inline throw new InvalidOperationException() in common “invalid state” paths (e.g., empty builder, invalid enumerator Current).
Changes:
- Switched a few
InvalidOperationExceptionthrow sites toThrowHelper.ThrowInvalidOperationException()across builder helpers and enumerators. - Added
using System.Collections;in theSystem.Linqextensions file to accessThrowHelper. - Kept enumerator semantics equivalent while slightly reshaping conditionals (e.g., invert the null/bounds check and throw via helper).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs | Uses ThrowHelper.ThrowInvalidOperationException() for empty ImmutableArray<T>.Builder First/Last helpers (adds using System.Collections; for access). |
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.Enumerator.cs | Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state. |
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.Enumerator.cs | Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state in both struct and class enumerators. |
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Enumerator.cs | Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state. |
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Enumerator.cs | Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state. |
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Enumerator.cs | Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state. |
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.HashBucket.cs | Uses ThrowHelper.ThrowInvalidOperationException() for FirstValue when bucket is empty. |
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Enumerator.cs | Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state and preserves the (uint) bounds-check pattern. |
prozolic
marked this pull request as ready for review
June 28, 2026 07:23
eiriktsarpalis
pushed a commit
that referenced
this pull request
Jul 15, 2026
…#129942) This PR replaces inline `throw new InvalidOperationException` statements with `ThrowHelper.ThrowInvalidOperationException` calls, following the pattern established in `FrozenSet`/`FrozenDictionary`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR replaces inline
throw new InvalidOperationExceptionstatements withThrowHelper.ThrowInvalidOperationExceptioncalls, following the pattern established inFrozenSet/FrozenDictionary.