Use Requires.Range in LastIndexOf#124560
Merged
ViveliDuCh merged 5 commits intodotnet:mainfrom Feb 24, 2026
Merged
Conversation
Change `Requires.Argument(index - count + 1 >= 0)` to `Requires.Range(index - count + 1 >= 0, nameof(count))` in `ImmutableList<T>.Node.LastIndexOf` to align with the validation used in `ImmutableArray<T>.LastIndexOf`.
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR is a follow-up to #124161 that improves parameter validation in ImmutableList<T>.Node.LastIndexOf by changing from Requires.Argument to Requires.Range. This ensures that ArgumentOutOfRangeException (instead of ArgumentException) is thrown when the combination of index and count parameters results in an invalid range, aligning the behavior with ImmutableArray<T>.LastIndexOf.
Changes:
- Updated
LastIndexOfto useRequires.Rangewithnameof(count)for the validation checkindex - count + 1 >= 0 - Added test cases to verify
ArgumentOutOfRangeExceptionis thrown for invalidindexandcountcombinations in bothLastIndexOfandFindLastIndex
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs | Changed validation from Requires.Argument to Requires.Range(index - count + 1 >= 0, nameof(count)) to throw the appropriate exception type |
| src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs | Added test case to verify ArgumentOutOfRangeException is thrown when index - count + 1 < 0 in LastIndexOf |
| src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs | Added test case to verify ArgumentOutOfRangeException is thrown when startIndex - count + 1 < 0 in FindLastIndex |
...raries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Collections.Immutable/tests/ImmutableListTestBase.cs
Outdated
Show resolved
Hide resolved
This was referenced Feb 18, 2026
src/libraries/System.Collections.Immutable/tests/ImmutableListBuilderTest.cs
Outdated
Show resolved
Hide resolved
This was referenced Feb 21, 2026
Open
Member
|
/ba-g Failing consistently with Known Issue #118603 after retries. Unrelated to PR changes. |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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 is a follow-up to #124161.
Change
Requires.Argument(index - count + 1 >= 0)toRequires.Range(index - count + 1 >= 0, nameof(count))inImmutableList<T>.Node.LastIndexOfto align with the validation used inImmutableArray<T>.LastIndexOf.Changes:
Requires.RangeinImmutableList<T>.Node.LastIndexOffor validating theindexandcountparameters.ArgumentOutOfRangeExceptionis thrown when validation of theindexandcountparameters inLastIndexOfandFindLastIndexfails.ImmutableList<T>.Node.LastIndexOfindex param to startIndex