Uh oh!
There was an error while loading. Please reload this page.
feat: Add INTL0304 analyzer for Task.Delay(0) usage - #443
feat: Add INTL0304 analyzer for Task.Delay(0) usage#443BenjaminMichaelis wants to merge 4 commits into
Conversation
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
There was a problem hiding this comment.
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
TaskDelayZeroanalyzer matchingTask.Delayoverloads whose first parameter isint millisecondsDelayand the argument is constant0. - New
TaskDelayZerocode fix that emitsTask.CompletedTaskor, for the token overload, a safe ternary—bailing out on side-effecting/property token expressions. - Documentation entries in
03XX.Performance.mdandindex.md, plus anAnalyzerReleases.Unshipped.mdentry.
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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- 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
Why
Task.Delay(0)is effectively a completed task and adds unnecessary overhead. This change adds guidance and an automatic fix so call sites preferTask.CompletedTaskwhile preserving cancellation semantics for the token overload.What changed
INTL0304(Performance,Info) to detectTask.Delay(...)calls wheremillisecondsDelayis compile-time0.Task.Delay(0)->Task.CompletedTaskTask.Delay(0, token)->token.IsCancellationRequested ? Task.FromCanceled(token) : Task.CompletedTaskAnalyzerReleases.Unshipped.md.INTL0304indocs/analyzers/03XX.Performance.mdand linked it fromdocs/analyzers/index.md.Non-obvious behavior and safety
System.Threading.Tasks.Task.Delay(uint, TimeProvider, CancellationToken)lines 5907-5911.CancellationToken.None) to avoid changing behavior via duplicated side-effecting evaluations.Validation
CancellationToken.None,await Task.Delay(0)replacement,dotnet test IntelliTect.Analyzer.sln --nologopasses.