Skip to content
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

Emit VSTHRD003 warning when awaiting on foreign tasks from properties #1244

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(Utils.DebuggableWrapper(this.AnalyzeLambdaExpression), SyntaxKind.ParenthesizedLambdaExpression);
}

private static bool IsSymbolAlwaysOkToAwait(ISymbol? symbol)
{
if (symbol is IFieldSymbol field)
{
// Allow the TplExtensions.CompletedTask and related fields.
if (field.ContainingType.Name == Types.TplExtensions.TypeName && field.BelongsToNamespace(Types.TplExtensions.Namespace) &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both this and the conditional on 88 are a little bit difficult to grok based on initial read. Consider making a method with a descriptive name to make it easier to understand at a glance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it is. I have a code comment above that explains it, and this is the method that keeps things simple for the caller. :)

(field.Name == Types.TplExtensions.CompletedTask || field.Name == Types.TplExtensions.CanceledTask || field.Name == Types.TplExtensions.TrueTask || field.Name == Types.TplExtensions.FalseTask))
{
return true;
}
}
else if (symbol is IPropertySymbol property)
{
// Explicitly allow Task.CompletedTask
if (property.ContainingType.Name == Types.Task.TypeName && property.BelongsToNamespace(Types.Task.Namespace) &&
property.Name == Types.Task.CompletedTask)
{
return true;
}
}

return false;
}

private void AnalyzeArrowExpressionClause(SyntaxNodeAnalysisContext context)
{
var arrowExpressionClause = (ArrowExpressionClauseSyntax)context.Node;
Expand Down Expand Up @@ -156,6 +180,9 @@ private void AnalyzeAwaitExpression(SyntaxNodeAnalysisContext context)
symbolType = localSymbol.Type;
dataflowAnalysisCompatibleVariable = true;
break;
case IPropertySymbol propertySymbol when !IsSymbolAlwaysOkToAwait(propertySymbol):
symbolType = propertySymbol.Type;
break;
case IParameterSymbol parameterSymbol:
symbolType = parameterSymbol.Type;
dataflowAnalysisCompatibleVariable = true;
Expand Down Expand Up @@ -209,23 +236,9 @@ private void AnalyzeAwaitExpression(SyntaxNodeAnalysisContext context)
}

ISymbol? definition = declarationSemanticModel.GetSymbolInfo(memberAccessSyntax, cancellationToken).Symbol;
if (definition is IFieldSymbol field)
{
// Allow the TplExtensions.CompletedTask and related fields.
if (field.ContainingType.Name == Types.TplExtensions.TypeName && field.BelongsToNamespace(Types.TplExtensions.Namespace) &&
(field.Name == Types.TplExtensions.CompletedTask || field.Name == Types.TplExtensions.CanceledTask || field.Name == Types.TplExtensions.TrueTask || field.Name == Types.TplExtensions.FalseTask))
{
return null;
}
}
else if (definition is IPropertySymbol property)
if (IsSymbolAlwaysOkToAwait(definition))
{
// Explicitly allow Task.CompletedTask
if (property.ContainingType.Name == Types.Task.TypeName && property.BelongsToNamespace(Types.Task.Namespace) &&
property.Name == Types.Task.CompletedTask)
{
return null;
}
return null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,62 @@ async Task FooAsync() {
await CSVerify.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task ReportWarningWhenAwaitingTaskReturningProperty()
{
var test = @"
using System.Threading.Tasks;

class Tests
{
async Task GetTask(TaskCompletionSource<int> tcs)
{
await [|tcs.Task|];
}
}
";
await CSVerify.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task DoNotReportWarningWhenAwaitingTaskPropertyThatWasSetInContext()
{
var test = @"
using System.Threading.Tasks;

class Tests
{
private Task MyTaskProperty { get; set; }

async Task GetTask()
{
this.MyTaskProperty = Task.Run(() => {});
await this.MyTaskProperty;
}
}
";
await CSVerify.VerifyAnalyzerAsync(test);
}

[Fact]
public async Task ReportWarningWhenAwaitingTaskPropertyThatWasNotSetInContext()
{
var test = @"
using System.Threading.Tasks;

class Tests
{
private Task MyTaskProperty { get; set; } = Task.Run(() => {});

async Task GetTask()
{
await [|this.MyTaskProperty|];
}
}
";
await CSVerify.VerifyAnalyzerAsync(test);
}

private DiagnosticResult CreateDiagnostic(int line, int column, int length) =>
CSVerify.Diagnostic().WithSpan(line, column, line, column + length);
}