Skip to content

Commit a864039

Browse files
Copilotdrewnoakes
andcommitted
Fix CompletedTaskAttribute preprocessor directive and update documentation
Co-authored-by: drewnoakes <[email protected]>
1 parent b9f60ec commit a864039

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

docfx/analyzers/VSTHRD003.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ When required to await a task that was started earlier, start it within a delega
1010
`JoinableTaskFactory.RunAsync`, storing the resulting `JoinableTask` in a field or variable.
1111
You can safely await the `JoinableTask` later.
1212

13+
## Suppressing warnings for completed tasks
14+
15+
If you have a property, method, or field that returns a pre-completed task (such as a cached task with a known value),
16+
you can suppress this warning by applying the `[CompletedTask]` attribute to the member.
17+
This attribute is automatically included when you install the `Microsoft.VisualStudio.Threading.Analyzers` package.
18+
19+
```csharp
20+
[Microsoft.VisualStudio.Threading.CompletedTask]
21+
private static readonly Task<bool> TrueTask = Task.FromResult(true);
22+
23+
async Task MyMethodAsync()
24+
{
25+
await TrueTask; // No warning - TrueTask is marked as a completed task
26+
}
27+
```
28+
29+
The analyzer already recognizes the following as safe to await without the attribute:
30+
- `Task.CompletedTask`
31+
- `Task.FromResult(...)`
32+
- `Task.FromCanceled(...)`
33+
- `Task.FromException(...)`
34+
- `TplExtensions.CompletedTask`
35+
- `TplExtensions.CanceledTask`
36+
- `TplExtensions.TrueTask`
37+
- `TplExtensions.FalseTask`
38+
1339
## Simple examples of patterns that are flagged by this analyzer
1440

1541
The following example would likely deadlock if `MyMethod` were called on the main thread,

src/Microsoft.VisualStudio.Threading.Analyzers.CodeFixes/buildTransitive/AdditionalFiles/CompletedTaskAttribute.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
#if !COMPLETEDTASKATTRIBUTE_INCLUDED
5+
#define COMPLETEDTASKATTRIBUTE_INCLUDED
56

67
namespace Microsoft.VisualStudio.Threading;
78

@@ -16,18 +17,10 @@ namespace Microsoft.VisualStudio.Threading;
1617
/// as awaiting an already-completed task does not pose a risk of deadlock.
1718
/// </remarks>
1819
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Method | System.AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
19-
internal sealed class CompletedTaskAttribute : System.Attribute
20-
{
21-
}
22-
23-
#pragma warning disable SA1403 // File may only contain a single namespace
2420
#pragma warning disable SA1649 // File name should match first type name
25-
internal static class CompletedTaskAttributeDefinition
21+
internal sealed class CompletedTaskAttribute : System.Attribute
2622
{
27-
internal const bool Included = true;
2823
}
2924
#pragma warning restore SA1649 // File name should match first type name
30-
#pragma warning restore SA1403 // File may only contain a single namespace
3125

32-
#define COMPLETEDTASKATTRIBUTE_INCLUDED
3326
#endif

0 commit comments

Comments
 (0)