Skip to content

[code-simplifier] perf: simplify Memory(T).HasCount to avoid unnecessary ReadOnlyMemory cast#9189

Draft
Evangelink wants to merge 1 commit into
mainfrom
code-simplifier/using-propertybagenumerator-2026-06-16-da79ede7557d9c48
Draft

[code-simplifier] perf: simplify Memory(T).HasCount to avoid unnecessary ReadOnlyMemory cast#9189
Evangelink wants to merge 1 commit into
mainfrom
code-simplifier/using-propertybagenumerator-2026-06-16-da79ede7557d9c48

Conversation

@Evangelink

Copy link
Copy Markdown
Member

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 intermediate ReadOnlyMemory<T> cast in the Memory<T> overload of Assert.HasCount.

Improvement

The Memory<T> string-message overload of Assert.HasCount was forwarding to the private ReadOnlySpan<T> overload via:

=> HasCount(nameof(HasCount), expected, ((ReadOnlyMemory<T>)collection).Span, message, collectionExpression);

The explicit cast to ReadOnlyMemory<T> is unnecessary. Memory<T>.Span already returns Span<T>, which is implicitly convertible to ReadOnlySpan<T> when the compiler resolves the private overload. Simplified to:

=> HasCount(nameof(HasCount), expected, collection.Span, message, collectionExpression);

This now matches the adjacent ReadOnlyMemory<T> overload directly above it, which also uses collection.Span.

Changes Based On

Recent changes from:

Testing

  • ✅ Build succeeds with zero warnings (./build.sh -b -c Debug)
  • ✅ No functional changes — behavior is identical

🤖 Automated content by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Code Simplifier workflow. · 1.6K AIC · ⌖ 24.1 AIC · [◷]( · )

Add this agentic workflows to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/code-simplifier.md@main
  • expires on Jun 17, 2026, 3:20 PM UTC

… 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>
Copilot AI review requested due to automatic review settings June 16, 2026 15:20
@Evangelink Evangelink added type/automation Created or maintained by an agentic workflow. type/tech-debt Code health, refactoring, simplification. labels Jun 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).Span cast and replaced it with collection.Span.
  • Keeps forwarding to the existing private HasCount(..., ReadOnlySpan<T> ...) implementation (via implicit Span<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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/automation Created or maintained by an agentic workflow. type/tech-debt Code health, refactoring, simplification.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants