-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MA0152: Use Unwrap instead of using await twice
- Loading branch information
Showing
6 changed files
with
237 additions
and
9 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,10 @@ | ||
# MA0152 - Use Unwrap instead of using await twice | ||
|
||
Prefer using [`Unwrap`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskextensions.unwrap?view=net-8.0&WT.mc_id=DT-MVP-5003978) instead of using `await` twice | ||
|
||
````c# | ||
Task<Task> t; | ||
await await t; // non-compliant | ||
await t.Unwrap(); // compliant | ||
```` |
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,100 @@ | ||
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 UseTaskUnwrapAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor Rule = new( | ||
RuleIdentifiers.UseTaskUnwrap, | ||
title: "Use Unwrap instead of double await", | ||
messageFormat: "Use Unwrap instead of double await", | ||
RuleCategories.Performance, | ||
DiagnosticSeverity.Info, | ||
isEnabledByDefault: true, | ||
description: "", | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.UseTaskUnwrap)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
||
context.RegisterCompilationStartAction(context => | ||
{ | ||
var ctx = new AnalyzerContext(context.Compilation); | ||
if (!ctx.IsValid) | ||
return; | ||
|
||
context.RegisterOperationAction(ctx.AnalyzeAwait, OperationKind.Await); | ||
}); | ||
} | ||
|
||
private sealed class AnalyzerContext | ||
{ | ||
public AnalyzerContext(Compilation compilation) | ||
{ | ||
TaskSymbol = compilation.GetBestTypeByMetadataName("System.Threading.Tasks.Task"); | ||
TaskOfTSymbol = compilation.GetBestTypeByMetadataName("System.Threading.Tasks.Task`1"); | ||
|
||
ConfiguredTaskAwaitableSymbol = compilation.GetBestTypeByMetadataName("System.Runtime.CompilerServices.ConfiguredTaskAwaitable"); | ||
ConfiguredTaskAwaitableOfTSymbol = compilation.GetBestTypeByMetadataName("System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1"); | ||
|
||
if (TaskSymbol is not null && TaskOfTSymbol is not null) | ||
{ | ||
TaskOfTaskSymbol = TaskOfTSymbol.Construct(TaskSymbol); | ||
TaskOfTaskOfTSymbol = TaskOfTSymbol.Construct(TaskOfTSymbol); | ||
} | ||
} | ||
|
||
public INamedTypeSymbol? TaskSymbol { get; } | ||
public INamedTypeSymbol? TaskOfTSymbol { get; } | ||
public INamedTypeSymbol? TaskOfTaskSymbol { get; } | ||
public INamedTypeSymbol? TaskOfTaskOfTSymbol { get; } | ||
|
||
public INamedTypeSymbol? ConfiguredTaskAwaitableSymbol { get; } | ||
public INamedTypeSymbol? ConfiguredTaskAwaitableOfTSymbol { get; } | ||
|
||
public bool IsValid => TaskOfTaskSymbol is not null || TaskOfTaskOfTSymbol is not null; | ||
|
||
public void AnalyzeAwait(OperationAnalysisContext context) | ||
{ | ||
var operation = (IAwaitOperation)context.Operation; | ||
|
||
if (operation.Operation is IAwaitOperation childAwaitOperation) | ||
{ | ||
if (childAwaitOperation.Operation.Type is not INamedTypeSymbol childAwaitOperationType) | ||
return; | ||
|
||
// Task<Task> | ||
if (childAwaitOperationType.IsEqualTo(TaskOfTaskSymbol)) | ||
{ | ||
context.ReportDiagnostic(Rule, operation); | ||
} | ||
// Task<Task<T>> | ||
else if (childAwaitOperationType.OriginalDefinition.IsEqualTo(TaskOfTSymbol) && childAwaitOperationType.TypeArguments[0].OriginalDefinition.IsEqualTo(TaskOfTSymbol)) | ||
{ | ||
context.ReportDiagnostic(Rule, operation); | ||
} | ||
} | ||
else if (operation.Operation is IInvocationOperation { Instance: IAwaitOperation { Operation.Type: INamedTypeSymbol childAwaitOperationType }, Type: var invocationType } && invocationType.IsEqualToAny(ConfiguredTaskAwaitableSymbol, ConfiguredTaskAwaitableOfTSymbol)) | ||
{ | ||
// Task<Task> | ||
if (childAwaitOperationType.IsEqualTo(TaskOfTaskSymbol)) | ||
{ | ||
context.ReportDiagnostic(Rule, operation); | ||
} | ||
// Task<Task<T>> | ||
else if (childAwaitOperationType.OriginalDefinition.IsEqualTo(TaskOfTSymbol) && childAwaitOperationType.TypeArguments[0].OriginalDefinition.IsEqualTo(TaskOfTSymbol)) | ||
{ | ||
context.ReportDiagnostic(Rule, operation); | ||
} | ||
} | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
tests/Meziantou.Analyzer.Test/Rules/UseTaskUnwrapAnalyzerTests.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,109 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Rules; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Rules; | ||
|
||
public sealed class UseTaskUnwrapAnalyzerTests | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
{ | ||
return new ProjectBuilder() | ||
.WithAnalyzer<UseTaskUnwrapAnalyzer>() | ||
.WithTargetFramework(TargetFramework.Net6_0) | ||
.WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfTask() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Threading.Tasks; | ||
Task<Task> a = null; | ||
[|await await a|]; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfTask_ConfigureAwait() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Threading.Tasks; | ||
Task<Task> a = null; | ||
await (await a.ConfigureAwait(false)); | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfTask_ConfigureAwait_Root() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Threading.Tasks; | ||
Task<Task> a = null; | ||
[|await (await a).ConfigureAwait(false)|]; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfTask_Unwrap_ConfigureAwait_Root() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Threading.Tasks; | ||
Task<Task> a = null; | ||
await a.Unwrap().ConfigureAwait(false); | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfTaskOfInt32() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Threading.Tasks; | ||
Task<Task<int>> a = null; | ||
int b = [|await await a|]; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfValueTaskOfInt32() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Threading.Tasks; | ||
Task<ValueTask<int>> a = null; | ||
int b = await await a; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task ValueTaskOfTaskOfInt32() | ||
{ | ||
await CreateProjectBuilder() | ||
.WithSourceCode(""" | ||
using System.Threading.Tasks; | ||
ValueTask<Task<int>> a = default; | ||
int b = await await a; | ||
""") | ||
.ValidateAsync(); | ||
} | ||
|
||
} |