Skip to content

Commit

Permalink
Fix handling of nested 'await' expressions during VSTHRD103 code fix
Browse files Browse the repository at this point in the history
Fixes #329
  • Loading branch information
sharwell committed Aug 7, 2018
1 parent 536bbd5 commit ad635ee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,36 @@ static class Assert {
this.VerifyCSharpFix(test, withFix);
}

[Fact]
public void TaskOfTResultInTaskReturningMethodGeneratesWarning_FixRewritesCorrectExpression()
{
var test = @"
using System;
using System.Threading.Tasks;
class Test {
async Task T() {
await Task.Run(() => Console.Error).Result.WriteLineAsync();
}
}
";

var withFix = @"
using System;
using System.Threading.Tasks;
class Test {
async Task T() {
await (await Task.Run(() => Console.Error)).WriteLineAsync();
}
}
";

this.expect.Locations = new[] { new DiagnosticResultLocation("Test0.cs", 7, 45) };
this.VerifyCSharpDiagnostic(test, this.expect);
this.VerifyCSharpFix(test, withFix);
}

[Fact]
public void TaskOfTResultInTaskReturningAnonymousMethodWithinSyncMethod_GeneratesWarning()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ protected override async Task<Solution> GetChangedSolutionAsync(CancellationToke
awaitExpression = SyntaxFactory.AwaitExpression(
syncExpression.ReplaceNode(syncMethodName, asyncMethodName).WithoutLeadingTrivia())
.WithLeadingTrivia(syncExpression.GetLeadingTrivia());
if (!(syncExpression.Parent is ExpressionStatementSyntax))
{
awaitExpression = SyntaxFactory.ParenthesizedExpression(awaitExpression)
.WithAdditionalAnnotations(Simplifier.Annotation);
}
}
else
{
Expand All @@ -193,6 +188,12 @@ protected override async Task<Solution> GetChangedSolutionAsync(CancellationToke
.WithLeadingTrivia(syncMemberStrippedExpression.GetLeadingTrivia());
}

if (!(syncExpression.Parent is ExpressionStatementSyntax))
{
awaitExpression = SyntaxFactory.ParenthesizedExpression(awaitExpression)
.WithAdditionalAnnotations(Simplifier.Annotation);
}

updatedMethod = updatedMethod
.ReplaceNode(syncExpression, awaitExpression);

Expand Down

0 comments on commit ad635ee

Please sign in to comment.