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 @@ -105,16 +105,27 @@ private static async Task<Document> ReplaceWithAwaitUsing(Document document, Syn
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
if (nodeToFix is UsingStatementSyntax usingStatement)
{
editor.ReplaceNode(usingStatement, usingStatement.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword)));
var awaitKeyword = GetAwaitKeyword(usingStatement.UsingKeyword);
editor.ReplaceNode(
usingStatement,
usingStatement.WithUsingKeyword(usingStatement.UsingKeyword.WithLeadingTrivia(SyntaxFactory.TriviaList())).WithAwaitKeyword(awaitKeyword));
}
else if (nodeToFix is LocalDeclarationStatementSyntax localDeclarationStatement)
{
editor.ReplaceNode(localDeclarationStatement, localDeclarationStatement.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword)));
var awaitKeyword = GetAwaitKeyword(localDeclarationStatement.UsingKeyword);
editor.ReplaceNode(
localDeclarationStatement,
localDeclarationStatement.WithUsingKeyword(localDeclarationStatement.UsingKeyword.WithLeadingTrivia(SyntaxFactory.TriviaList())).WithAwaitKeyword(awaitKeyword));
}

return editor.GetChangedDocument();
}

private static SyntaxToken GetAwaitKeyword(SyntaxToken usingKeyword)
{
return SyntaxFactory.Token(usingKeyword.LeadingTrivia, SyntaxKind.AwaitKeyword, SyntaxFactory.TriviaList(SyntaxFactory.Space));
}

private static async Task<Document> ReplaceWithMethodName(Document document, SyntaxNode nodeToFix, string methodName, CancellationToken cancellationToken)
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,51 @@ private class Sample : IDisposable
.ValidateAsync();
}

[Fact]
public async Task Using_Diagnostic1_WithComment()
{
await CreateProjectBuilder()
.WithSourceCode("""
using System;
using System.Threading.Tasks;

class Test
{
public async Task A()
{
// MA0042 "Prefer using 'await using'"
[|using var a = new Sample();|]
}

private class Sample : IDisposable
{
public void Dispose() => throw null;
public ValueTask DisposeAsync() => throw null;
}
}
""")
.ShouldBatchFixCodeWith("""
using System;
using System.Threading.Tasks;

class Test
{
public async Task A()
{
// MA0042 "Prefer using 'await using'"
await using var a = new Sample();
}

private class Sample : IDisposable
{
public void Dispose() => throw null;
public ValueTask DisposeAsync() => throw null;
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task Using_Diagnostic2()
{
Expand Down
Loading