Skip to content

Commit

Permalink
Fixes microsoft#849. AnalyzeAwaitedOrReturnedExpression now detects i…
Browse files Browse the repository at this point in the history
…f the invocation is wrapped inside parentheses.
  • Loading branch information
bluetarpmedia committed Jun 15, 2021
1 parent 94e1337 commit 0add87c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
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

0 comments on commit 0add87c

Please sign in to comment.