-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: MA0147 - Avoid async void method for delegate (#659)
- Loading branch information
Showing
8 changed files
with
211 additions
and
5 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,9 @@ | ||
# MA0147 - Avoid async void method for delegate | ||
|
||
````c# | ||
Foo(() => {}); // ok | ||
Foo(async () => {}); // non-compliant as the delegate is not expecting an async method | ||
void Foo(System.Action action) => throw null; | ||
```` |
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
50 changes: 50 additions & 0 deletions
50
src/Meziantou.Analyzer/Rules/DoNotUseAsyncDelegateForSyncDelegateAnalyzer.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,50 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Meziantou.Analyzer.Rules; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class DoNotUseAsyncDelegateForSyncDelegateAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor s_rule = new( | ||
RuleIdentifiers.DoNotUseAsyncDelegateForSyncDelegate, | ||
title: "Avoid async void method for delegate", | ||
messageFormat: "Avoid async void method for delegate", | ||
RuleCategories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: false, | ||
description: "", | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.DoNotUseAsyncDelegateForSyncDelegate)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterOperationAction(AnalyzerDelegateCreationOperation, OperationKind.DelegateCreation); | ||
} | ||
|
||
private static void AnalyzerDelegateCreationOperation(OperationAnalysisContext context) | ||
{ | ||
var operation = (IDelegateCreationOperation)context.Operation; | ||
if (operation.Type is INamedTypeSymbol { DelegateInvokeMethod: IMethodSymbol delegateInvokeMethod }) | ||
{ | ||
if (!delegateInvokeMethod.ReturnsVoid) | ||
return; | ||
} | ||
else | ||
{ | ||
// Cannot determine the delegate type | ||
return; | ||
} | ||
|
||
if (operation.Target is IAnonymousFunctionOperation { Symbol: { IsAsync: true } }) | ||
{ | ||
context.ReportDiagnostic(s_rule, operation); | ||
} | ||
} | ||
} |
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
123 changes: 123 additions & 0 deletions
123
tests/Meziantou.Analyzer.Test/Rules/DoNotUseAsyncDelegateForSyncDelegateAnalyzerTests.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,123 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Rules; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Rules; | ||
|
||
public sealed class DoNotUseAsyncDelegateForSyncDelegateAnalyzerTests | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
{ | ||
return new ProjectBuilder() | ||
.WithAnalyzer<DoNotUseAsyncDelegateForSyncDelegateAnalyzer>() | ||
.WithTargetFramework(TargetFramework.Net8_0) | ||
.WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication); | ||
} | ||
|
||
[Fact] | ||
public async Task List_ForEach_Sync() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
new System.Collections.Generic.List<int>().ForEach(item => {}); | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task List_ForEach_Async() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
new System.Collections.Generic.List<int>().ForEach([|async item => {}|]); | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task CustomDelegate_Sync() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
A(() => {}); | ||
|
||
void A(D a) => throw null; | ||
delegate void D(); | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task CustomDelegate_AsyncVoid() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
A([|async () => {}|]); | ||
|
||
void A(D a) => throw null; | ||
delegate void D(); | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Action_Sync() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
A(() => {}); | ||
|
||
void A(System.Action a) => throw null; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task Action_AsyncVoid() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
A([|async () => {}|]); | ||
|
||
void A(System.Action a) => throw null; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task FuncTask_AsyncDelegate() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
A(async () => {}); | ||
|
||
void A(System.Func<System.Threading.Tasks.Task> a) => throw null; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task FuncValueTask_AsyncDelegate() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
A(async () => {}); | ||
|
||
void A(System.Func<System.Threading.Tasks.ValueTask> a) => throw null; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task FuncValueTaskOfInt_AsyncDelegate() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
A(async () => 1); | ||
|
||
void A(System.Func<System.Threading.Tasks.ValueTask<int>> a) => throw null; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
} |