[code-simplifier] perf: simplify Memory(T).HasCount to avoid unnecessary ReadOnlyMemory cast#9189
Draft
Evangelink wants to merge 1 commit into
Conversation
… cast The Memory<T> overload of Assert.HasCount was using ((ReadOnlyMemory<T>)collection).Span to forward to the private ReadOnlySpan<T> overload. The intermediate cast to ReadOnlyMemory<T> is unnecessary: Memory<T>.Span returns Span<T>, which is implicitly convertible to ReadOnlySpan<T> when the compiler resolves the private method call. This matches the pattern used by the ReadOnlyMemory<T> overload directly above it: => HasCount(nameof(HasCount), expected, collection.Span, message, collectionExpression); No behavior change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR simplifies the MSTest Assert.HasCount implementation in src/TestFramework/TestFramework by removing an unnecessary ReadOnlyMemory<T> cast in the Memory<T> + string? message overload, while preserving the existing behavior and call flow to the span-based private implementation.
Changes:
- Removed an explicit
((ReadOnlyMemory<T>)collection).Spancast and replaced it withcollection.Span. - Keeps forwarding to the existing private
HasCount(..., ReadOnlySpan<T> ...)implementation (via implicitSpan<T>→ReadOnlySpan<T>conversion).
Show a summary per file
| File | Description |
|---|---|
| src/TestFramework/TestFramework/Assertions/Assert.Count.cs | Simplifies Memory<T> HasCount overload by removing an unnecessary cast before forwarding to the span-based implementation. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 0
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.
Code Simplification — 2026-06-16
This PR simplifies recently added code from #9176 to improve clarity while preserving all functionality.
Files Simplified
src/TestFramework/TestFramework/Assertions/Assert.Count.cs— Removed an unnecessary intermediateReadOnlyMemory<T>cast in theMemory<T>overload ofAssert.HasCount.Improvement
The
Memory<T>string-message overload ofAssert.HasCountwas forwarding to the privateReadOnlySpan<T>overload via:The explicit cast to
ReadOnlyMemory<T>is unnecessary.Memory<T>.Spanalready returnsSpan<T>, which is implicitly convertible toReadOnlySpan<T>when the compiler resolves the private overload. Simplified to:This now matches the adjacent
ReadOnlyMemory<T>overload directly above it, which also usescollection.Span.Changes Based On
Recent changes from:
Assert.HasCountand extend MSTEST0037Testing
./build.sh -b -c Debug)Add this agentic workflows to your repo
To install this agentic workflow, run