Skip to content

Commit

Permalink
Merge pull request #73010 from CyrusNajmabadi/toImmutableAndClear
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Apr 13, 2024
2 parents d20c3e1 + 0789664 commit bc83d42
Show file tree
Hide file tree
Showing 217 changed files with 364 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ public static ImmutableArray<CollectionExpressionMatch<StatementSyntax>> TryGetM
return default;
}

return matches.ToImmutable();
return matches.ToImmutableAndClear();
}

public static bool IsCollectionFactoryCreate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private static ImmutableArray<IFieldSymbol> GetEqualizedFields(
value, successRequirement: true, type, fields, out var _2))
{
// we're done, no more statements to check
return fields.ToImmutable();
return fields.ToImmutableAndClear();
}
// check for the first statement as an explicit cast to a variable declaration
// like: var otherC = other as C;
Expand All @@ -557,7 +557,7 @@ private static ImmutableArray<IFieldSymbol> GetEqualizedFields(
return [];
}

return fields.ToImmutable();
return fields.ToImmutableAndClear();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static ImmutableArray<PositionalParameterInfo> GetPropertiesForPositional
_ => throw ExceptionUtilities.Unreachable(),
}).WhereNotNull());

return resultBuilder.ToImmutable();
return resultBuilder.ToImmutableAndClear();
}

public static ImmutableArray<IPropertySymbol> GetInheritedPositionalParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static ImmutableArray<UsingDirectiveSyntax> GetAllUsingDirectives(Compil

Recurse(compilationUnit.Members);

return result.ToImmutable();
return result.ToImmutableAndClear();

void Recurse(SyntaxList<MemberDeclarationSyntax> members)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static ImmutableArray<StatementSyntax> Expand(UsingStatementSyntax using
for (int i = 0, n = result.Count; i < n; i++)
result[i] = result[i].WithAdditionalAnnotations(Formatter.Annotation);

return result.ToImmutable();
return result.ToImmutableAndClear();
}

private static SyntaxTriviaList Expand(ArrayBuilder<StatementSyntax> result, UsingStatementSyntax usingStatement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ public void Clear() { }
}
""";

public static readonly IEnumerable<object[]> FailureCreationPatterns = new[]
{
new[] {"var builder = ImmutableArray.CreateBuilder<int>();" },
new[] {"var builder = ArrayBuilder<int>.GetInstance();" },
new[] {"using var _ = ArrayBuilder<int>.GetInstance(out var builder);" },
};

public static readonly IEnumerable<object[]> SuccessCreationPatterns = new[]
{
new[] {"[|var builder = ImmutableArray.[|CreateBuilder|]<int>();|]" },
new[] {"[|var builder = ArrayBuilder<int>.[|GetInstance|]();|]" },
new[] {"[|using var _ = ArrayBuilder<int>.[|GetInstance|](out var builder);|]" },
};
public static readonly IEnumerable<object[]> FailureCreationPatterns =
[
["var builder = ImmutableArray.CreateBuilder<int>();"],
["var builder = ArrayBuilder<int>.GetInstance();"],
["using var _ = ArrayBuilder<int>.GetInstance(out var builder);"],
];

public static readonly IEnumerable<object[]> SuccessCreationPatterns =
[
["[|var builder = ImmutableArray.[|CreateBuilder|]<int>();|]"],
["[|var builder = ArrayBuilder<int>.[|GetInstance|]();|]"],
["[|using var _ = ArrayBuilder<int>.[|GetInstance|](out var builder);|]"],
];

[Theory, MemberData(nameof(FailureCreationPatterns))]
public async Task TestNotInCSharp11(string pattern)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected ImmutableArray<TMatch> AnalyzeWorker(CancellationToken cancellationTok
if (!TryAddMatches(matches, cancellationToken))
return default;

return matches.ToImmutable();
return matches.ToImmutableAndClear();
}

protected UpdateExpressionState<TExpressionSyntax, TStatementSyntax>? TryInitializeState(
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzers/Core/CodeFixes/AddExplicitCast/Fixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected abstract class Fixer<TArgumentSyntax, TArgumentListSyntax, TInvocation
// operations are in order and user can choose least specific types(more accurate)
mutablePotentialConversionTypes.Sort(new InheritanceDistanceComparer<TExpressionSyntax>(semanticModel));

return mutablePotentialConversionTypes.ToImmutable();
return mutablePotentialConversionTypes.ToImmutableAndClear();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static async Task<ImmutableArray<Diagnostic>> GetSolutionDiagnosticsAsync(FixAll
diagnostics.AddRange(projectDiagnostics);
}

return diagnostics.ToImmutable();
return diagnostics.ToImmutableAndClear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private ImmutableArray<TextChange> DetermineTotalEditsToMakeToRawString(
if (quotesToAdd != null)
edits.Add(new TextChange(new TextSpan(StringExpressionBeforePasteInfo.EndDelimiterSpanWithoutSuffix.End, 0), quotesToAdd));

return edits.ToImmutable();
return edits.ToImmutableAndClear();
}

private void UpdateExistingInterpolationBraces(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private ImmutableArray<TextChange> GetEditsForRawString()
if (quotesToAdd != null)
edits.Add(new TextChange(new TextSpan(StringExpressionBeforePasteInfo.EndDelimiterSpanWithoutSuffix.End, 0), quotesToAdd));

return edits.ToImmutable();
return edits.ToImmutableAndClear();
}

/// <inheritdoc cref="AbstractPasteProcessor.GetQuotesToAddToRawString(SourceText, ImmutableArray{TextSpan})" />
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core.Wpf/Peek/PeekableItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task<IEnumerable<IPeekableItem>> GetPeekableItemsAsync(
}
}

return results.ToImmutable();
return results.ToImmutableAndClear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private VisibleBlock(double x, ImmutableArray<(double start, double end)> ySegme
if ((currentSegmentBottom - currentSegmentTop) >= MinLineHeight)
segments.Add((currentSegmentTop, currentSegmentBottom));

return segments.ToImmutable();
return segments.ToImmutableAndClear();
}

private static bool IsInHole(ImmutableArray<SnapshotSpan> orderedHoleSpans, ITextViewLine line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ internal async Task<ImmutableArray<CodeDefinitionWindowLocation>> GetContextFrom
}
}

return builder.ToImmutable();
return builder.ToImmutableAndClear();
}

// We didn't have regular source references, but possibly:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static ImmutableArray<TextSpan> GetSpans(RoslynNavigationBarItem underly
using var _ = ArrayBuilder<TextSpan>.GetInstance(out var spans);
AddSpans(underlyingItem, spans);
spans.SortAndRemoveDuplicates(Comparer<TextSpan>.Default);
return spans.ToImmutable();
return spans.ToImmutableAndClear();

static void AddSpans(RoslynNavigationBarItem underlyingItem, ArrayBuilder<TextSpan> spans)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<ImmutableArray<IntentSource>> ComputeIntentsAsync(IntentReques
convertedResults.AddIfNotNull(convertedIntent);
}

return convertedResults.ToImmutable();
return convertedResults.ToImmutableAndClear();
}

private static async Task<IntentSource?> ConvertToIntelliCodeResultAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public ImmutableArray<CompletionFilterWithState> GetFilterStatesInSet()
}
}

return builder.ToImmutable();
return builder.ToImmutableAndClear();
}

/// <summary>
Expand All @@ -218,7 +218,7 @@ public static ImmutableArray<CompletionFilterWithState> CombineFilterStates(Immu
}
}

return builder.ToImmutable();
return builder.ToImmutableAndClear();

void AddFilterState(ImmutableArray<CompletionFilterWithState> filterStates)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static ImmutableArray<SnapshotSpan> GetTags(
tags.AddRange(tag.Span.GetSpans(span.Snapshot.TextBuffer));

tags.Sort(static (ss1, ss2) => ss1.Start - ss2.Start);
return tags.ToImmutable();
return tags.ToImmutableAndClear();
}

private static SnapshotSpan GetDestinationTag(
Expand Down
4 changes: 2 additions & 2 deletions src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -941,9 +941,9 @@ public ImmutableArray<SyntaxNode> ParseStatements(string statements)
return default;
}

using var listDisposer = ArrayBuilder<SyntaxNode>.GetInstance(out var list);
var delimiter = IsVisualBasic ? "\r\n" : ";";
var parts = statements.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
var list = new FixedSizeArrayBuilder<SyntaxNode>(parts.Length);
foreach (var p in parts)
{
if (IsVisualBasic)
Expand All @@ -956,7 +956,7 @@ public ImmutableArray<SyntaxNode> ParseStatements(string statements)
}
}

return list.ToImmutable();
return list.MoveToImmutable();
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ private static ImmutableArray<SnippetPlaceholder> GetSnippetPlaceholders(string
}
}

return arrayBuilder.ToImmutable();
return arrayBuilder.ToImmutableAndClear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static ImmutableArray<TextChange> GetMergedChanges(TextChange? newLineEdit, Immu
[newLineEdit.Value.ToTextChangeRange()],
formattingChanges.SelectAsArray(f => f.ToTextChangeRange()));

using var _ = ArrayBuilder<TextChange>.GetInstance(out var mergedChanges);
var mergedChanges = new FixedSizeArrayBuilder<TextChange>(newRanges.Length);
var amountToShift = 0;
foreach (var newRange in newRanges)
{
Expand All @@ -200,7 +200,7 @@ static ImmutableArray<TextChange> GetMergedChanges(TextChange? newLineEdit, Immu
mergedChanges.Add(new TextChange(newTextChangeSpan, newTextChangeText));
}

return mergedChanges.ToImmutable();
return mergedChanges.MoveToImmutable();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ private ImmutableArray<T> TransferLeadingWhitespaceTrivia<T, U>(IEnumerable<T> n
index++;
}

return result.ToImmutable();
return result.ToImmutableAndClear();
}

private async ValueTask<ImmutableArray<SyntaxTrivia>> UpdateParamTagsInLeadingTriviaAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<ImmutableArray<CodeLensMember>> GetCodeLensMembersAsync(Docume

visitor.Visit(root);

return codeLensNodes.ToImmutable();
return codeLensNodes.ToImmutableAndClear();
}

private sealed class CSharpCodeLensVisitor(ArrayBuilder<CodeLensMember> memberBuilder) : CSharpSyntaxWalker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DeclarationNameRecommender()
GetRecommendedNames(names, nameInfo, context, result, namingStyleOptions, cancellationToken);
}

return result.ToImmutable();
return result.ToImmutableAndClear();
}

private ImmutableArray<ImmutableArray<string>> GetBaseNames(SemanticModel semanticModel, NameDeclarationInfo nameInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private static ImmutableArray<StatementSyntax> GenerateProgramMainStatements(
}
}

return statements.ToImmutable();
return statements.ToImmutableAndClear();
}

private static TSyntaxNode FixupComments<TSyntaxNode>(TSyntaxNode node) where TSyntaxNode : SyntaxNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ private static ImmutableArray<GlobalStatementSyntax> GetGlobalStatements(
}
}

using var _1 = ArrayBuilder<GlobalStatementSyntax>.GetInstance(out var globalStatements);
var globalStatements = new FixedSizeArrayBuilder<GlobalStatementSyntax>(statements.Count);
foreach (var statement in statements)
globalStatements.Add(GlobalStatement(statement).WithAdditionalAnnotations(Formatter.Annotation));

return globalStatements.ToImmutable();
return globalStatements.MoveToImmutable();
}

private static VariableDeclarationSyntax ConvertDeclaration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ protected override async Task<ImmutableArray<Location>> GetAdditionalReferencesA
}
}

return results.ToImmutable();
return results.ToImmutableAndClear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override ImmutableArray<StatementSyntax> GetInitialStatementsForMethod
}
}

return list.ToImmutable();
return list.ToImmutableAndClear();
}

private static IEnumerable<StatementSyntax> GetStatementsFromContainer(SyntaxNode node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ private ImmutableArray<StatementSyntax> MoveDeclarationOutFromMethodDefinition(
result.AddRange(expressionStatements);
}

return result.ToImmutable();
return result.ToImmutableAndClear();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private ImmutableArray<StatementSyntax> MergeDeclarationStatementsWorker(Immutab
if (map.Count > 0)
result.AddRange(GetMergedDeclarationStatements(map));

return result.ToImmutable();
return result.ToImmutableAndClear();
}

private void AppendDeclarationStatementToMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ protected override ImmutableArray<ITypeParameterSymbol> GenerateTypeParameters(C
}
else
{
using var _ = ArrayBuilder<ITypeParameterSymbol>.GetInstance(out var list);
var list = new FixedSizeArrayBuilder<ITypeParameterSymbol>(genericName.TypeArgumentList.Arguments.Count);

var usedIdentifiers = new HashSet<string> { "T" };
using var _ = PooledHashSet<string>.GetInstance(out var usedIdentifiers);
usedIdentifiers.Add("T");
foreach (var type in genericName.TypeArgumentList.Arguments)
{
var typeParameter = GetUniqueTypeParameter(
Expand All @@ -99,7 +100,7 @@ protected override ImmutableArray<ITypeParameterSymbol> GenerateTypeParameters(C
list.Add(typeParameter);
}

return list.ToImmutable();
return list.MoveToImmutable();
}
}

Expand Down Expand Up @@ -147,18 +148,18 @@ protected override bool IsImplicitReferenceConversion(Compilation compilation, I

protected override ImmutableArray<ITypeSymbol> DetermineTypeArguments(CancellationToken cancellationToken)
{
using var _ = ArrayBuilder<ITypeSymbol>.GetInstance(out var result);

if (State.SimpleNameOpt is GenericNameSyntax genericName)
if (State.SimpleNameOpt is not GenericNameSyntax genericName)
return [];

var result = new FixedSizeArrayBuilder<ITypeSymbol>(genericName.TypeArgumentList.Arguments.Count);
foreach (var typeArgument in genericName.TypeArgumentList.Arguments)
{
foreach (var typeArgument in genericName.TypeArgumentList.Arguments)
{
var typeInfo = Document.SemanticModel.GetTypeInfo(typeArgument, cancellationToken);
result.Add(typeInfo.Type);
}
var typeInfo = Document.SemanticModel.GetTypeInfo(typeArgument, cancellationToken);
result.Add(typeInfo.Type);
}

return result.ToImmutable();
return result.MoveToImmutable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ protected override ImmutableArray<SymbolDisplayPart> GetNormalAnonymousTypeParts
members.AddRange(Space());
members.Add(Punctuation(SyntaxFacts.GetText(SyntaxKind.CloseBraceToken)));

return members.ToImmutable();
return members.ToImmutableAndClear();
}
}
Loading

0 comments on commit bc83d42

Please sign in to comment.