Skip to content

Commit 5494576

Browse files
committed
async void does not allow
1 parent 94e0b1c commit 5494576

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/ConsoleAppFramework/DiagnosticDescriptors.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,8 @@ public static DiagnosticDescriptor Create(int id, string title, string messageFo
118118
public static DiagnosticDescriptor DocCommentParameterNameNotMatched { get; } = Create(
119119
15,
120120
"Document Comment parameter name '{0}' does not match method parameter name.");
121+
122+
public static DiagnosticDescriptor ReturnTypeMethodAsyncVoid { get; } = Create(
123+
16,
124+
"Command method return type does not allow async void.");
121125
}

src/ConsoleAppFramework/Parser.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,18 @@ internal class Parser(ConsoleAppFrameworkGeneratorOptions generatorOptions, Diag
414414
if (methodSymbol.ReturnType.SpecialType == SpecialType.System_Void)
415415
{
416416
isVoid = true;
417+
418+
// check `async void`
419+
var syntax = methodSymbol.DeclaringSyntaxReferences[0].GetSyntax() as MethodDeclarationSyntax;
420+
if (syntax != null)
421+
{
422+
var asyncKeyword = syntax.Modifiers.FirstOrDefault(x => x.IsKind(SyntaxKind.AsyncKeyword));
423+
if (asyncKeyword != default)
424+
{
425+
context.ReportDiagnostic(DiagnosticDescriptors.ReturnTypeMethodAsyncVoid, asyncKeyword.GetLocation());
426+
return null;
427+
}
428+
}
417429
}
418430
else if (methodSymbol.ReturnType.SpecialType == SpecialType.System_Int32)
419431
{

tests/ConsoleAppFramework.GeneratorTests/DiagnosticsTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,24 @@ public void Bar(string msg)
456456
""", "Bar");
457457

458458
}
459+
460+
[Fact]
461+
public void AsyncVoid()
462+
{
463+
verifier.Verify(16, """
464+
var app = ConsoleApp.Create();
465+
app.Add<MyCommands2>();
466+
app.Run(args);
467+
468+
public class MyCommands2
469+
{
470+
public async void Foo()
471+
{
472+
await Task.Yield();
473+
}
474+
}
475+
476+
""", "async");
477+
}
459478
}
460479

0 commit comments

Comments
 (0)