Skip to content

[code-simplifier] refactor: simplify TestRunCache.OnTestCompletion null checks - #16176

Closed
Jakub Jareš (nohwnd) wants to merge 1 commit into
mainfrom
code-simplifier/testrun-cache-null-check-1ce60291296ba22e
Closed

[code-simplifier] refactor: simplify TestRunCache.OnTestCompletion null checks#16176
Jakub Jareš (nohwnd) wants to merge 1 commit into
mainfrom
code-simplifier/testrun-cache-null-check-1ce60291296ba22e

Conversation

@nohwnd

Copy link
Copy Markdown
Member

Code Simplification — 2026-06-26

This PR simplifies OnTestCompletion in TestRunCache to improve clarity and correctness following recent changes in #16165.

Files Simplified

  • src/Microsoft.TestPlatform.CrossPlatEngine/Execution/TestRunCache.cs — removed dead null check and applied project null-check conventions

Improvements Made

  1. Removed dead-code null check

  2. Applied is null / is not null conventions

    • completedTest == nullcompletedTest is null
    • inProgressTest != nullinProgressTest is not null
    • Per project coding standards: "Always use is null or is not null instead of == null or != null."
  3. Fixed misleading warning message

    • "TestRunCache: InProgressTests is null""TestRunCache: InProgressTests is empty"
    • The condition was always checking Count == 0; the message now accurately describes what was detected.

Changes Based On

Recent changes from:

Testing

  • ✅ Build succeeds (0 errors, same pre-existing IL-trimming warnings)
  • ✅ No functional changes — behaviour is identical; only unreachable code paths removed and log messages corrected

Review Focus

Please verify:

  • The _inProgressTests == null branch was indeed unreachable (the field is always initialised to a new List<TestCase>(...) in the constructor and only ever replaced with another non-null list)
  • Null-pattern syntax (is null / is not null) is consistent with the rest of the codebase
  • Warning message text reflects the actual condition

Generated by Code Simplifier · 1.1K AIC · ⌖ 31.6 AIC · ⊞ 31.6K ·

  • expires on Jun 27, 2026, 3:54 PM UTC

PR #16165 changed _inProgressTests from ICollection<TestCase> to List<TestCase>
(a non-nullable type), making the 'null' check in OnTestCompletion dead code.
Remove the unreachable null guard and align with project conventions:

- Use 'is null' / 'is not null' instead of '== null' / '!= null'
- Remove '_inProgressTests == null' dead-code branch (non-nullable List<T>)
- Fix misleading warning message: 'InProgressTests is null' -> 'InProgressTests is empty'

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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 TestRunCache.OnTestCompletion in the CrossPlatEngine execution layer by removing an unreachable null-check branch and aligning null-check syntax and diagnostics with established project conventions.

Changes:

  • Replaced == null / != null with is null / is not null in OnTestCompletion.
  • Removed the dead _inProgressTests == null guard (the field is always initialized to a non-null List<TestCase> and only re-assigned to another non-null list).
  • Updated the warning text to correctly describe the Count == 0 condition (“empty” rather than “null”).

@nohwnd Jakub Jareš (nohwnd) left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🧠 Reviewed by expert-reviewer · Parallel Execution & Scheduling Safety · Null Safety & Boundary Validation · Error Reporting & Diagnostic Clarity

All three changes are correct and safe. Here's the key reasoning:

_inProgressTests null guard removal

Every write to _inProgressTests assigns a fresh new List<TestCase>(...):

  • Constructor (line 95): _inProgressTests = new List<TestCase>(InitialCapacity(cacheSize));
  • SendResults() (line 343): _inProgressTests = new List<TestCase>(InitialCapacity(_cacheSize));

SendResults() is only ever invoked from CheckForCacheHit() / CheckForCacheHitOnTimer(), both of which are themselves called under lock (_syncObject). OnTestCompletion also holds the same lock. The field can therefore never be null at the point of the removed check — the guard was provably dead code after #16165 changed the field type from ICollection<TestCase> to List<TestCase>.

is null / is not null patterns

TestCase is a sealed class, so FirstOrDefault correctly returns null (not a default struct value) when no matching element is found. The is not null check is semantically identical to the old != null and aligns with project conventions.

Warning message fix

The old message said "InProgressTests is null" but the condition guarded was Count == 0. The corrected message "InProgressTests is empty" accurately reflects the actual diagnostic condition.

Description alignment ✅

The PR description precisely describes all three changes with no undescribed edits and no stale claims.

🧠 Reviewed by Expert Code Reviewer 🧠

@github-actions

Copy link
Copy Markdown
Contributor

This pull request was automatically closed because it expired on 2026-06-27T15:54:22.603Z.

Closed by Workflow

@github-actions github-actions Bot closed this Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants