Skip to content
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 @@ -15,16 +15,11 @@ public override FixAllProvider GetFixAllProvider()
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var nodeToFix = root?.FindNode(context.Span, getInnermostNodeForTie: false);
// The statement has the same span as the await expression when the semicolon is missing, and the argument when it is an argument: F(await task)
var nodeToFix = root?.FindNode(context.Span, getInnermostNodeForTie: true);
if (nodeToFix is null)
return;

// The argument has the same span as the awaited expression: F(await task)
if (nodeToFix is ArgumentSyntax argument)
{
nodeToFix = argument.Expression;
}

context.RegisterCodeFix(
CodeAction.Create(
"Use ConfigureAwait(false)",
Expand Down Expand Up @@ -90,10 +85,12 @@ private static async Task<Document> AddConfigureAwait(CodeFixContext context, Sy
{
if (awaitSyntax.Expression is not null)
{
var newExpression = (ExpressionSyntax)generator.InvocationExpression(
generator.MemberAccessExpression(awaitSyntax.Expression.Parenthesize(), nameof(Task.ConfigureAwait)),
// The trailing trivia of the expression is the end of the line when the semicolon is missing
var newExpression = ((ExpressionSyntax)generator.InvocationExpression(
generator.MemberAccessExpression(awaitSyntax.Expression.WithoutTrailingTrivia().Parenthesize(), nameof(Task.ConfigureAwait)),
generator.LiteralExpression(value))
.Parenthesize();
.Parenthesize())
.WithTrailingTrivia(awaitSyntax.Expression.GetTrailingTrivia());

var newInvokeExpression = awaitSyntax.WithExpression(newExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1404,4 +1404,35 @@ void B(int value) { }

return test.RunAsync();
}

[Fact]
[Trait("Issue", "https://github.com/meziantou/Meziantou.Analyzer/issues/1676")]
public Task InvalidCode_MissingSemicolon()
{
var test = CreateTest();
// The code does not compile, so the compiler diagnostics cannot be verified
test.CompilerDiagnostics = CompilerDiagnostics.None;
test.TestCode = """
using System.Threading.Tasks;
class ClassTest
{
async Task Test()
{
{|MA0004:await Task.Delay(1)|}
}
}
""";
test.FixedCode = """
using System.Threading.Tasks;
class ClassTest
{
async Task Test()
{
await Task.Delay(1).ConfigureAwait(false)
}
}
""";

return test.RunAsync();
}
}
Loading