Repository navigation
Recognize safely completed Task results in VSTHRD103 #1647
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
Merged
Andrew Arnott (AArnott)
merged 1 commit into
main
from
aarnott-fix-vsthrd103-flow-suppressions
Aug 22, 2026
Merged
Changes from all commits
Commits
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
170 changes: 170 additions & 0 deletions
170
src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/TaskCompletionAnalysis.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,170 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
|
|
||
| namespace Microsoft.VisualStudio.Threading.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Recognizes cases where accessing <see cref="System.Threading.Tasks.Task{TResult}.Result"/> cannot block. | ||
| /// </summary> | ||
| internal static class TaskCompletionAnalysis | ||
| { | ||
| /// <summary> | ||
| /// Determines whether a task is known to be complete at a particular <c>Result</c> access. | ||
| /// </summary> | ||
| /// <param name="context">The analyzer context.</param> | ||
| /// <param name="resultAccess">The <c>Result</c> access.</param> | ||
| /// <returns><see langword="true"/> if the task is known to be complete; otherwise, <see langword="false"/>.</returns> | ||
| internal static bool IsTaskKnownToBeCompleted(SyntaxNodeAnalysisContext context, MemberAccessExpressionSyntax resultAccess) | ||
| { | ||
| ISymbol? taskSymbol = context.SemanticModel.GetSymbolInfo(resultAccess.Expression, context.CancellationToken).Symbol; | ||
| if (taskSymbol is ILocalSymbol { RefKind: not RefKind.None } | ||
| || taskSymbol is IParameterSymbol { RefKind: not RefKind.None } | ||
| || taskSymbol is not ILocalSymbol and not IParameterSymbol) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| SyntaxNode executableScope = GetExecutableScope(resultAccess); | ||
| ISymbol? executableSymbol = executableScope switch | ||
| { | ||
| AnonymousFunctionExpressionSyntax anonymousFunction => context.SemanticModel.GetSymbolInfo(anonymousFunction, context.CancellationToken).Symbol, | ||
| LocalFunctionStatementSyntax localFunction => context.SemanticModel.GetDeclaredSymbol(localFunction, context.CancellationToken), | ||
| BaseMethodDeclarationSyntax method => context.SemanticModel.GetDeclaredSymbol(method, context.CancellationToken), | ||
| AccessorDeclarationSyntax accessor => context.SemanticModel.GetDeclaredSymbol(accessor, context.CancellationToken), | ||
| _ => null, | ||
| }; | ||
| if (!SymbolEqualityComparer.Default.Equals(taskSymbol.ContainingSymbol, executableSymbol)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return IsGuardedBySuccessfulCompletion(context, resultAccess, taskSymbol, executableScope) | ||
| || IsContinueWithAntecedent(context, resultAccess, taskSymbol, executableScope); | ||
| } | ||
|
|
||
| private static bool IsGuardedBySuccessfulCompletion( | ||
| SyntaxNodeAnalysisContext context, | ||
| MemberAccessExpressionSyntax resultAccess, | ||
| ISymbol taskSymbol, | ||
| SyntaxNode executableScope) | ||
| { | ||
| foreach (IfStatementSyntax ifStatement in resultAccess.Ancestors() | ||
| .TakeWhile(ancestor => ancestor != executableScope) | ||
| .OfType<IfStatementSyntax>()) | ||
| { | ||
| if (ifStatement.Statement.Span.Contains(resultAccess.Span) | ||
| && ConditionProvesSuccessfulCompletion(context, ifStatement.Condition, taskSymbol) | ||
| && !MayWriteSymbol(context, executableScope, taskSymbol)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static bool IsContinueWithAntecedent( | ||
| SyntaxNodeAnalysisContext context, | ||
| MemberAccessExpressionSyntax resultAccess, | ||
| ISymbol taskSymbol, | ||
| SyntaxNode executableScope) | ||
| { | ||
| if (executableScope is not AnonymousFunctionExpressionSyntax anonymousFunction | ||
| || taskSymbol is not IParameterSymbol { ContainingSymbol: IMethodSymbol containingMethod, Ordinal: 0 } antecedentParameter | ||
| || !SymbolEqualityComparer.Default.Equals( | ||
| containingMethod, | ||
| context.SemanticModel.GetSymbolInfo(anonymousFunction, context.CancellationToken).Symbol) | ||
| || anonymousFunction.Parent is not ArgumentSyntax argument | ||
| || argument.Parent is not ArgumentListSyntax argumentList | ||
| || argumentList.Parent is not InvocationExpressionSyntax continueWithInvocation | ||
| || context.SemanticModel.GetSymbolInfo(continueWithInvocation, context.CancellationToken).Symbol is not IMethodSymbol continueWithMethod | ||
| || continueWithMethod.Name != nameof(System.Threading.Tasks.Task.ContinueWith) | ||
| || !Utils.IsTask(continueWithMethod.ContainingType)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (context.SemanticModel.GetOperation(argument, context.CancellationToken) is not IArgumentOperation argumentOperation | ||
| || argumentOperation.Parameter?.Type is not INamedTypeSymbol { TypeKind: TypeKind.Delegate } continuationDelegate | ||
| || continuationDelegate.DelegateInvokeMethod is not { Parameters.Length: > 0 } delegateInvokeMethod | ||
| || !SymbolEqualityComparer.Default.Equals(delegateInvokeMethod.Parameters[0].Type, antecedentParameter.Type)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return !MayWriteSymbol(context, executableScope, taskSymbol); | ||
| } | ||
|
|
||
| private static bool ConditionProvesSuccessfulCompletion(SyntaxNodeAnalysisContext context, ExpressionSyntax condition, ISymbol expectedTaskSymbol) | ||
| { | ||
| while (condition is ParenthesizedExpressionSyntax parenthesized) | ||
| { | ||
| condition = parenthesized.Expression; | ||
| } | ||
|
|
||
| if (condition is BinaryExpressionSyntax { RawKind: (int)SyntaxKind.LogicalAndExpression } logicalAnd) | ||
| { | ||
| return ConditionProvesSuccessfulCompletion(context, logicalAnd.Left, expectedTaskSymbol) | ||
| || ConditionProvesSuccessfulCompletion(context, logicalAnd.Right, expectedTaskSymbol); | ||
| } | ||
|
|
||
| if (condition is not MemberAccessExpressionSyntax { Name.Identifier.ValueText: "IsCompletedSuccessfully" } completionAccess) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| ISymbol? completionProperty = context.SemanticModel.GetSymbolInfo(completionAccess, context.CancellationToken).Symbol; | ||
| ISymbol? completedTaskSymbol = context.SemanticModel.GetSymbolInfo(completionAccess.Expression, context.CancellationToken).Symbol; | ||
| return completionProperty is IPropertySymbol { ContainingType: { } containingType } | ||
| && Utils.IsTask(containingType) | ||
| && SymbolEqualityComparer.Default.Equals(expectedTaskSymbol, completedTaskSymbol); | ||
| } | ||
|
|
||
| private static bool MayWriteSymbol(SyntaxNodeAnalysisContext context, SyntaxNode executableScope, ISymbol expectedTaskSymbol) | ||
| { | ||
| if (executableScope.DescendantNodes() | ||
| .OfType<RefExpressionSyntax>() | ||
| .Any()) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| foreach (SyntaxNode node in executableScope.DescendantNodes()) | ||
| { | ||
| ExpressionSyntax? writtenExpression = node switch | ||
| { | ||
| AssignmentExpressionSyntax assignment => assignment.Left, | ||
| PrefixUnaryExpressionSyntax prefix when prefix.IsKind(SyntaxKind.PreIncrementExpression) || prefix.IsKind(SyntaxKind.PreDecrementExpression) => prefix.Operand, | ||
| PostfixUnaryExpressionSyntax postfix when postfix.IsKind(SyntaxKind.PostIncrementExpression) || postfix.IsKind(SyntaxKind.PostDecrementExpression) => postfix.Operand, | ||
| ArgumentSyntax argument when argument.RefOrOutKeyword.IsKind(SyntaxKind.RefKeyword) || argument.RefOrOutKeyword.IsKind(SyntaxKind.OutKeyword) => argument.Expression, | ||
| _ => null, | ||
| }; | ||
|
|
||
| if (writtenExpression is not null && writtenExpression.DescendantNodesAndSelf() | ||
| .OfType<ExpressionSyntax>() | ||
| .Any(candidate => SymbolEqualityComparer.Default.Equals( | ||
| context.SemanticModel.GetSymbolInfo(candidate, context.CancellationToken).Symbol, | ||
| expectedTaskSymbol))) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static SyntaxNode GetExecutableScope(ExpressionSyntax expression) | ||
| => expression.Ancestors().FirstOrDefault(ancestor => | ||
| ancestor is AnonymousFunctionExpressionSyntax | ||
| or LocalFunctionStatementSyntax | ||
| or QueryExpressionSyntax | ||
| or BaseMethodDeclarationSyntax | ||
| or AccessorDeclarationSyntax) ?? expression; | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.