-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule (disabled by default) to detect async void methods
- Loading branch information
Showing
5 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# MA0155 - Do not use async void methods | ||
|
||
```c# | ||
// not-compliant | ||
async void SomeMethod() | ||
{ | ||
await Task.Delay(1000); | ||
} | ||
|
||
// ok | ||
async Task SomeMethod() | ||
{ | ||
await Task.Delay(1000); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Meziantou.Analyzer.Rules; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class DoNotUseAsyncVoidAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor Rule = new( | ||
RuleIdentifiers.DoNotUseAsyncVoid, | ||
title: "Do not use async void methods", | ||
messageFormat: "Do not use async void methods", | ||
RuleCategories.Design, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: false, | ||
description: "", | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.DoNotUseAsyncVoid)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Method); | ||
} | ||
|
||
private static void AnalyzeSymbol(SymbolAnalysisContext context) | ||
{ | ||
var symbol = (IMethodSymbol)context.Symbol; | ||
if (symbol is { ReturnsVoid: true, IsAsync: true }) | ||
{ | ||
context.ReportDiagnostic(Rule, symbol); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
tests/Meziantou.Analyzer.Test/Rules/DoNotUseAsyncVoidAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Rules; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Rules; | ||
public sealed class DoNotUseAsyncVoidAnalyzerTests | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
{ | ||
return new ProjectBuilder() | ||
.WithAnalyzer<DoNotUseAsyncVoidAnalyzer>() | ||
.WithTargetFramework(TargetFramework.Net8_0); | ||
} | ||
|
||
[Fact] | ||
public async Task Method_Void() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
class Sample | ||
{ | ||
void A() => throw null; | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Method_AsyncVoid() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
class Sample | ||
{ | ||
async void [|A|]() => throw null; | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Method_AsyncTask() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
class Sample | ||
{ | ||
async System.Threading.Tasks.Task A() => throw null; | ||
} | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
} |