-
Notifications
You must be signed in to change notification settings - Fork 153
Add CompletedTaskAttribute to suppress VSTHRD003 for known completed tasks #1510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/add-support-for-vsthrd003
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
91a1e36
Initial plan
Copilot b9f60ec
Add CompletedTaskAttribute support for VSTHRD003
Copilot a864039
Fix CompletedTaskAttribute preprocessor directive and update document…
Copilot d067b66
Add assembly-level CompletedTaskAttribute support for external types
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
51 changes: 51 additions & 0 deletions
51
...o.Threading.Analyzers.CodeFixes/buildTransitive/AdditionalFiles/CompletedTaskAttribute.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #if !COMPLETEDTASKATTRIBUTE_INCLUDED | ||
| #define COMPLETEDTASKATTRIBUTE_INCLUDED | ||
|
|
||
| namespace Microsoft.VisualStudio.Threading; | ||
|
|
||
| /// <summary> | ||
| /// Indicates that a property, method, or field returns a task that is already completed. | ||
| /// This suppresses VSTHRD003 warnings when awaiting the returned task. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Apply this attribute to properties, methods, or fields that return cached, pre-completed tasks | ||
| /// such as singleton instances with well-known immutable values. | ||
| /// The VSTHRD003 analyzer will not report warnings when these members are awaited, | ||
| /// as awaiting an already-completed task does not pose a risk of deadlock. | ||
| /// </para> | ||
| /// <para> | ||
| /// This attribute can also be applied at the assembly level to mark members in external types | ||
| /// that you don't control: | ||
| /// <code> | ||
| /// [assembly: CompletedTask(Member = "System.Threading.Tasks.TplExtensions.TrueTask")] | ||
| /// </code> | ||
| /// </para> | ||
| /// </remarks> | ||
| [System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Method | System.AttributeTargets.Field | System.AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)] | ||
| #pragma warning disable SA1649 // File name should match first type name | ||
| internal sealed class CompletedTaskAttribute : System.Attribute | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="CompletedTaskAttribute"/> class. | ||
| /// </summary> | ||
| public CompletedTaskAttribute() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the fully qualified name of the member that returns a completed task. | ||
| /// This is only used when the attribute is applied at the assembly level. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The format should be: "Namespace.TypeName.MemberName". | ||
| /// For example: "System.Threading.Tasks.TplExtensions.TrueTask". | ||
| /// </remarks> | ||
| public string? Member { get; set; } | ||
| } | ||
| #pragma warning restore SA1649 // File name should match first type name | ||
|
|
||
| #endif |
5 changes: 4 additions & 1 deletion
5
...ng.Analyzers.CodeFixes/buildTransitive/Microsoft.VisualStudio.Threading.Analyzers.targets
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <ItemGroup> | ||
| <AdditionalFiles Include="$(MSBuildThisFileDirectory)AdditionalFiles\**"> | ||
| <AdditionalFiles Include="$(MSBuildThisFileDirectory)AdditionalFiles\*.txt"> | ||
| <Visible>false</Visible> | ||
| </AdditionalFiles> | ||
| <Compile Include="$(MSBuildThisFileDirectory)AdditionalFiles\*.cs" Link="%(Filename)%(Extension)"> | ||
| <Visible>false</Visible> | ||
| </Compile> | ||
| </ItemGroup> | ||
| </Project> |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot what if the attribute could be applied at the assembly level, such that you could also apply this attribute to types you don't control. For example if I was using
TplExtensionsand it wasn't annotated:Or something like that. If this isn't feasible, or has major drawbacks, feel free to push back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! I've implemented assembly-level attribute support. You can now mark external members like this:
The attribute now supports a
Memberproperty that accepts fully qualified names in the formatNamespace.TypeName.MemberName. The analyzer checks assembly-level attributes and matches them against the symbols being analyzed. Multiple assembly-level attributes are supported for marking different external members.Committed in d067b66.