Fix BizHawk.Analyzer potentially throwing

Co-Authored-By: James Groom <13409956+YoshiRulz@users.noreply.github.com>
This commit is contained in:
Morilli 2024-04-20 17:00:26 +02:00
parent 17cf58c964
commit fd36a37f17
2 changed files with 8 additions and 2 deletions

View File

@ -1,5 +1,6 @@
namespace BizHawk.Analyzers;
using System;
using System.Collections.Immutable;
using System.Linq;
@ -42,8 +43,13 @@ public sealed class FirstOrDefaultOnStructAnalyzer : DiagnosticAnalyzer
var operation = (IInvocationOperation) oac.Operation;
var calledSym = operation.TargetMethod.ConstructedFrom;
if (!(firstOrDefaultWithPredSym!.Matches(calledSym) || firstOrDefaultNoPredSym!.Matches(calledSym))) return;
var receiverExprType = (INamedTypeSymbol) operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax)!.ConvertedType!;
if (receiverExprType.TypeArguments[0].IsValueType) oac.ReportDiagnostic(Diagnostic.Create(DiagUseFirstOrNull, operation.Syntax.GetLocation()));
var receiverExprType = operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax).ConvertedType!;
var collectionElemType = receiverExprType switch {
INamedTypeSymbol nts => nts.TypeArguments[0],
IArrayTypeSymbol ats => ats.ElementType,
_ => throw new InvalidOperationException($"receiver parameter's effective type was of an unexpected kind (neither class/struct nor array): {receiverExprType}")
};
if (collectionElemType.IsValueType) oac.ReportDiagnostic(Diagnostic.Create(DiagUseFirstOrNull, operation.Syntax.GetLocation()));
},
OperationKind.Invocation);
});

Binary file not shown.