Fix BizHawk.Analyzer potentially throwing
Co-Authored-By: James Groom <13409956+YoshiRulz@users.noreply.github.com>
This commit is contained in:
parent
17cf58c964
commit
fd36a37f17
|
@ -1,5 +1,6 @@
|
||||||
namespace BizHawk.Analyzers;
|
namespace BizHawk.Analyzers;
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
@ -42,8 +43,13 @@ public sealed class FirstOrDefaultOnStructAnalyzer : DiagnosticAnalyzer
|
||||||
var operation = (IInvocationOperation) oac.Operation;
|
var operation = (IInvocationOperation) oac.Operation;
|
||||||
var calledSym = operation.TargetMethod.ConstructedFrom;
|
var calledSym = operation.TargetMethod.ConstructedFrom;
|
||||||
if (!(firstOrDefaultWithPredSym!.Matches(calledSym) || firstOrDefaultNoPredSym!.Matches(calledSym))) return;
|
if (!(firstOrDefaultWithPredSym!.Matches(calledSym) || firstOrDefaultNoPredSym!.Matches(calledSym))) return;
|
||||||
var receiverExprType = (INamedTypeSymbol) operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax)!.ConvertedType!;
|
var receiverExprType = operation.SemanticModel!.GetTypeInfo((CSharpSyntaxNode) operation.Arguments[0].Syntax).ConvertedType!;
|
||||||
if (receiverExprType.TypeArguments[0].IsValueType) oac.ReportDiagnostic(Diagnostic.Create(DiagUseFirstOrNull, operation.Syntax.GetLocation()));
|
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);
|
OperationKind.Invocation);
|
||||||
});
|
});
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue