Skip to content

Commit fd36a37

Browse files
MorilliYoshiRulz
andcommitted
Fix BizHawk.Analyzer potentially throwing
Co-Authored-By: James Groom <[email protected]>
1 parent 17cf58c commit fd36a37

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

ExternalProjects/BizHawk.Analyzer/FirstOrDefaultOnStructAnalyzer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace BizHawk.Analyzers;
22

3+
using System;
34
using System.Collections.Immutable;
45
using System.Linq;
56

@@ -42,8 +43,13 @@ public override void Initialize(AnalysisContext context)
4243
var operation = (IInvocationOperation) oac.Operation;
4344
var calledSym = operation.TargetMethod.ConstructedFrom;
4445
if (!(firstOrDefaultWithPredSym!.Matches(calledSym) || firstOrDefaultNoPredSym!.Matches(calledSym))) return;
45-
var receiverExprType = (INamedTypeSymbol) operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax)!.ConvertedType!;
46-
if (receiverExprType.TypeArguments[0].IsValueType) oac.ReportDiagnostic(Diagnostic.Create(DiagUseFirstOrNull, operation.Syntax.GetLocation()));
46+
var receiverExprType = operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax).ConvertedType!;
47+
var collectionElemType = receiverExprType switch {
48+
INamedTypeSymbol nts => nts.TypeArguments[0],
49+
IArrayTypeSymbol ats => ats.ElementType,
50+
_ => throw new InvalidOperationException($"receiver parameter's effective type was of an unexpected kind (neither class/struct nor array): {receiverExprType}")
51+
};
52+
if (collectionElemType.IsValueType) oac.ReportDiagnostic(Diagnostic.Create(DiagUseFirstOrNull, operation.Syntax.GetLocation()));
4753
},
4854
OperationKind.Invocation);
4955
});

References/BizHawk.Analyzer.dll

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)