Skip to content

Commit 7959d59

Browse files
committed
WIP Add Analyzer to prevent empty ArraySegment collection expression
1 parent 42a9bc6 commit 7959d59

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
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)) DiagBrokenCollectionExpression.ReportAt(operation, oac);
31+
},
32+
OperationKind.CollectionExpression);
33+
});
34+
}
35+
}

0 commit comments

Comments
 (0)