Skip to content

Commit

Permalink
Switch to collection expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Jan 7, 2025
1 parent cd134f7 commit fc6ddbf
Show file tree
Hide file tree
Showing 43 changed files with 88 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void AnalyzeNamespace(SyntaxNodeAnalysisContext context)
diagnosticLocation,
option.Notification,
context.Options,
ImmutableArray.Create(declaration.GetLocation()),
[declaration.GetLocation()],
ImmutableDictionary<string, string?>.Empty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void AnalyzeNamespace(SyntaxNodeAnalysisContext context)
diagnosticLocation,
option.Notification,
context.Options,
ImmutableArray.Create(declaration.GetLocation()),
[declaration.GetLocation()],
ImmutableDictionary<string, string?>.Empty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void ProcessCompilationUnit(SyntaxNodeAnalysisContext context)
methodDeclaration, isHidden: option.Notification.Severity.WithDefaultSeverity(DiagnosticSeverity.Hidden) == ReportDiagnostic.Hidden),
option.Notification,
context.Options,
ImmutableArray.Create(methodDeclaration.GetLocation()),
[methodDeclaration.GetLocation()],
ImmutableDictionary<string, string?>.Empty));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
localFunction.Identifier.GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(localFunction.GetLocation()),
additionalLocations: [localFunction.GetLocation()],
properties: null));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void AnalyzeBlock(
location,
notificationOption,
context.Options,
additionalLocations: ImmutableArray.Create(additionalLocation),
additionalLocations: [additionalLocation],

Check failure on line 166 in src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs#L166

src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs(166,39): error CS8601: (NETCORE_ENGINEERING_TELEMETRY=Build) Possible null reference assignment.

Check failure on line 166 in src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs#L166

src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs(166,39): error CS8601: (NETCORE_ENGINEERING_TELEMETRY=Build) Possible null reference assignment.

Check failure on line 166 in src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs

View check run for this annotation

Azure Pipelines / roslyn-CI (Correctness Correctness_Analyzers)

src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs#L166

src/Analyzers/CSharp/Analyzers/MakeStructMemberReadOnly/CSharpMakeStructMemberReadOnlyAnalyzer.cs(166,39): error CS8601: (NETCORE_ENGINEERING_TELEMETRY=Build) Possible null reference assignment.
properties: null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override void InitializeWorker(AnalysisContext context)
location,
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(additionalLocation),
additionalLocations: [additionalLocation],
properties: null));
});
}, SymbolKind.NamedType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void ProcessConstructorInitializer(
colonToken.GetLocation(),
notificationOption,
context.Options,
additionalLocations: ImmutableArray.Create(initializer.GetLocation()),
additionalLocations: [initializer.GetLocation()],
properties: null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
((PostfixUnaryExpressionSyntax)left).OperatorToken.GetLocation(),
NotificationOption2.Warning,
context.Options,
ImmutableArray.Create(node.GetLocation()),
[node.GetLocation()],
properties: null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void AnalyzeSubpattern(SyntaxNodeAnalysisContext syntaxContext)
expressionColon.GetLocation(),
styleOption.Notification,
syntaxContext.Options,
ImmutableArray.Create(subpattern.GetLocation()),
[subpattern.GetLocation()],
properties: null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private void AnalyzeArrayInitializerExpression(SyntaxNodeAnalysisContext context
initializer.OpenBraceToken.GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(initializer.GetLocation()),
additionalLocations: [initializer.GetLocation()],
properties: changesSemantics ? ChangesSemantics : null));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void AnalyzeMemberAccess(SyntaxNodeAnalysisContext context, INamedTypeSy
memberAccess.Name.Identifier.GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(nodeToReplace.GetLocation()),
additionalLocations: [nodeToReplace.GetLocation()],
properties: changesSemantics ? ChangesSemantics : null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void AnalyzeMemberAccess(SyntaxNodeAnalysisContext context, INamedTypeSy
memberAccess.Name.Identifier.GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(invocation.GetLocation()),
additionalLocations: [invocation.GetLocation()],
properties: analysisResult.Value.ChangesSemantics ? ChangesSemantics : null));

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context, CodeStyleOp
{
var location = GetDiagnosticLocation(declaration);

var additionalLocations = ImmutableArray.Create(declaration.GetLocation());
var properties = ImmutableDictionary<string, string?>.Empty;
return DiagnosticHelper.Create(
s_useExpressionBodyForLambda,
location, option.Notification,
analyzerOptions, additionalLocations, properties);
analyzerOptions, [declaration.GetLocation()], properties);
}

if (UseExpressionBodyForLambdaHelpers.CanOfferUseBlockBody(semanticModel, option.Value, declaration, cancellationToken))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
objectCreation.Type.GetLocation(),
styleOption.Notification,
context.Options,
ImmutableArray.Create(objectCreation.GetLocation()),
[objectCreation.GetLocation()],
properties: null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void ProcessBlockLikeStatement(SyntaxTreeAnalysisContext context, Notifi
GetDiagnosticLocation(block),
notificationOption,
context.Options,
additionalLocations: ImmutableArray.Create(nextToken.GetLocation()),
additionalLocations: [nextToken.GetLocation()],
properties: null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void CheckToken(SyntaxTreeAnalysisContext context, NotificationOption2 n
Location.Create(badTrivia.SyntaxTree!, new TextSpan(badTrivia.SpanStart, 0)),
notificationOption,
context.Options,
additionalLocations: ImmutableArray.Create(token.GetLocation()),
additionalLocations: [token.GetLocation()],
properties: null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
assignmentToken.GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(assignment.GetLocation()),
additionalLocations: [assignment.GetLocation()],
properties: ImmutableDictionary.Create<string, string?>()
.Add(UseCompoundAssignmentUtilities.Increment, UseCompoundAssignmentUtilities.Increment)));
return;
Expand All @@ -180,7 +180,7 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
assignmentToken.GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(assignment.GetLocation()),
additionalLocations: [assignment.GetLocation()],
properties: ImmutableDictionary.Create<string, string?>()
.Add(UseCompoundAssignmentUtilities.Decrement, UseCompoundAssignmentUtilities.Decrement)));
return;
Expand All @@ -193,7 +193,7 @@ private void AnalyzeAssignment(SyntaxNodeAnalysisContext context)
assignmentToken.GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(assignment.GetLocation()),
additionalLocations: [assignment.GetLocation()],
properties: null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void AnalyzeOperation(OperationAnalysisContext context)
ifStatement.GetFirstToken().GetLocation(),
option.Notification,
context.Options,
additionalLocations: ImmutableArray.Create(ifStatement.GetLocation()),
additionalLocations: [ifStatement.GetLocation()],
properties: canSimplify ? UseConditionalExpressionHelpers.CanSimplifyProperties : null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ private void AnalyzeIfStatement(
ifStatement.GetFirstToken().GetLocation(),
option.Notification,
context.Options,
ImmutableArray.Create(
ifStatement.GetLocation(),
trueStatement.GetLocation(),
whenPartMatch.GetLocation()),
[ifStatement.GetLocation(), trueStatement.GetLocation(), whenPartMatch.GetLocation()],
properties));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public async Task TrackingService_GetLatestSpansAsync(bool scheduleInitialTracki

spanProvider.GetBaseActiveStatementSpansImpl = (_, documentIds) =>
[
ImmutableArray.Create(
new ActiveStatementSpan(new ActiveStatementId(0), span11, ActiveStatementFlags.NonLeafFrame),
new ActiveStatementSpan(new ActiveStatementId(1), span12, ActiveStatementFlags.LeafFrame)),
[
new ActiveStatementSpan(new ActiveStatementId(0), span11, ActiveStatementFlags.NonLeafFrame),
new ActiveStatementSpan(new ActiveStatementId(1), span12, ActiveStatementFlags.LeafFrame),
],
[],
];

Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Text/Extensions.SnapshotSourceText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private IReadOnlyList<TextChangeRange> GetChangeRanges(ITextImage? oldImage, int
{
// Claim its all changed
Logger.Log(FunctionId.Workspace_SourceText_GetChangeRanges, "Invalid Snapshots");
return ImmutableArray.Create(new TextChangeRange(new TextSpan(0, oldTextLength), this.Length));
return [new TextChangeRange(new TextSpan(0, oldTextLength), this.Length)];
}
else if (AreSameReiteratedVersion(oldImage, newImage))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private FixAllProvider()
}

public override IEnumerable<FixAllScope> GetSupportedFixAllScopes()
=> ImmutableArray.Create(FixAllScope.Solution);
=> [FixAllScope.Solution];

public override Task<CodeAction?> GetFixAsync(FixAllContext fixAllContext)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperation
var oldText = await OriginalDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
var newText = await updatedDocument.GetValueTextAsync(cancellationToken).ConfigureAwait(false);

return ImmutableArray.Create<CodeActionOperation>(
new InstallPackageAndAddImportOperation(
OriginalDocument.Id, oldText, newText, _installOperation));
return [new InstallPackageAndAddImportOperation(
OriginalDocument.Id, oldText, newText, _installOperation)];
}

private sealed class InstallPackageAndAddImportOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ private SuppressionFixAllProvider()
}

public override IEnumerable<FixAllScope> GetSupportedFixAllScopes()
=> ImmutableArray.Create(FixAllScope.Document, FixAllScope.Project,
FixAllScope.Solution, FixAllScope.ContainingMember, FixAllScope.ContainingType);
=> [FixAllScope.Document, FixAllScope.Project, FixAllScope.Solution, FixAllScope.ContainingMember, FixAllScope.ContainingType];

public override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void AnalyzeNode(SyntaxNodeAnalysisContext context)
context.ReportDiagnostic(DiagnosticHelper.Create(
Descriptor, thisExpression.GetLocation(), notification,
context.Options,
ImmutableArray.Create(memberAccessExpression.GetLocation()),
[memberAccessExpression.GetLocation()],
builder.ToImmutable()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected static IReadOnlyList<SyntaxNode> WalkUpScopeBlocks(
blockFacts.IsScopeBlock(parent) &&
blockFacts.GetExecutableBlockStatements(parent).Count == statements.Count)
{
statements = ImmutableArray.Create(parent);
statements = [parent];
}

return statements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected sealed override async Task<SyntaxNode> GetChangedRootAsync(
if (!blockFacts.IsExecutableBlock(ifOrElseIf.Parent))
{
// In order to insert a new statement, we have to be inside a block.
editor.ReplaceNode(ifOrElseIf, (currentNode, _) => generator.ScopeBlock(ImmutableArray.Create(currentNode)));
editor.ReplaceNode(ifOrElseIf, (currentNode, _) => generator.ScopeBlock([currentNode]));
}

editor.InsertAfter(ifOrElseIf, secondIfStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ await localWorkspace.ChangeSolutionAsync(localWorkspace.CurrentSolution
mockEncService.GetBaseActiveStatementSpansImpl = (solution, documentIds) =>
{
AssertEx.Equal(new[] { documentId, inProcOnlyDocumentId }, documentIds);
return [ImmutableArray.Create(activeStatementSpan1)];
return [[activeStatementSpan1]];
};

var baseActiveSpans = await sessionProxy.GetBaseActiveStatementSpansAsync(localWorkspace.CurrentSolution, [documentId, inProcOnlyDocumentId], CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void M()
Uri = ProtocolConversions.CreateAbsoluteUri($"C:\\{TestSpanMapper.GeneratedFileName}"),
Range = new LSP.Range { Start = position, End = position }
});
AssertLocationsEqual(ImmutableArray.Create(TestSpanMapper.MappedFileLocation), results);
AssertLocationsEqual([TestSpanMapper.MappedFileLocation], results);
}

[Theory, CombinatorialData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void IA.M()
Uri = ProtocolConversions.CreateAbsoluteUri($"C:\\{TestSpanMapper.GeneratedFileName}"),
Range = new LSP.Range { Start = position, End = position }
});
AssertLocationsEqual(ImmutableArray.Create(TestSpanMapper.MappedFileLocation), results);
AssertLocationsEqual([TestSpanMapper.MappedFileLocation], results);
}

[Theory, CombinatorialData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private class MockAnalyzerReference : AnalyzerReference
public readonly ImmutableArray<DiagnosticAnalyzer> Analyzers;

private static readonly CodeFixProvider s_defaultFixer = new MockFixer();
private static readonly ImmutableArray<DiagnosticAnalyzer> s_defaultAnalyzers = ImmutableArray.Create<DiagnosticAnalyzer>(new MockDiagnosticAnalyzer());
private static readonly ImmutableArray<DiagnosticAnalyzer> s_defaultAnalyzers = [new MockDiagnosticAnalyzer()];

public MockAnalyzerReference(CodeFixProvider? fixer, ImmutableArray<DiagnosticAnalyzer> analyzers)
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public override ImmutableArray<DiagnosticAnalyzer> GetAnalyzersForAllLanguages()
=> Analyzers;

public ImmutableArray<CodeFixProvider> GetFixers()
=> Fixer != null ? ImmutableArray.Create(Fixer) : ImmutableArray<CodeFixProvider>.Empty;
=> Fixer != null ? [Fixer] : ImmutableArray<CodeFixProvider>.Empty;

public class MockFixer : CodeFixProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ private async Task AssertResetInteractiveAsync(
CreateImport,
buildSucceeds: buildSucceeds)
{
References = ImmutableArray.CreateRange(GetProjectReferences(workspace, project)),
ReferenceSearchPaths = ImmutableArray.Create("rsp1", "rsp2"),
SourceSearchPaths = ImmutableArray.Create("ssp1", "ssp2"),
ProjectNamespaces = ImmutableArray.Create("System", "ResetInteractiveTestsDocument", "VisualBasicResetInteractiveTestsDocument"),
NamespacesToImport = ImmutableArray.Create("System", "ResetInteractiveTestsDocument"),
References = [.. GetProjectReferences(workspace, project)],
ReferenceSearchPaths = ["rsp1", "rsp2"],
SourceSearchPaths = ["ssp1", "ssp2"],
ProjectNamespaces = ["System", "ResetInteractiveTestsDocument", "VisualBasicResetInteractiveTestsDocument"],
NamespacesToImport = ["System", "ResetInteractiveTestsDocument"],
ProjectDirectory = "pj",
Platform = InteractiveHostPlatform.Desktop64,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task RuleSet_GeneralOption_CPS()

Assert.Equal(expected: ReportDiagnostic.Default, actual: options.GeneralDiagnosticOption);

project.SetOptions(ImmutableArray.Create($"/ruleset:{ruleSetFile.Path}"));
project.SetOptions([$"/ruleset:{ruleSetFile.Path}"]);

workspaceProject = environment.Workspace.CurrentSolution.Projects.Single();
options = (CSharpCompilationOptions)workspaceProject.CompilationOptions;
Expand All @@ -60,10 +60,10 @@ public async Task RuleSet_SpecificOptions_CPS()
using var environment = new TestEnvironment();
using var project = await CSharpHelpers.CreateCSharpCPSProjectAsync(environment, "Test");
// Verify SetRuleSetFile updates the ruleset.
project.SetOptions(ImmutableArray.Create($"/ruleset:{ruleSetFile.Path}"));
project.SetOptions([$"/ruleset:{ruleSetFile.Path}"]);

// We need to explicitly update the command line arguments so the new ruleset is used to update options.
project.SetOptions(ImmutableArray.Create($"/ruleset:{ruleSetFile.Path}"));
project.SetOptions([$"/ruleset:{ruleSetFile.Path}"]);
var ca1012DiagnosticOption = environment.Workspace.CurrentSolution.Projects.Single().CompilationOptions.SpecificDiagnosticOptions["CA1012"];
Assert.Equal(expected: ReportDiagnostic.Error, actual: ca1012DiagnosticOption);
}
Expand All @@ -77,7 +77,7 @@ public async Task RuleSet_PathCanBeFound()

using (var project = await CSharpHelpers.CreateCSharpCPSProjectAsync(environment, "Test"))
{
project.SetOptions(ImmutableArray.Create($"/ruleset:{ruleSetFile.Path}"));
project.SetOptions([$"/ruleset:{ruleSetFile.Path}"]);

projectId = project.Id;

Expand Down
Loading

0 comments on commit fc6ddbf

Please sign in to comment.