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
14 changes: 13 additions & 1 deletion src/Meziantou.Analyzer/Rules/DoNotUseAsyncVoidAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Immutable;
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace Meziantou.Analyzer.Rules;

Expand All @@ -25,6 +27,16 @@ public override void Initialize(AnalysisContext context)
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Method);
context.RegisterOperationAction(AnalyzeLocalFunction, OperationKind.LocalFunction);
}

private void AnalyzeLocalFunction(OperationAnalysisContext context)
{
var operation = (ILocalFunctionOperation)context.Operation;
if (operation.Symbol is { ReturnsVoid: true, IsAsync: true })
{
context.ReportDiagnostic(Rule, operation);
}
}

private static void AnalyzeSymbol(SymbolAnalysisContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,52 @@ class Sample
.ValidateAsync();
}

[Fact]
public async Task LocalFunction_Void()
{
await CreateProjectBuilder()
.WithSourceCode("""
class Sample
{
void A()
{
void Local() => throw null;
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task LocalFunction_AsyncVoid()
{
await CreateProjectBuilder()
.WithSourceCode("""
class Sample
{
void A()
{
[|async void Local() => throw null;|]
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task LocalFunction_AsyncTask()
{
await CreateProjectBuilder()
.WithSourceCode("""
class Sample
{
void A()
{
async System.Threading.Tasks.Task Local() => throw null;
}
}
""")
.ValidateAsync();
}

}