Uh oh!
There was an error while loading. Please reload this page.
Use Requires.Range in LastIndexOf - #124560
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`.
Tagging subscribers to this area: @dotnet/area-system-collections |
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 |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ViveliDuCh
commented
Feb 24, 2026
/ba-g Failing consistently with Known Issue #118603 after retries. Unrelated to PR changes. |
Uh oh!
There was an error while loading. Please reload this page.
This PR is a follow-up to dotnet#124161. 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`. Changes: - Use `Requires.Range` in `ImmutableList<T>.Node.LastIndexOf` for validating the `index` and `count` parameters. - Add a test case to verify that an `ArgumentOutOfRangeException` is thrown when validation of the `index` and `count` parameters in `LastIndexOf` and `FindLastIndex` fails. - Fix test case to also validate the parameter name. - Rename `ImmutableList<T>.Node.LastIndexOf` index param to startIndex
This PR is a follow-up to #124161 that fixes#70950
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