[code-simplifier] refactor: simplify TestRunCache.OnTestCompletion null checks - #16176
[code-simplifier] refactor: simplify TestRunCache.OnTestCompletion null checks#16176Jakub Jareš (nohwnd) wants to merge 1 commit into
Conversation
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>
There was a problem hiding this comment.
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/!= nullwithis null/is not nullinOnTestCompletion. - Removed the dead
_inProgressTests == nullguard (the field is always initialized to a non-nullList<TestCase>and only re-assigned to another non-null list). - Updated the warning text to correctly describe the
Count == 0condition (“empty” rather than “null”).
Jakub Jareš (nohwnd)
left a comment
There was a problem hiding this comment.
🧠 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 🧠
|
This pull request was automatically closed because it expired on 2026-06-27T15:54:22.603Z.
|
Code Simplification — 2026-06-26
This PR simplifies
OnTestCompletioninTestRunCacheto 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 conventionsImprovements Made
Removed dead-code null check
_inProgressTestsfromICollection<TestCase>toList<TestCase>(non-nullable). The guard_inProgressTests == nullbecame unreachable after that change.Applied
is null/is not nullconventionscompletedTest == null→completedTest is nullinProgressTest != null→inProgressTest is not nullis nulloris not nullinstead of== nullor!= null."Fixed misleading warning message
"TestRunCache: InProgressTests is null"→"TestRunCache: InProgressTests is empty"Count == 0; the message now accurately describes what was detected.Changes Based On
Recent changes from:
[efficiency-improver] perf: pre-allocate List(T) capacity in DiscoveryResultCache and TestRunCacheTesting
Review Focus
Please verify:
_inProgressTests == nullbranch was indeed unreachable (the field is always initialised to anew List<TestCase>(...)in the constructor and only ever replaced with another non-null list)is null/is not null) is consistent with the rest of the codebase