提交 ac408e99 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #17850 from CyrusNajmabadi/noAsync

Do not offer to convert method to async if we can't find the type for 'Task' and 'Task<T>'.
......@@ -34,6 +34,19 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
}
var semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var compilation = semanticModel.Compilation;
// Find the symbols for Task, Task<T> and ValueTask<T>. Note that the first
// two are mandatory (since we need them to generate the return types for our
// method if we convert it. The last is optional. It is only needed to know
// if our member is already Task-Like, and that functionality recognizes
// ValueTask if it is available, but does not care if it is not.
var (taskType, taskOfTType, valueTaskOfTTypeOpt) = GetTaskTypes(compilation);
if (taskType == null || taskOfTType == null)
{
return;
}
var symbol = semanticModel.GetDeclaredSymbol(node, cancellationToken) as IMethodSymbol;
// If it's a void returning method, offer to keep the void return type, or convert to
......@@ -60,6 +73,15 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
}
}
private (INamedTypeSymbol taskType, INamedTypeSymbol taskOfTType, INamedTypeSymbol valueTaskOfTTypeOpt) GetTaskTypes(Compilation compilation)
{
var taskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
var taskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
var valueTaskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.ValueTask`1");
return (taskType, taskOfTType, valueTaskOfTType);
}
protected abstract string GetMakeAsyncTaskFunctionResource();
protected abstract string GetMakeAsyncVoidFunctionResource();
......@@ -128,9 +150,7 @@ private SyntaxNode GetContainingFunction(Diagnostic diagnostic, CancellationToke
SyntaxNode node, CancellationToken cancellationToken)
{
var compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
var taskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
var taskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
var valueTaskOfTType = compilation.GetTypeByMetadataName("System.Threading.Tasks.ValueTask`1");
var (taskType, taskOfTType, valueTaskOfTType) = GetTaskTypes(compilation);
var newNode = AddAsyncTokenAndFixReturnType(keepVoid, methodSymbolOpt, node, taskType, taskOfTType, valueTaskOfTType)
.WithAdditionalAnnotations(Formatter.Annotation);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册