-
-
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.
New rule MA0129: Await task in using statements (#459)
- Loading branch information
Showing
6 changed files
with
204 additions
and
0 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 @@ | ||
# MA0129 - Await task in using statement | ||
|
||
A `Task` doesn't need to be disposed. When used in a `using` statement, most of the time, developers forgot to await it. | ||
|
||
```` | ||
Task<IDiposable> t = ...; | ||
using(t) { } // non-compliant | ||
using(await t) { } // 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
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,93 @@ | ||
using System; | ||
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 TaskInUsingAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static readonly DiagnosticDescriptor s_rule = new( | ||
RuleIdentifiers.TaskInUsing, | ||
title: "Await task in using statement", | ||
messageFormat: "Await task in using statement", | ||
RuleCategories.Usage, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: "", | ||
helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.TaskInUsing)); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze); | ||
|
||
context.RegisterCompilationStartAction(ctx => | ||
{ | ||
var taskSymbol = ctx.Compilation.GetBestTypeByMetadataName("System.Threading.Tasks.Task"); | ||
if (taskSymbol == null) | ||
return; | ||
|
||
var analyzerContext = new AnalyzerContext(taskSymbol); | ||
ctx.RegisterOperationAction(analyzerContext.AnalyzeUsing, OperationKind.Using); | ||
ctx.RegisterOperationAction(analyzerContext.AnalyzeUsingDeclaration, OperationKind.UsingDeclaration); | ||
}); | ||
} | ||
|
||
private sealed class AnalyzerContext | ||
{ | ||
private INamedTypeSymbol _taskSymbol; | ||
|
||
public AnalyzerContext(INamedTypeSymbol taskSymbol) | ||
{ | ||
_taskSymbol = taskSymbol; | ||
} | ||
|
||
public void AnalyzeUsing(OperationAnalysisContext context) | ||
{ | ||
var operation = (IUsingOperation)context.Operation; | ||
AnalyzeResource(context, operation.Resources); | ||
} | ||
|
||
internal void AnalyzeUsingDeclaration(OperationAnalysisContext context) | ||
{ | ||
var operation = (IUsingDeclarationOperation)context.Operation; | ||
AnalyzeResource(context, operation.DeclarationGroup); | ||
} | ||
|
||
private void AnalyzeResource(OperationAnalysisContext context, IOperation? operation) | ||
{ | ||
if (operation == null) | ||
return; | ||
|
||
if (operation is IVariableDeclarationGroupOperation variableDeclarationGroupOperation) | ||
{ | ||
foreach (var declaration in variableDeclarationGroupOperation.Declarations) | ||
{ | ||
AnalyzeResource(context, declaration); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (operation is IVariableDeclarationOperation variableDeclarationOperation) | ||
{ | ||
foreach (var declarator in variableDeclarationOperation.Declarators) | ||
{ | ||
AnalyzeResource(context, declarator.Initializer?.Value); | ||
} | ||
return; | ||
} | ||
|
||
operation = operation.UnwrapImplicitConversionOperations(); | ||
if (operation.Type != null && operation.Type.IsOrInheritFrom(_taskSymbol)) | ||
{ | ||
context.ReportDiagnostic(s_rule, operation); | ||
} | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
tests/Meziantou.Analyzer.Test/Rules/TaskInUsingAnalyzerTests.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,92 @@ | ||
using System.Threading.Tasks; | ||
using Meziantou.Analyzer.Rules; | ||
using Microsoft.CodeAnalysis; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Meziantou.Analyzer.Test.Rules; | ||
public sealed class TaskInUsingAnalyzerTests | ||
{ | ||
private static ProjectBuilder CreateProjectBuilder() | ||
{ | ||
return new ProjectBuilder() | ||
.WithOutputKind(OutputKind.ConsoleApplication) | ||
.WithAnalyzer<TaskInUsingAnalyzer>(); | ||
} | ||
|
||
[Fact] | ||
public async Task SingleTaskInUsing() | ||
{ | ||
const string SourceCode = """ | ||
using System.Threading.Tasks; | ||
Task t = null; | ||
using ([|t|]) { } | ||
"""; | ||
|
||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task SingleTaskAssignedInUsing() | ||
{ | ||
const string SourceCode = """ | ||
using System.Threading.Tasks; | ||
Task t = null; | ||
using (var a = [|t|]) { } | ||
"""; | ||
|
||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task MultipleTasksInUsing() | ||
{ | ||
const string SourceCode = """ | ||
using System.Threading.Tasks; | ||
Task t1 = null; | ||
Task t2 = null; | ||
using (Task a = [|t1|], b = [|t2|]) { } | ||
"""; | ||
|
||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfTInUsing() | ||
{ | ||
const string SourceCode = """ | ||
using System.Threading.Tasks; | ||
Task<System.IDisposable> t1 = null; | ||
using ([|t1|]) { } | ||
"""; | ||
|
||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task TaskOfTInUsingStatement() | ||
{ | ||
const string SourceCode = """ | ||
using System.Threading.Tasks; | ||
Task<System.IDisposable> t1 = null; | ||
using var a = [|t1|]; | ||
"""; | ||
|
||
await CreateProjectBuilder() | ||
.WithSourceCode(SourceCode) | ||
.ValidateAsync(); | ||
} | ||
} |