Skip to content

feat: Add INTL0304 analyzer for Task.Delay(0) usage#443

Open
BenjaminMichaelis wants to merge 3 commits into
mainfrom
benjaminmichaelis/issue-55-plan
Open

feat: Add INTL0304 analyzer for Task.Delay(0) usage#443
BenjaminMichaelis wants to merge 3 commits into
mainfrom
benjaminmichaelis/issue-55-plan

Conversation

@BenjaminMichaelis
Copy link
Copy Markdown
Member

Why

Task.Delay(0) is effectively a completed task and adds unnecessary overhead. This change adds guidance and an automatic fix so call sites prefer Task.CompletedTask while preserving cancellation semantics for the token overload.

What changed

  • Added new analyzer rule INTL0304 (Performance, Info) to detect Task.Delay(...) calls where millisecondsDelay is compile-time 0.
  • Added code fix:
    • Task.Delay(0) -> Task.CompletedTask
    • Task.Delay(0, token) -> token.IsCancellationRequested ? Task.FromCanceled(token) : Task.CompletedTask
  • Added release tracking entry in AnalyzerReleases.Unshipped.md.
  • Added docs for INTL0304 in docs/analyzers/03XX.Performance.md and linked it from docs/analyzers/index.md.

Non-obvious behavior and safety

  • The code fix includes a source-linked runtime behavior note referencing:
    System.Threading.Tasks.Task.Delay(uint, TimeProvider, CancellationToken) lines 5907-5911.
  • Token-overload fixes are intentionally constrained to safe token expressions (locals/parameters/default and CancellationToken.None) to avoid changing behavior via duplicated side-effecting evaluations.

Validation

  • Updated analyzer/code-fix tests covering:
    • direct zero-delay replacement,
    • token overload semantics,
    • CancellationToken.None,
    • await Task.Delay(0) replacement,
    • no-fix behavior for side-effecting/property token expressions.
  • dotnet test IntelliTect.Analyzer.sln --nologo passes.

Implement analyzer and code fix for Task.Delay(0), including cancellation-token semantics and runtime-source documentation links. Add release metadata, docs updates, and expanded tests for await, CancellationToken.None, and safe code-fix applicability.
- Remove duplicate IsTaskDelayWithIntMilliseconds from CodeFixes/TaskDelayZero.cs;
  call the public static method on Analyzers.TaskDelayZero instead
- Make Analyzers.TaskDelayZero.IsTaskDelayWithIntMilliseconds public so the
  CodeFixes assembly can reference it without InternalsVisibleTo
- Extract DiagnosticId and message into constants in TaskDelayZeroTests to
  avoid repeated string literals
@BenjaminMichaelis BenjaminMichaelis marked this pull request as ready for review May 17, 2026 23:55
Copilot AI review requested due to automatic review settings May 17, 2026 23:55
@BenjaminMichaelis BenjaminMichaelis changed the title Add INTL0304 analyzer for Task.Delay(0) usage feat: Add INTL0304 analyzer for Task.Delay(0) usage May 17, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new analyzer rule INTL0304 (Performance, Info) that detects Task.Delay(...) calls with a compile-time millisecondsDelay of 0, plus a code fix that rewrites them to Task.CompletedTask (or to a IsCancellationRequested-aware ternary using Task.FromCanceled/Task.CompletedTask when a cancellation token is supplied). Release tracking and analyzer documentation are updated accordingly.

Changes:

  • New TaskDelayZero analyzer matching Task.Delay overloads whose first parameter is int millisecondsDelay and the argument is constant 0.
  • New TaskDelayZero code fix that emits Task.CompletedTask or, for the token overload, a safe ternary—bailing out on side-effecting/property token expressions.
  • Documentation entries in 03XX.Performance.md and index.md, plus an AnalyzerReleases.Unshipped.md entry.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
IntelliTect.Analyzer/IntelliTect.Analyzer/Analyzers/TaskDelayZero.cs New analyzer detecting Task.Delay(0) calls.
IntelliTect.Analyzer/IntelliTect.Analyzer.CodeFixes/TaskDelayZero.cs New code fix replacing the invocation with Task.CompletedTask or token-aware ternary.
IntelliTect.Analyzer/IntelliTect.Analyzer.Test/TaskDelayZeroTests.cs Tests for diagnostic emission and code-fix output across overloads and unsafe token expressions.
IntelliTect.Analyzer/IntelliTect.Analyzer/AnalyzerReleases.Unshipped.md Release tracking entry for INTL0304.
docs/analyzers/03XX.Performance.md Docs section for INTL0304.
docs/analyzers/index.md Index link to the new rule.
Directory.Packages.props No effective change (line-ending/whitespace only in the diff).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread IntelliTect.Analyzer/IntelliTect.Analyzer.CodeFixes/TaskDelayZero.cs Outdated
- Add Simplifier annotations so code-fix output simplifies Task symbols
- Build replacement expressions with syntax nodes instead of raw strings
- Skip code-fix rewrites when Task.Delay overload includes timeProvider
- Add regression tests for TimeProvider overload no-fix behavior
- Update fixed-code expectations for simplified output
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated no new comments.

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.

2 participants