feat: Add INTL0304 analyzer for Task.Delay(0) usage#443
Open
BenjaminMichaelis wants to merge 3 commits into
Open
feat: Add INTL0304 analyzer for Task.Delay(0) usage#443BenjaminMichaelis wants to merge 3 commits into
BenjaminMichaelis wants to merge 3 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
Contributor
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.
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.