提交 5ed2a672 编写于 作者: S Stephen Toub

Use cached true, false, and empty tasks in a handful of places

In a handful of places, Task.FromResult(true), Task.FromResult(false), and Task.FromResult(default(T)) were being used, even though equivalent cached tasks are available in SpecializedTasks. This commit just changes those ~20 call sites to use already cached tasks.
上级 a7eb09e9
......@@ -36,7 +36,7 @@ private class TrackingSession : ForegroundThreadAffinitizedObject
private readonly CancellationToken _cancellationToken;
private readonly IAsynchronousOperationListener _asyncListener;
private Task<bool> _newIdentifierBindsTask = Task.FromResult(false);
private Task<bool> _newIdentifierBindsTask = SpecializedTasks.False;
private readonly string _originalName;
public string OriginalName { get { return _originalName; } }
......
......@@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CodeFixes.Async;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Roslyn.Utilities;
using Resources = Microsoft.CodeAnalysis.CSharp.CSharpFeaturesResources;
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.Async
......@@ -51,17 +52,17 @@ protected override Task<SyntaxNode> GetNewRoot(SyntaxNode root, SyntaxNode oldNo
case CS4016:
if (expression == null)
{
return Task.FromResult<SyntaxNode>(null);
return SpecializedTasks.Default<SyntaxNode>();
}
if (!IsCorrectReturnType(expression, semanticModel))
{
return Task.FromResult<SyntaxNode>(null);
return SpecializedTasks.Default<SyntaxNode>();
}
return Task.FromResult(root.ReplaceNode(oldNode, ConvertToAwaitExpression(expression)));
default:
return Task.FromResult<SyntaxNode>(null);
return SpecializedTasks.Default<SyntaxNode>();
}
}
......
......@@ -254,7 +254,7 @@ public Task<string> GetSnippetExpansionNoteForCompletionItemAsync(CompletionItem
return Task.FromResult(string.Format(FeaturesResources.NoteTabTwiceToInsertTheSnippet, insertionText));
}
return Task.FromResult((string)null);
return SpecializedTasks.Default<string>();
}
public virtual bool SupportSnippetCompletionListOnTab
......
......@@ -142,7 +142,7 @@ internal class CompletionItem : IComparable<CompletionItem>
public virtual Task<ImmutableArray<SymbolDisplayPart>> GetDescriptionAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return this.LazyDescription == null
? Task.FromResult(ImmutableArray.Create<SymbolDisplayPart>())
? SpecializedTasks.EmptyImmutableArray<SymbolDisplayPart>()
: this.LazyDescription.GetValueAsync(cancellationToken);
}
......
......@@ -24,7 +24,7 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
public override Task AnalyzeSyntaxAsync(Document document, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
{
// No syntax diagnostics produced by the EnC engine.
return Task.FromResult(false);
return SpecializedTasks.EmptyTask;
}
public override async Task AnalyzeSemanticsAsync(Document document, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
......
......@@ -12,7 +12,7 @@ internal class ExtractInterfaceCodeAction : CodeActionWithOptions
{
private readonly ExtractInterfaceTypeAnalysisResult _typeAnalysisResult;
private readonly AbstractExtractInterfaceService _extractInterfaceService;
private readonly Task<IEnumerable<CodeActionOperation>> _taskReturningNoCodeActionOperations = Task.FromResult(SpecializedCollections.EmptyEnumerable<CodeActionOperation>());
private readonly Task<IEnumerable<CodeActionOperation>> _taskReturningNoCodeActionOperations = SpecializedTasks.EmptyEnumerable<CodeActionOperation>();
public ExtractInterfaceCodeAction(AbstractExtractInterfaceService extractInterfaceService, ExtractInterfaceTypeAnalysisResult typeAnalysisResult)
{
......
......@@ -42,14 +42,14 @@ protected new class State : AbstractGenerateParameterizedMemberService<TService,
{
if (!TryInitializeImplicitConversion(service, document, node, cancellationToken))
{
return Task.FromResult(false);
return SpecializedTasks.False;
}
}
else if (service.IsExplicitConversionGeneration(node))
{
if (!TryInitializeExplicitConversion(service, document, node, cancellationToken))
{
return Task.FromResult(false);
return SpecializedTasks.False;
}
}
......
......@@ -58,14 +58,14 @@ internal new class State : AbstractGenerateParameterizedMemberService<TService,
{
if (!TryInitializeExplicitInterface(service, document, node, cancellationToken))
{
return Task.FromResult(false);
return SpecializedTasks.False;
}
}
else if (service.IsSimpleNameGeneration(node))
{
if (!TryInitializeSimpleName(service, document, (TSimpleNameSyntax)node, cancellationToken))
{
return Task.FromResult(false);
return SpecializedTasks.False;
}
}
......
......@@ -382,14 +382,14 @@ protected override Task ExecuteAsync()
if (project == null)
{
data.AsyncToken.Dispose();
return Task.FromResult(true);
return SpecializedTasks.EmptyTask;
}
if (!data.NeedDependencyTracking)
{
EnqueueWorkItem(project);
data.AsyncToken.Dispose();
return Task.FromResult(true);
return SpecializedTasks.EmptyTask;
}
// do dependency tracking here with current solution
......@@ -403,7 +403,7 @@ protected override Task ExecuteAsync()
}
data.AsyncToken.Dispose();
return Task.FromResult(true);
return SpecializedTasks.EmptyTask;
}
private Data Dequeue()
......
......@@ -42,7 +42,7 @@ public override IEnumerable<ICompletionProvider> GetDefaultCompletionProviders()
public override Task<TextSpan> GetDefaultTrackingSpanAsync(Document document, int position, CancellationToken cancellationToken)
{
return Task.FromResult(default(TextSpan));
return SpecializedTasks.Default<TextSpan>();
}
protected override bool TriggerOnBackspace(SourceText text, int position, CompletionTriggerInfo triggerInfo, OptionSet options)
......
......@@ -268,7 +268,7 @@ private bool TryGetBestDeclarationForSingleEdit(ISymbol symbol, out SyntaxNode d
(e, d, c) =>
{
editAction(e, d);
return Task.FromResult(true);
return SpecializedTasks.EmptyTask;
},
cancellationToken);
}
......@@ -365,7 +365,7 @@ private void CheckSymbolArgument(ISymbol currentSymbol, ISymbol argSymbol)
(e, d, c) =>
{
editAction(e, d);
return Task.FromResult(true);
return SpecializedTasks.EmptyTask;
},
cancellationToken);
}
......@@ -448,7 +448,7 @@ private void CheckSymbolArgument(ISymbol currentSymbol, ISymbol argSymbol)
(e, d, c) =>
{
editAction(e, d);
return Task.FromResult(true);
return SpecializedTasks.EmptyTask;
},
cancellationToken);
}
......@@ -529,7 +529,7 @@ private void CheckSymbolArgument(ISymbol currentSymbol, ISymbol argSymbol)
(e, d, c) =>
{
editAction(e, d);
return Task.FromResult(true);
return SpecializedTasks.EmptyTask;
},
cancellationToken);
}
......
......@@ -161,7 +161,7 @@ public Task<SyntaxTree> GetSyntaxTreeAsync(CancellationToken cancellationToken =
// immediately.
if (!this.SupportsSyntaxTree)
{
return Task.FromResult<SyntaxTree>(null);
return SpecializedTasks.Default<SyntaxTree>();
}
if (_syntaxTreeResultTask != null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册