Skip to content

Commit

Permalink
Fixer for MA0042 adds parentheses if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Dec 26, 2024
1 parent aa97561 commit 764a0a8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis;

namespace Meziantou.Analyzer.Internals;
internal static class ParenthesesExtensions
{
public static SyntaxNode Parentheses(this SyntaxNode syntaxNode)
{
if (syntaxNode is ExpressionSyntax expression)
{
return SyntaxFactory.ParenthesizedExpression(expression).WithAdditionalAnnotations(Simplifier.Annotation);
}

return syntaxNode;
}

public static ParenthesizedExpressionSyntax Parentheses<T>(this ExpressionSyntax expression)
{
return SyntaxFactory.ParenthesizedExpression(expression).WithAdditionalAnnotations(Simplifier.Annotation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,63 +34,63 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
switch (data)
{
case DoNotUseBlockingCallInAsyncContextData.Thread_Sleep:
{
var codeAction = CodeAction.Create(
"Use Task.Delay",
ct => UseTaskDelay(context.Document, nodeToFix, ct),
equivalenceKey: "Thread_Sleep");
{

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 37 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
var codeAction = CodeAction.Create(

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 38 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
"Use Task.Delay",

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 39 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
ct => UseTaskDelay(context.Document, nodeToFix, ct),

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 40 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
equivalenceKey: "Thread_Sleep");

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 41 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}
context.RegisterCodeFix(codeAction, context.Diagnostics);

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 43 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
break;

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 44 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
}

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 45 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

case DoNotUseBlockingCallInAsyncContextData.Task_Wait:
{
var codeAction = CodeAction.Create(
"Use await",
ct => ReplaceTaskWaitWithAwait(context.Document, nodeToFix, ct),
equivalenceKey: "Task_Wait");
{

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 48 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
var codeAction = CodeAction.Create(

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / check_documentation

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.6)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.4)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn4.2)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, default)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, Release, roslyn3.8)

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 49 in src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseBlockingCallInAsyncContextFixer.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
"Use await",
ct => ReplaceTaskWaitWithAwait(context.Document, nodeToFix, ct),
equivalenceKey: "Task_Wait");

context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}
context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}

case DoNotUseBlockingCallInAsyncContextData.Task_Result:
{
var codeAction = CodeAction.Create(
"Use await",
ct => ReplaceTaskResultWithAwait(context.Document, nodeToFix, ct),
equivalenceKey: "Task_Result");
{
var codeAction = CodeAction.Create(
"Use await",
ct => ReplaceTaskResultWithAwait(context.Document, nodeToFix, ct),
equivalenceKey: "Task_Result");

context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}
context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}

case DoNotUseBlockingCallInAsyncContextData.Overload:
{
if (!properties.TryGetValue("MethodName", out var methodName) || methodName is null)
return;
{
if (!properties.TryGetValue("MethodName", out var methodName) || methodName is null)
return;

var codeAction = CodeAction.Create(
$"Use '{methodName}'",
ct => ReplaceWithMethodName(context.Document, nodeToFix, methodName, ct),
equivalenceKey: "Overload");
var codeAction = CodeAction.Create(
$"Use '{methodName}'",
ct => ReplaceWithMethodName(context.Document, nodeToFix, methodName, ct),
equivalenceKey: "Overload");

context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}
context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}

case DoNotUseBlockingCallInAsyncContextData.Using:
case DoNotUseBlockingCallInAsyncContextData.UsingDeclarator:
{
var codeAction = CodeAction.Create(
$"Use 'await using'",
ct => ReplaceWithAwaitUsing(context.Document, nodeToFix, ct),
equivalenceKey: "Overload");

context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}
{
var codeAction = CodeAction.Create(
$"Use 'await using'",
ct => ReplaceWithAwaitUsing(context.Document, nodeToFix, ct),
equivalenceKey: "Overload");

context.RegisterCodeFix(codeAction, context.Diagnostics);
break;
}
}
}

Expand All @@ -115,11 +115,18 @@ private static async Task<Document> ReplaceWithMethodName(Document document, Syn
var generator = editor.Generator;

var invocation = (InvocationExpressionSyntax)nodeToFix;
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
var nodeToReplace = invocation.Expression switch
{
MemberAccessExpressionSyntax memberAccess => memberAccess.Name,
IdentifierNameSyntax identifier => identifier,
_ => null,
};

if (nodeToReplace is null)
return document;

var newNode = nodeToFix.ReplaceNode(memberAccess.Name, generator.IdentifierName(methodName));
var newExpression = generator.AwaitExpression(newNode);
var newNode = nodeToFix.ReplaceNode(nodeToReplace, generator.IdentifierName(methodName));
var newExpression = generator.AwaitExpression(newNode).Parentheses();
editor.ReplaceNode(nodeToFix, newExpression);

return editor.GetChangedDocument();
Expand All @@ -134,7 +141,7 @@ private static async Task<Document> ReplaceTaskResultWithAwait(Document document
if (expr is null)
return document;

var newExpression = generator.AwaitExpression(expr);
var newExpression = generator.AwaitExpression(expr).Parentheses();
editor.ReplaceNode(nodeToFix, newExpression);

return editor.GetChangedDocument();
Expand All @@ -150,7 +157,7 @@ private static async Task<Document> ReplaceTaskWaitWithAwait(Document document,
if (expr is null)
return document;

var newExpression = generator.AwaitExpression(expr);
var newExpression = generator.AwaitExpression(expr).Parentheses();
editor.ReplaceNode(nodeToFix, newExpression);

return editor.GetChangedDocument();
Expand All @@ -168,7 +175,7 @@ private static async Task<Document> UseTaskDelay(Document document, SyntaxNode n
var invocation = (InvocationExpressionSyntax)nodeToFix;
var delay = invocation.ArgumentList.Arguments[0].Expression;

var newExpression = generator.AwaitExpression(generator.InvocationExpression(generator.MemberAccessExpression(generator.TypeExpression(taskSymbol), "Delay"), delay));
var newExpression = generator.AwaitExpression(generator.InvocationExpression(generator.MemberAccessExpression(generator.TypeExpression(taskSymbol), "Delay"), delay)).Parentheses();
editor.ReplaceNode(nodeToFix, newExpression);
return editor.GetChangedDocument();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ public async Task A()
}")
.ValidateAsync();
}

[Fact]
public async Task FixerShouldAddParentheses()
{
await CreateProjectBuilder()
.WithSourceCode("""
using System.Threading.Tasks;
class Test
{
public async Task A()
{
_ = [||]Write().Length;
}
public string Write() => throw null;
public Task<string> WriteAsync() => throw null;
}
""")
.ShouldFixCodeWith("""
using System.Threading.Tasks;
class Test
{
public async Task A()
{
_ = (await WriteAsync()).Length;
}
public string Write() => throw null;
public Task<string> WriteAsync() => throw null;
}
""")
.ValidateAsync();
}

[Fact]
public async Task Async_Wait_Int32_Diagnostic()
Expand Down

0 comments on commit 764a0a8

Please sign in to comment.