Skip to content

Commit eddff78

Browse files
committed
Add Analyzer to prevent empty ArraySegment collection expression
see 83d04b0
1 parent 14a3f64 commit eddff78

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

Directory.Packages.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<PackageVersion Include="Meziantou.Polyfill" Version="1.0.50" />
1313
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="6.0.0" />
1414
<PackageVersion Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
15-
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
16-
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
15+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
16+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" />
1717
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
1818
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
1919
<PackageVersion Include="Microsoft.Data.Sqlite.Core" Version="8.0.4" />
@@ -34,7 +34,7 @@
3434
<PackageVersion Include="Silk.NET.WGL.Extensions.NV" Version="2.21.0" />
3535
<PackageVersion Include="SQLitePCLRaw.provider.e_sqlite3" Version="2.1.8" />
3636
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" /><!-- don't forget to update .stylecop.json at the same time -->
37-
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" />
37+
<PackageVersion Include="System.Collections.Immutable" Version="9.0.0" />
3838
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
3939
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
4040
<PackageVersion Include="System.Drawing.Common" Version="6.0.0" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace BizHawk.Analyzers;
2+
3+
using System.Collections.Immutable;
4+
5+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
6+
public sealed class BrokenCollectionExpressionAnalyzer : DiagnosticAnalyzer
7+
{
8+
private static readonly DiagnosticDescriptor DiagBrokenCollectionExpression = new(
9+
id: "BHI1234",
10+
title: "don't this",
11+
messageFormat: "don't this",
12+
category: "Usage",
13+
defaultSeverity: DiagnosticSeverity.Warning,
14+
isEnabledByDefault: true);
15+
16+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
17+
= ImmutableArray.Create(/*HawkSourceAnalyzer.DiagWTF,*/ DiagBrokenCollectionExpression);
18+
19+
public override void Initialize(AnalysisContext context)
20+
{
21+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
22+
context.EnableConcurrentExecution();
23+
context.RegisterCompilationStartAction(initContext =>
24+
{
25+
var arraySegmentSym = initContext.Compilation.GetTypeByMetadataName("System.ArraySegment`1")!;
26+
initContext.RegisterOperationAction(
27+
oac =>
28+
{
29+
var operation = (ICollectionExpressionOperation) oac.Operation;
30+
if (arraySegmentSym.Matches(operation.Type.OriginalDefinition)) DiagBrokenCollectionExpression.ReportAt(operation, oac);
31+
},
32+
OperationKind.CollectionExpression);
33+
});
34+
}
35+
}

References/BizHawk.Analyzer.dll

1 KB
Binary file not shown.

0 commit comments

Comments
 (0)