Skip to content

[code-simplifier] refactor: use switch expression for TargetInvocationException unwrap in BaseRunTests - #16181

Merged
Jakub Jareš (nohwnd) merged 1 commit into
mainfrom
simplify/base-run-tests-switch-expression-9f527948216eefa0
Jun 29, 2026
Merged

[code-simplifier] refactor: use switch expression for TargetInvocationException unwrap in BaseRunTests#16181
Jakub Jareš (nohwnd) merged 1 commit into
mainfrom
simplify/base-run-tests-switch-expression-9f527948216eefa0

Conversation

@nohwnd

Copy link
Copy Markdown
Member

Code Simplification — 2026-06-28

This PR simplifies recently modified code to improve clarity and consistency while preserving all functionality.

Files Simplified

  • src/Microsoft.TestPlatform.CrossPlatEngine/Execution/BaseRunTests.cs — replaced conditional ternary with a switch expression for the TargetInvocationException unwrap logic

Improvements Made

Applied Project Standards

  • Replaced is ... && ... ? ... : ... ternary with a switch expression, per the project guideline: "Use pattern matching and switch expressions wherever possible"
  • The property pattern TargetInvocationException { InnerException: { } inner } makes both the type check and the null guard explicit in one arm, removing the intermediate tie binding variable

Before:

Exception realException = ex is TargetInvocationException tie && tie.InnerException is not null
    ? tie.InnerException
    : ex;

After:

Exception realException = ex switch
{
    TargetInvocationException { InnerException: { } inner } => inner,
    _ => ex,
};

Changes Based On

Recent changes from:

Testing

  • ✅ Build succeeds (0 errors)
  • ✅ All 654 Microsoft.TestPlatform.CrossPlatEngine.UnitTests tests pass (net11.0|x64)
  • ✅ All 630 Microsoft.TestPlatform.CommunicationUtilities.UnitTests tests pass (net11.0|x64)
  • ✅ No functional changes — behavior is identical

Generated by Code Simplifier · 301.3 AIC · ⌖ 21.9 AIC · ⊞ 31.6K ·

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

…in BaseRunTests

Replace the conditional ternary with a switch expression, which is the
idiomatic pattern in this codebase per project guidelines.

No behavioral change — the switch expression is semantically equivalent
to the ternary it replaces.

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 performs a small refactor in the CrossPlatEngine execution path to simplify the TargetInvocationException unwrapping logic in BaseRunTests.RunTests, using a switch expression while keeping behavior unchanged.

Changes:

  • Replaced the ternary-based TargetInvocationException unwrap with a switch expression using a property pattern.
  • Kept the existing behavior: unwrap only when InnerException is non-null; otherwise preserve the original exception.

@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 · Error Reporting & Diagnostic Clarity · Algorithmic Correctness)

Scope

Single-file refactor in BaseRunTests.cs — replaces a ternary with a switch expression in the TargetInvocationException unwrap logic. Activated dimensions: Parallel Execution & Scheduling Safety, Error Reporting & Diagnostic Clarity, Algorithmic Correctness, Defensive Coding at Boundaries.


Semantic Equivalence ✅

The two forms are provably equivalent:

Form Condition Result
Old ternary ex is TargetInvocationException tie && tie.InnerException is not null tie.InnerException
New switch arm TargetInvocationException { InnerException: { } inner } inner

The { } property pattern in C# is a non-null object pattern — it matches any non-null value, exactly mirroring is not null. No behavioral change.

Error Reporting & Diagnostic Clarity ✅

The switch expression preserves all exception propagation semantics introduced in #16167. realException.Message and realException as inner exception are unaffected.

Parallel Execution & Scheduling Safety ✅

No threading-related changes. The catch block is local to the RunTests execution thread.

Defensive Coding at Boundaries ✅

The _ => ex fallback arm correctly handles all non-TargetInvocationException cases, including TargetInvocationException with a null InnerException (which the { } pattern rejects, falling through to _ => ex).

PR Description Alignment ✅

Title, description, before/after snippets, and reference to #16167 all match the diff exactly.


No issues found. The refactor is correct and clean.

🧠 Reviewed by Expert Code Reviewer 🧠

@nohwnd Jakub Jareš (nohwnd) added the 🚢 Ship it! Add to PRs where owner approves automated PR, but cannot approve because they "wrote it". label Jun 29, 2026
@nohwnd
Jakub Jareš (nohwnd) marked this pull request as ready for review June 29, 2026 13:07
@nohwnd
Jakub Jareš (nohwnd) enabled auto-merge (squash) June 29, 2026 13:07

@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 · Error Reporting & Diagnostic Clarity · Algorithmic Correctness)

Scope

Single-file refactor in BaseRunTests.cs — replaces a ternary with a switch expression in the TargetInvocationException unwrap logic. Activated dimensions: Parallel Execution & Scheduling Safety, Error Reporting & Diagnostic Clarity, Algorithmic Correctness, Defensive Coding at Boundaries.


Semantic Equivalence ✅

The two forms are provably equivalent:

Form Condition Result
Old ternary ex is TargetInvocationException tie && tie.InnerException is not null tie.InnerException
New switch arm TargetInvocationException { InnerException: { } inner } inner

The { } property pattern in C# is a non-null object pattern — it matches any non-null value, exactly mirroring is not null. The _ => ex arm handles all non-matching cases, including TargetInvocationException with a null InnerException. No behavioral change.

Error Reporting & Diagnostic Clarity ✅

The switch expression preserves all exception propagation semantics introduced in #16167. realException.Message and realException as inner exception are unaffected.

Parallel Execution & Scheduling Safety ✅

No threading-related changes. The catch block is local to the RunTests execution thread.

Defensive Coding at Boundaries ✅

The _ => ex fallback arm is exhaustive and correctly handles all non-TargetInvocationException cases.

PR Description Alignment ✅

Title, description, before/after snippets, and reference to #16167 all match the diff exactly.


No issues found. The refactor is correct and clean.

🧠 Reviewed by Expert Code Reviewer 🧠

🧠 Reviewed by Expert Code Reviewer 🧠

@nohwnd
Jakub Jareš (nohwnd) merged commit ae10697 into main Jun 29, 2026
51 checks passed
@nohwnd
Jakub Jareš (nohwnd) deleted the simplify/base-run-tests-switch-expression-9f527948216eefa0 branch June 29, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agentic-workflows 🚢 Ship it! Add to PRs where owner approves automated PR, but cannot approve because they "wrote it".

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants