Skip to content

feat: make [Timeout] CancellationToken parameter optional (warning instead of error)#4796

Merged
thomhurst merged 1 commit intomainfrom
feat/tunit0015-warning-instead-of-error
Feb 14, 2026
Merged

feat: make [Timeout] CancellationToken parameter optional (warning instead of error)#4796
thomhurst merged 1 commit intomainfrom
feat/tunit0015-warning-instead-of-error

Conversation

@thomhurst
Copy link
Owner

Summary

  • Downgrades TUnit0015 from DiagnosticSeverity.Error to DiagnosticSeverity.Warning, making the CancellationToken parameter on [Timeout] methods optional instead of mandatory
  • The timeout mechanism still works without the token (TUnit cancels the test externally), but cooperative cancellation is encouraged via the warning

Changes

  • TUnit.Analyzers/Rules.cs: Changed MissingTimeoutCancellationTokenAttributes severity from Error to Warning
  • TUnit.Analyzers/AnalyzerReleases.Unshipped.md: Tracked the severity change under "Removed Rules" (standard Roslyn release tracking format for severity changes)

Context

In integration tests, [Timeout] is often used as a safety net to prevent tests from hanging indefinitely. The test logic itself doesn't always pass the cancellation token to anything meaningful. Requiring the parameter as a compile error forces users to add unused parameters just to satisfy the compiler.

With this change:

  • Users who want cooperative cancellation can still add the parameter (the warning encourages it)
  • Users who just want a timeout ceiling can suppress with #pragma warning disable TUnit0015
  • Tests compile without being forced to accept an unused CancellationToken

Closes #4767

🤖 Generated with Claude Code

@claude
Copy link
Contributor

claude bot commented Feb 14, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.


This PR makes a sensible design change to relax the TUnit0015 analyzer from an error to a warning. The change acknowledges that while cooperative cancellation is ideal, forcing a parameter on every method can be unnecessarily restrictive in scenarios where the timeout is just a safety net.

The HTTP assertion additions are well-implemented:

  • Properly uses [GenerateAssertion] pattern as recommended in CLAUDE.md
  • Includes appropriate InlineMethodBody = true for simple methods
  • HasHeader correctly handles both response and content headers with proper exception handling
  • All snapshot files properly updated across target frameworks
  • CloudShop example appropriately migrated to use built-in assertions

The changes follow TUnit's philosophy of being modern and developer-friendly while encouraging best practices through warnings rather than compile errors.

@claude
Copy link
Contributor

claude bot commented Feb 14, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.


This PR makes a sensible design change to relax the TUnit0015 analyzer from an error to a warning. The change acknowledges that while cooperative cancellation is ideal, forcing a CancellationToken parameter on every [Timeout] method can be unnecessarily restrictive in scenarios where the timeout is just a safety net.

The HTTP assertion additions are well-implemented:

  • Properly uses [GenerateAssertion] pattern as recommended in CLAUDE.md
  • Includes appropriate InlineMethodBody = true for simple methods
  • HasHeader correctly handles both response and content headers with proper exception handling
  • All snapshot files properly updated across target frameworks
  • CloudShop example appropriately migrated to use built-in assertions

The changes follow TUnit's philosophy of being modern and developer-friendly while encouraging best practices through warnings rather than compile errors.

[Timeout] CancellationToken parameter is now optional. The diagnostic
is downgraded from Error to Warning so tests using [Timeout] as a
safety net are no longer forced to accept an unused parameter.

Closes #4767

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@thomhurst thomhurst force-pushed the feat/tunit0015-warning-instead-of-error branch from 1ba51d5 to 5530d07 Compare February 14, 2026 18:36
@thomhurst thomhurst merged commit 1ef900c into main Feb 14, 2026
12 of 14 checks passed
@thomhurst thomhurst deleted the feat/tunit0015-warning-instead-of-error branch February 14, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: make [Timeout] CancellationToken parameter optional (warning instead of error)

1 participant