Skip to content

Commit

Permalink
CSE009: Do not report task awaited in lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Apr 18, 2024
1 parent 4a70f06 commit 330463b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,7 @@
<data name="_009_ReturnedTask" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TestCases\009_ReturnedTask.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="_010_Awaited_In_Lambda" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TestCases\010_Awaited_In_Lambda.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ public void should_not_report_returned_task()
{
NoDiagnosticAtMarker(TaskVariableNotAwaiteTestCases._009_ReturnedTask,TaskVariableNotAwaitedAnalyzer.TaskVariableNotAwaitedDescriptor.Id);
}

[Test]
public void should_not_report_awaited_in_lambda()
{
NoDiagnosticAtMarker(TaskVariableNotAwaiteTestCases._010_Awaited_In_Lambda,TaskVariableNotAwaitedAnalyzer.TaskVariableNotAwaitedDescriptor.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TestNamespace
{
public Task Test()
{
var [|t2|] = CalculateAsync();
var [| t2 |] = CalculateAsync();
if(DateTime.Now.IsDaylightSavingTime())
{
return t2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;

namespace TestNamespace
{
class SampleClass
{
public async Task Test()
{
var [|t2|] = CalculateAsync();
await Step(async () =>
{
await t2;
});

}


private static Task<int> CalculateAsync() => throw null;
private static Task Step(Func<Task> func) => throw null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;

namespace TestNamespace
{
class SampleClass
{
public async Task Test()
{
var t2 = CalculateAsync();
await Step(async () =>
{
await t2;
});

}


private static Task<int> CalculateAsync() => throw null;
private static Task Step(Func<Task> func) => throw null;
}
}
15 changes: 12 additions & 3 deletions src/CSharpExtensions.Analyzers/TaskVariableNotAwaitedAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ public override void Initialize(AnalysisContext context)
var variableType = variableDeclaratorOperation.Symbol.Type;
if (IsTask(variableType) || (variableType is IArrayTypeSymbol arrayTypeSymbol && IsTask(arrayTypeSymbol.ElementType)))
{
var controlFlowGraph = actionContext.GetControlFlowGraph();
IEnumerable<IOperation> EnumerateOperations(IEnumerable<IOperation> input)
{
foreach (var operation in input)
{
if (operation == null)
Expand All @@ -38,15 +41,21 @@ IEnumerable<IOperation> EnumerateOperations(IEnumerable<IOperation> input)
}
yield return operation;
if (operation is IFlowAnonymousFunctionOperation lambdaOperation)
{
var lambdaFlow = controlFlowGraph.GetAnonymousFunctionControlFlowGraph(lambdaOperation);
foreach (var lambdaSubOperation in EnumerateOperations(lambdaFlow.Blocks.SelectMany(c => c.Operations.Add(c.BranchValue))))
{
yield return lambdaSubOperation;
}
}
foreach (var childOperation in EnumerateOperations(operation.Children))
{
yield return childOperation;
}
}
}
var controlFlowGraph = actionContext.GetControlFlowGraph();
foreach (var operation in EnumerateOperations(controlFlowGraph.Blocks.SelectMany(c => c.Operations.Add(c.BranchValue))))
{
Expand Down

0 comments on commit 330463b

Please sign in to comment.