Skip to content

Commit

Permalink
Fix EC88
Browse files Browse the repository at this point in the history
  • Loading branch information
Vianney de Bellabre committed May 19, 2024
1 parent e4b4fb8 commit da50914
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
CodeAction.Create(
title: "Dispose resource asynchronously",
createChangedDocument: async token =>
await context.Document.GetSyntaxRootAsync(token) is { } root
await context.Document.GetSyntaxRootAsync(token).ConfigureAwait(false) is { } root
? context.Document.WithSyntaxRoot(root.ReplaceNode(usingStatement, usingStatement
.WithoutLeadingTrivia() // Needs to be removed then re-added to keep everything ordered
.WithoutTrailingTrivia() // Needs to be removed then re-added to keep everything ordered
.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))
.WithLeadingTriviaIfDifferent(usingStatement.GetLeadingTrivia())
.WithTrailingTriviaIfDifferent(usingStatement.GetTrailingTrivia())))
.WithLeadingTrivia(usingStatement.GetLeadingTrivia())
.WithTrailingTrivia(usingStatement.GetTrailingTrivia())))
: context.Document,
equivalenceKey: "Dispose resource asynchronously"),
diagnostic);
Expand All @@ -53,8 +53,8 @@ await context.Document.GetSyntaxRootAsync(token).ConfigureAwait(false) is { } ro
.WithoutLeadingTrivia() // Needs to be removed then re-added to keep everything ordered
.WithoutTrailingTrivia() // Needs to be removed then re-added to keep everything ordered
.WithAwaitKeyword(SyntaxFactory.Token(SyntaxKind.AwaitKeyword))
.WithLeadingTriviaIfDifferent(usingDeclaration.GetLeadingTrivia())
.WithTrailingTriviaIfDifferent(usingDeclaration.GetTrailingTrivia())))
.WithLeadingTrivia(usingDeclaration.GetLeadingTrivia())
.WithTrailingTrivia(usingDeclaration.GetTrailingTrivia())))
: context.Document,
equivalenceKey: "Dispose resource asynchronously"),
diagnostic);
Expand Down
53 changes: 21 additions & 32 deletions src/EcoCode.Tests/Tests/EC88.DisposeResourceAsynchronously.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static async Task Run()
""");

[TestMethod]
public Task WarnOnAsyncableUsingsInAsyncMethodAsync() => VerifyAsync("""
public Task WarnOnAsyncableUsingsInAsyncMethod1Async() => VerifyAsync("""
using System;
using System.Threading.Tasks;
public static class Test
Expand All @@ -65,6 +65,17 @@ public static async Task Run()
// Leading trivia
[|using|] var d2 = new AsyncDisposableClass(); // Trailing trivia
Console.WriteLine(d2);
// More tests to make sure everything stays in place
Console.WriteLine();
// Leading trivia
[|using|] (var d3 = new AsyncDisposableClass()) // Trailing trivia
{
Console.WriteLine(d3);
Console.WriteLine(d3);
Console.WriteLine(d3);
}
}
}
""", """
Expand All @@ -84,39 +95,17 @@ public static async Task Run()
// Leading trivia
await using var d2 = new AsyncDisposableClass(); // Trailing trivia
Console.WriteLine(d2);
}
}
""");

[TestMethod]
public Task WarnOnAsyncableUsingsInAsyncMethod2Async() => VerifyAsync("""
using System;
using System.Threading.Tasks;
public static class Test
{
private class DisposableClass : IDisposable { public void Dispose() { } }
private sealed class AsyncDisposableClass : DisposableClass, IAsyncDisposable { public ValueTask DisposeAsync() => default; }
public static async Task Run()
{
// Leading trivia
[|using|] var d2 = new AsyncDisposableClass(); // Trailing trivia
Console.WriteLine(d2);
}
}
""", """
using System;
using System.Threading.Tasks;
public static class Test
{
private class DisposableClass : IDisposable { public void Dispose() { } }
private sealed class AsyncDisposableClass : DisposableClass, IAsyncDisposable { public ValueTask DisposeAsync() => default; }
public static async Task Run()
{
// More tests to make sure everything stays in place
Console.WriteLine();
// Leading trivia
await using var d2 = new AsyncDisposableClass(); // Trailing trivia
Console.WriteLine(d2);
await using (var d3 = new AsyncDisposableClass()) // Trailing trivia
{
Console.WriteLine(d3);
Console.WriteLine(d3);
Console.WriteLine(d3);
}
}
}
""");
Expand Down

0 comments on commit da50914

Please sign in to comment.