Skip to content

Commit ac9b03d

Browse files
authored
Code clean-up (Buildalyzer#314)
1 parent 190ac32 commit ac9b03d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+134
-189
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ root = true
66
# Don't use tabs for indentation.
77
[*]
88
indent_style = space
9+
insert_final_newline = true
910
charset = utf-8
1011
# (Please don't specify an indent_size here; that has too many unintended consequences.)
1112

Directory.Build.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PublishRepositoryUrl>true</PublishRepositoryUrl>
99
<IncludeSymbols>true</IncludeSymbols>
1010
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
11+
<DebugType>portable</DebugType>
1112
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1213
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1314
</PropertyGroup>
@@ -20,6 +21,7 @@
2021
<LangVersion>12</LangVersion>
2122
<Nullable>enable</Nullable>
2223
<EnableNETAnalyzers>true</EnableNETAnalyzers>
24+
<NuGetAuditMode>all</NuGetAuditMode>
2325
<IsPublishable>false</IsPublishable>
2426
</PropertyGroup>
2527

@@ -58,7 +60,7 @@
5860
</ItemGroup>
5961

6062
<ItemGroup Label="Analyzers">
61-
<PackageReference Include="DotNetProjectFile.Analyzers" Version="*" PrivateAssets="all" Condition="'$(TargetFramework)'=='net8.0'" />
63+
<PackageReference Include="DotNetProjectFile.Analyzers" Version="*" PrivateAssets="all" />
6264
<PackageReference Include="IDisposableAnalyzers" Version="*" PrivateAssets="all"/>
6365
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="*" PrivateAssets="all" />
6466
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="*" PrivateAssets="all"/>

src/Buildalyzer.Logger/BuildalyzerLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BuildalyzerLogger : PipeLogger
1313
public override void Initialize(IEventSource eventSource)
1414
{
1515
// Parse the parameters
16-
string[] parameters = [..(Parameters?.Split(';').Select(x => x.Trim())).OfType<string>()];
16+
string[] parameters = [.. (Parameters?.Split(';').Select(x => x.Trim())).OfType<string>()];
1717
if (parameters.Length != 2)
1818
{
1919
throw new LoggerException("Unexpected number of parameters");

src/Buildalyzer.Workspaces/AnalyzerResultExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static Project AddToWorkspace(this IAnalyzerResult analyzerResult, Worksp
5959
ProjectId projectId = ProjectId.CreateFromSerialized(analyzerResult.ProjectGuid);
6060

6161
// Cache the project references
62-
analyzerResult.Manager.WorkspaceProjectReferences[projectId.Id] = analyzerResult.ProjectReferences.ToArray();
62+
analyzerResult.Manager.WorkspaceProjectReferences[projectId.Id] = [.. analyzerResult.ProjectReferences];
6363

6464
// Create and add the project, but only if it's a support Roslyn project type
6565
ProjectInfo projectInfo = GetProjectInfo(analyzerResult, workspace, projectId);
@@ -92,7 +92,7 @@ public static Project AddToWorkspace(this IAnalyzerResult analyzerResult, Worksp
9292
// Add any project references not already added
9393
if (addProjectReferences)
9494
{
95-
foreach (ProjectAnalyzer referencedAnalyzer in GetReferencedAnalyzerProjects(analyzerResult))
95+
foreach (var referencedAnalyzer in GetReferencedAnalyzerProjects(analyzerResult))
9696
{
9797
// Check if the workspace contains the project inside the loop since adding one might also add this one due to transitive references
9898
if (!workspace.CurrentSolution.Projects.Any(x => x.FilePath == referencedAnalyzer.ProjectFile.Path))
@@ -104,8 +104,8 @@ public static Project AddToWorkspace(this IAnalyzerResult analyzerResult, Worksp
104104

105105
// By now all the references of this project have been recursively added, so resolve any remaining transitive project references
106106
Project project = workspace.CurrentSolution.GetProject(projectId);
107-
HashSet<ProjectReference> referencedProjects = new HashSet<ProjectReference>(project.ProjectReferences);
108-
HashSet<ProjectId> visitedProjectIds = new HashSet<ProjectId>();
107+
HashSet<ProjectReference> referencedProjects = [.. project.ProjectReferences];
108+
HashSet<ProjectId> visitedProjectIds = [];
109109
Stack<ProjectReference> projectReferenceStack = new Stack<ProjectReference>(project.ProjectReferences);
110110
while (projectReferenceStack.Count > 0)
111111
{

src/Buildalyzer/AnalyzerManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class AnalyzerManager : IAnalyzerManager
1616
SolutionProjectType.WebProject
1717
];
1818

19-
private readonly ConcurrentDictionary<string, IProjectAnalyzer> _projects = new ConcurrentDictionary<string, IProjectAnalyzer>();
19+
private readonly ConcurrentDictionary<string, IProjectAnalyzer> _projects = new();
2020

2121
public IReadOnlyDictionary<string, IProjectAnalyzer> Projects => _projects;
2222

@@ -32,7 +32,7 @@ public class AnalyzerManager : IAnalyzerManager
3232
/// This cache exists in <see cref="AnalyzerManager"/> so that it's lifetime can be controlled and it can be collected when <see cref="AnalyzerManager"/> goes out of scope.
3333
/// </summary>
3434
#pragma warning disable SA1401 // Fields should be private
35-
internal ConcurrentDictionary<Guid, string[]> WorkspaceProjectReferences = new ConcurrentDictionary<Guid, string[]>();
35+
internal ConcurrentDictionary<Guid, string[]> WorkspaceProjectReferences = new();
3636
#pragma warning restore SA1401 // Fields should be private
3737

3838
public string SolutionFilePath { get; }

src/Buildalyzer/AnalyzerResult.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace Buildalyzer;
77
[DebuggerDisplay("{DebuggerDisplay}")]
88
public class AnalyzerResult : IAnalyzerResult
99
{
10-
private readonly Dictionary<string, string> _properties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
11-
private readonly Dictionary<string, IProjectItem[]> _items = new Dictionary<string, IProjectItem[]>(StringComparer.OrdinalIgnoreCase);
10+
private readonly Dictionary<string, string> _properties = new(StringComparer.OrdinalIgnoreCase);
11+
private readonly Dictionary<string, IProjectItem[]> _items = new(StringComparer.OrdinalIgnoreCase);
1212
private readonly Guid _projectGuid;
1313

1414
public CompilerCommand CompilerCommand { get; private set; }
@@ -71,7 +71,7 @@ public string GetProperty(string name) =>
7171
public string[] References =>
7272
CompilerCommand?.MetadataReferences.ToArray() ?? [];
7373

74-
public ImmutableDictionary<string, ImmutableArray<string>> ReferenceAliases =>
74+
public ImmutableDictionary<string, ImmutableArray<string>> ReferenceAliases =>
7575
CompilerCommand?.Aliases ?? ImmutableDictionary<string, ImmutableArray<string>>.Empty;
7676

7777
public string[] AnalyzerReferences =>
@@ -141,7 +141,7 @@ internal void ProcessProject(PropertiesAndItems propertiesAndItems)
141141
// Add items
142142
foreach (var items in propertiesAndItems.Items)
143143
{
144-
_items[items.Key] = items.Values.Select(task => new ProjectItem(task)).ToArray();
144+
_items[items.Key] = [.. items.Values.Select(task => new ProjectItem(task))];
145145
}
146146
}
147147

@@ -165,7 +165,7 @@ internal void ProcessFscCommandLine(string commandLine)
165165
CompilerCommand = Compiler.CommandLine.Parse(new FileInfo(ProjectFilePath).Directory, commandLine, CompilerLanguage.FSharp);
166166
}
167167

168-
private class ProjectItemItemSpecEqualityComparer : IEqualityComparer<IProjectItem>
168+
private sealed class ProjectItemItemSpecEqualityComparer : IEqualityComparer<IProjectItem>
169169
{
170170
public bool Equals(IProjectItem x, IProjectItem y) => x.ItemSpec.Equals(y.ItemSpec, StringComparison.OrdinalIgnoreCase);
171171

src/Buildalyzer/AnalyzerResults.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Buildalyzer;
55

66
public class AnalyzerResults : IAnalyzerResults
77
{
8-
private readonly ConcurrentDictionary<string, IAnalyzerResult> _results = new ConcurrentDictionary<string, IAnalyzerResult>();
8+
private readonly ConcurrentDictionary<string, IAnalyzerResult> _results = new();
99

1010
private bool? _overallSuccess = null;
1111

@@ -39,7 +39,7 @@ internal void Add(IEnumerable<IAnalyzerResult> results, bool overallSuccess)
3939

4040
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
4141

42-
private class TargetFrameworkComparer : IComparer<string>
42+
private sealed class TargetFrameworkComparer : IComparer<string>
4343
{
4444
public static TargetFrameworkComparer Instance { get; } = new TargetFrameworkComparer();
4545

src/Buildalyzer/Compiler/CSharpCompilerCommand.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable enable
2-
31
using Microsoft.CodeAnalysis.CSharp;
42

53
namespace Buildalyzer;

src/Buildalyzer/Compiler/Compiler.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable enable
2-
31
using System.IO;
42
using Buildalyzer.IO;
53
using Microsoft.CodeAnalysis;
@@ -32,7 +30,7 @@ public static CompilerCommand Parse(DirectoryInfo? baseDir, string commandLine,
3230
{
3331
Text = commandLine,
3432
CompilerLocation = location,
35-
Arguments = args.ToImmutableArray(),
33+
Arguments = [.. args],
3634
};
3735

3836
static CompilerCommand Parse(string? baseDir, string? root, string[] args, CompilerLanguage language) => language switch
@@ -85,9 +83,9 @@ public static FSharpCompilerCommand Parse(string[] args)
8583

8684
return new()
8785
{
88-
MetadataReferences = metadataReferences.ToImmutableArray(),
89-
PreprocessorSymbolNames = preprocessorSymbolNames.ToImmutableArray(),
90-
SourceFiles = sourceFiles.ToImmutableArray(),
86+
MetadataReferences = [.. metadataReferences],
87+
PreprocessorSymbolNames = [.. preprocessorSymbolNames],
88+
SourceFiles = [.. sourceFiles],
9189
};
9290
}
9391
}
@@ -99,14 +97,14 @@ public static TCommand Enrich<TCommand>(TCommand command, CommandLineArguments a
9997

10098
=> command with
10199
{
102-
AnalyzerReferences = arguments.AnalyzerReferences.Select(AsIOPath).ToImmutableArray(),
103-
AnalyzerConfigPaths = arguments.AnalyzerConfigPaths.Select(IOPath.Parse).ToImmutableArray(),
104-
MetadataReferences = arguments.MetadataReferences.Select(m => m.Reference).ToImmutableArray(),
100+
AnalyzerReferences = [.. arguments.AnalyzerReferences.Select(AsIOPath)],
101+
AnalyzerConfigPaths = [.. arguments.AnalyzerConfigPaths.Select(IOPath.Parse)],
102+
MetadataReferences = [.. arguments.MetadataReferences.Select(m => m.Reference)],
105103
Aliases = arguments.MetadataReferences.Where(m => !m.Properties.Aliases.IsEmpty).ToImmutableDictionary(m => m.Reference, m => m.Properties.Aliases),
106-
PreprocessorSymbolNames = arguments.ParseOptions.PreprocessorSymbolNames.ToImmutableArray(),
107-
SourceFiles = arguments.SourceFiles.Select(AsIOPath).ToImmutableArray(),
108-
AdditionalFiles = arguments.AdditionalFiles.Select(AsIOPath).ToImmutableArray(),
109-
EmbeddedFiles = arguments.EmbeddedFiles.Select(AsIOPath).ToImmutableArray(),
104+
PreprocessorSymbolNames = [.. arguments.ParseOptions.PreprocessorSymbolNames],
105+
SourceFiles = [.. arguments.SourceFiles.Select(AsIOPath)],
106+
AdditionalFiles = [.. arguments.AdditionalFiles.Select(AsIOPath)],
107+
EmbeddedFiles = [.. arguments.EmbeddedFiles.Select(AsIOPath)],
110108
};
111109
}
112110

src/Buildalyzer/Compiler/CompilerCommand.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#nullable enable
2-
31
using System.IO;
42
using Buildalyzer.IO;
53
using Microsoft.CodeAnalysis;
@@ -16,39 +14,39 @@ public abstract record CompilerCommand
1614
public string Text { get; init; } = string.Empty;
1715

1816
/// <summary>The parsed command line arguments.</summary>
19-
public ImmutableArray<string> Arguments { get; init; }
17+
public ImmutableArray<string> Arguments { get; init; } = [];
2018

2119
/// <summary>The location of the used compiler.</summary>
2220
public FileInfo? CompilerLocation { get; init; }
2321

2422
/// <inheritdoc cref="CommandLineArguments.Errors" />
25-
public ImmutableArray<Diagnostic> Errors { get; init; }
23+
public ImmutableArray<Diagnostic> Errors { get; init; } = [];
2624

2725
/// <inheritdoc cref="CommandLineArguments.SourceFiles" />
28-
public ImmutableArray<IOPath> SourceFiles { get; init; }
26+
public ImmutableArray<IOPath> SourceFiles { get; init; } = [];
2927

3028
/// <inheritdoc cref="CommandLineArguments.AdditionalFiles" />
31-
public ImmutableArray<IOPath> AdditionalFiles { get; init; }
29+
public ImmutableArray<IOPath> AdditionalFiles { get; init; } = [];
3230

3331
/// <inheritdoc cref="CommandLineArguments.EmbeddedFiles" />
34-
public ImmutableArray<IOPath> EmbeddedFiles { get; init; }
32+
public ImmutableArray<IOPath> EmbeddedFiles { get; init; } = [];
3533

3634
/// <inheritdoc cref="CommandLineArguments.AnalyzerReferences" />
37-
public ImmutableArray<IOPath> AnalyzerReferences { get; init; }
35+
public ImmutableArray<IOPath> AnalyzerReferences { get; init; } = [];
3836

3937
/// <inheritdoc cref="CommandLineArguments.AnalyzerConfigPaths" />
40-
public ImmutableArray<IOPath> AnalyzerConfigPaths { get; init; }
38+
public ImmutableArray<IOPath> AnalyzerConfigPaths { get; init; } = [];
4139

4240
/// <inheritdoc cref="ParseOptions.PreprocessorSymbolNames" />
43-
public ImmutableArray<string> PreprocessorSymbolNames { get; init; }
41+
public ImmutableArray<string> PreprocessorSymbolNames { get; init; } = [];
4442

45-
/// <inheritdoc cref="CommandLineArguments.MetadataReferences" />
46-
public ImmutableArray<string> MetadataReferences { get; init; }
43+
/// <inheritdoc cref="CommandLineArguments.MetadataReferences" />
44+
public ImmutableArray<string> MetadataReferences { get; init; } = [];
4745

4846
/// <summary>
4947
/// The aliases used in the command line arguments.
5048
/// </summary>
51-
public ImmutableDictionary<string, ImmutableArray<string>> Aliases { get; init; }
49+
public ImmutableDictionary<string, ImmutableArray<string>> Aliases { get; init; } = ImmutableDictionary<string, ImmutableArray<string>>.Empty;
5250

5351
/// <inheritdoc />
5452
[Pure]

0 commit comments

Comments
 (0)