-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule: MA0134 Observe the result of awaitable methods (#509)
- Loading branch information
Showing
8 changed files
with
440 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,15 @@ | ||
# MA0134 - Observe result of async calls | ||
|
||
The result of awaitable method should be observed by using `await`, `Result`, `Wait`, or other methods. | ||
|
||
Note: [CS4014](https://learn.microsoft.com/en-US/dotnet/csharp/language-reference/compiler-messages/cs4014?WT.mc_id=DT-MVP-5003978) is similar but only operate in `async` methods. MA0134 operates in non-async methods. | ||
|
||
````c# | ||
void Sample() | ||
{ | ||
Task.Delay(1); // non-compliant | ||
Task.Delay(1).Wait(); // ok | ||
_ = Task.Delay(1); // ok | ||
} | ||
```` |
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
62 changes: 62 additions & 0 deletions
62
src/Meziantou.Analyzer/Rules/AwaitAwaitableMethodInSyncMethodAnalyzer.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,62 @@ | ||
using System.Collections.Immutable; | ||
using Meziantou.Analyzer.Internals; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Meziantou.Analyzer.Rules; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class AwaitAwaitableMethodInSyncMethodAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor s_rule = new( | ||
RuleIdentifiers.AwaitAwaitableMethodInSyncMethod, | ||
title: "Observe result of async calls", | ||
messageFormat: "Observe result of async calls", | ||
RuleCategories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.AwaitAwaitableMethodInSyncMethod)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterCompilationStartAction(context => | ||
{ | ||
var awaitableTypes = new AwaitableTypes(context.Compilation); | ||
context.RegisterSymbolStartAction(context => | ||
{ | ||
if (context.Symbol is IMethodSymbol method && (method.IsAsync || method.IsTopLevelStatementsEntryPointMethod())) | ||
return; // Already handled by CS4014 | ||
|
||
context.RegisterOperationAction(context => AnalyzeOperation(context, awaitableTypes), OperationKind.Invocation); | ||
}, SymbolKind.Method); | ||
}); | ||
} | ||
|
||
private static void AnalyzeOperation(OperationAnalysisContext context, AwaitableTypes awaitableTypes) | ||
{ | ||
var operation = (IInvocationOperation)context.Operation; | ||
|
||
var parent = operation.Parent; | ||
if (parent is null or IBlockOperation or IExpressionStatementOperation or IConditionalAccessOperation) | ||
{ | ||
var semanticModel = operation.SemanticModel!; | ||
var position = operation.Syntax.GetLocation().SourceSpan.End; | ||
|
||
// While there is a check in RegisterSymbolStartAction, this is needed to handle lambda and delegates | ||
var enclosingSymbol = semanticModel.GetEnclosingSymbol(position, context.CancellationToken); | ||
if (enclosingSymbol is IMethodSymbol method && (method.IsAsync || method.IsTopLevelStatementsEntryPointMethod())) | ||
return; | ||
|
||
if (!awaitableTypes.IsAwaitable(operation.Type, semanticModel, position)) | ||
return; | ||
|
||
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
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
Oops, something went wrong.