提交 1de61162 编写于 作者: A Allison Chou

Fix async bug and add tests

上级 05779838
......@@ -7594,6 +7594,12 @@ public async Task TestMissingReturnStatementInAsyncValueTaskMethod()
@"using System;
using System.Threading.Tasks;
namespace System.Threading.Tasks {
struct ValueTask
{
}
}
class Program
{
void M()
......@@ -7608,14 +7614,20 @@ async ValueTask M2()
@"using System;
using System.Threading.Tasks;
namespace System.Threading.Tasks {
struct ValueTask
{
}
}
class Program
{
void M()
{
Func<int, ValueTask> f = async x =>
{
ValueTask {|Rename:task|} = M2();
await task;
ValueTask {|Rename:valueTask|} = M2();
await valueTask;
};
}
......@@ -7662,6 +7674,59 @@ async Task<int> M2()
{
return 0;
}
}");
}
[WorkItem(40745, "https://github.com/dotnet/roslyn/issues/40745")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public async Task TestMissingReturnStatementInAsyncValueTaskTypeMethod()
{
await TestInRegularAndScriptAsync(
@"using System;
using System.Threading.Tasks;
namespace System.Threading.Tasks {
struct ValueTask<T>
{
}
}
class Program
{
void M()
{
Func<int, ValueTask<int>> f = async x => await [|M2()|];
}
async ValueTask<int> M2()
{
return 0;
}
}",
@"using System;
using System.Threading.Tasks;
namespace System.Threading.Tasks {
struct ValueTask<T>
{
}
}
class Program
{
void M()
{
Func<int, ValueTask<int>> f = async x =>
{
ValueTask<int> {|Rename:valueTask|} = M2();
return await valueTask;
};
}
async ValueTask<int> M2()
{
return 0;
}
}");
}
}
......
......@@ -125,8 +125,22 @@ internal partial class CSharpIntroduceVariableService
if (oldLambda.AsyncKeyword != default)
{
if (document.SemanticModel.Compilation.TaskType().Equals(delegateType.DelegateInvokeMethod.ReturnType) ||
document.SemanticModel.Compilation.ValueTaskOfTType().Equals(delegateType.DelegateInvokeMethod.ReturnType))
var compilation = document.SemanticModel.Compilation;
// Async lambdas with a Task or ValueTask return type don't need a return statement.
// e.g.:
// Func<int, Task> f = async x => await M2();
//
// After refactoring:
// Func<int, Task> f = async x =>
// {
// Task task = M2();
// await task;
// };
if ((compilation.TaskType() != null &&
compilation.TaskType().Equals(delegateType.DelegateInvokeMethod.ReturnType)) ||
(compilation.ValueTaskOfType() != null &&
compilation.ValueTaskOfType().Equals(delegateType.DelegateInvokeMethod.ReturnType)))
{
return false;
}
......
......@@ -112,8 +112,12 @@ public static ImmutableArray<IAssemblySymbol> GetReferencedAssemblySymbols(this
public static INamedTypeSymbol? TaskOfTType(this Compilation compilation)
=> compilation.GetTypeByMetadataName(typeof(Task<>).FullName!);
public static INamedTypeSymbol? ValueTaskOfType(this Compilation compilation)
=> compilation.GetTypeByMetadataName("System.Threading.Tasks.ValueTask");
public static INamedTypeSymbol? ValueTaskOfTType(this Compilation compilation)
=> compilation.GetTypeByMetadataName("System.Threading.Tasks.ValueTask`1");
public static INamedTypeSymbol? IEnumerableOfTType(this Compilation compilation)
=> compilation.GetTypeByMetadataName(typeof(IEnumerable<>).FullName!);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册