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

Better handling of parenthesized expressions in VSTHRD003 #865

Merged
merged 1 commit into from Jun 22, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -134,6 +134,14 @@ private void AnalyzeAwaitExpression(SyntaxNodeAnalysisContext context)
SymbolInfo symbolToConsider = semanticModel.GetSymbolInfo(expressionSyntax, cancellationToken);
if (CommonInterest.TaskConfigureAwait.Any(configureAwait => configureAwait.IsMatch(symbolToConsider.Symbol)))
{
// If the invocation is wrapped inside parentheses then drill down to get the invocation.
while (expressionSyntax is ParenthesizedExpressionSyntax parenthesizedExprSyntax)
{
expressionSyntax = parenthesizedExprSyntax.Expression;
}

Debug.Assert(expressionSyntax is InvocationExpressionSyntax, "expressionSyntax should be an invocation");

if (((InvocationExpressionSyntax)expressionSyntax).Expression is MemberAccessExpressionSyntax memberAccessExpression)
{
symbolToConsider = semanticModel.GetSymbolInfo(memberAccessExpression.Expression, cancellationToken);
Expand Down
Expand Up @@ -27,10 +27,12 @@ class Tests
public void Test()
{
JoinableTaskFactory jtf = ThreadHelper.JoinableTaskFactory;
System.Threading.Tasks.Task task = SomeOperationAsync();
System.Threading.Tasks.Task task1 = SomeOperationAsync();
System.Threading.Tasks.Task task2 = SomeOperationAsync();
jtf.Run(async delegate
{
await task;
await task1;
await (task2); // Bug 849
});
}

Expand All @@ -42,7 +44,11 @@ public async Task<int> SomeOperationAsync()
}
}
";
DiagnosticResult expected = Verify.Diagnostic().WithLocation(14, 19);
DiagnosticResult[] expected =
{
Verify.Diagnostic().WithLocation(15, 19),
Verify.Diagnostic().WithLocation(16, 19),
};
await Verify.VerifyAnalyzerAsync(test, expected);
}

Expand Down Expand Up @@ -1224,6 +1230,28 @@ static Task<bool> MyMethodAsync()
await test.RunAsync();
}

[Fact]
public async Task DoNotReportWarningWithParenthesizedAwaitExpressions()
{
// This is a test for bug 849. Parenthesized expressions caused an InvalidCastException.
var test = @"
using System.Threading.Tasks;

class Test {
async Task FooAsync() {
await (Task.Delay(1));
await ((Task.Delay(1)));
await (((Task.Delay(1))));

await (Task.Delay(1).ConfigureAwait(false));
await ((Task.Delay(1).ConfigureAwait(false)));
await (((Task.Delay(1).ConfigureAwait(false))));
}
}
";
await Verify.VerifyAnalyzerAsync(test);
}

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