Use `SpecialType` where possible in BizHawk.Analyzer

This commit is contained in:
YoshiRulz 2025-01-31 17:52:20 +10:00
parent ae97d1f49d
commit de1a7b1e30
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
5 changed files with 14 additions and 21 deletions

View File

@ -26,27 +26,24 @@ public sealed class AmbiguousMoneyToFloatConversionAnalyzer : DiagnosticAnalyzer
context.EnableConcurrentExecution(); context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(initContext => context.RegisterCompilationStartAction(initContext =>
{ {
var decimalSym = initContext.Compilation.GetTypeByMetadataName("System.Decimal")!;
var doubleSym = initContext.Compilation.GetTypeByMetadataName("System.Double")!;
var floatSym = initContext.Compilation.GetTypeByMetadataName("System.Single")!;
initContext.RegisterOperationAction(oac => initContext.RegisterOperationAction(oac =>
{ {
var conversionOp = (IConversionOperation) oac.Operation; var conversionOp = (IConversionOperation) oac.Operation;
var typeOutput = conversionOp.Type; var typeOutput = conversionOp.Type?.SpecialType ?? SpecialType.None;
var typeInput = conversionOp.Operand.Type; var typeInput = conversionOp.Operand.Type?.SpecialType ?? SpecialType.None;
bool isToDecimal; bool isToDecimal;
bool isDoublePrecision; bool isDoublePrecision;
if (decimalSym.Matches(typeOutput)) if (typeOutput is SpecialType.System_Decimal)
{ {
if (doubleSym.Matches(typeInput)) isDoublePrecision = true; if (typeInput is SpecialType.System_Double) isDoublePrecision = true;
else if (floatSym.Matches(typeInput)) isDoublePrecision = false; else if (typeInput is SpecialType.System_Single) isDoublePrecision = false;
else return; else return;
isToDecimal = true; isToDecimal = true;
} }
else if (decimalSym.Matches(typeInput)) else if (typeInput is SpecialType.System_Decimal)
{ {
if (doubleSym.Matches(typeOutput)) isDoublePrecision = true; if (typeOutput is SpecialType.System_Double) isDoublePrecision = true;
else if (floatSym.Matches(typeOutput)) isDoublePrecision = false; else if (typeOutput is SpecialType.System_Single) isDoublePrecision = false;
else return; else return;
isToDecimal = false; isToDecimal = false;
} }

View File

@ -25,8 +25,6 @@ public sealed class TernaryInferredTypeMismatchAnalyzer : DiagnosticAnalyzer
context.EnableConcurrentExecution(); context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(initContext => context.RegisterCompilationStartAction(initContext =>
{ {
var objectSym = initContext.Compilation.GetTypeByMetadataName("System.Object")!;
var stringSym = initContext.Compilation.GetTypeByMetadataName("System.String")!;
initContext.RegisterOperationAction(oac => initContext.RegisterOperationAction(oac =>
{ {
var ifelseOrTernaryOp = (IConditionalOperation) oac.Operation; var ifelseOrTernaryOp = (IConditionalOperation) oac.Operation;
@ -37,7 +35,7 @@ public sealed class TernaryInferredTypeMismatchAnalyzer : DiagnosticAnalyzer
var ternaryOp = ifelseOrTernaryOp; var ternaryOp = ifelseOrTernaryOp;
var typeTernary = ternaryOp.Type!; var typeTernary = ternaryOp.Type!;
#if false // never hit; either both branches are string and there are no conversions, or conversions are necessary #if false // never hit; either both branches are string and there are no conversions, or conversions are necessary
if (stringSym.Matches(typeTernary)) return; if (typeTernary.SpecialType is SpecialType.System_String) return;
#endif #endif
var lhs = ternaryOp.WhenTrue; var lhs = ternaryOp.WhenTrue;
var rhs = ternaryOp.WhenFalse; var rhs = ternaryOp.WhenFalse;
@ -52,17 +50,17 @@ public sealed class TernaryInferredTypeMismatchAnalyzer : DiagnosticAnalyzer
var fatal = false; var fatal = false;
IOperation flaggedOp = ternaryOp; IOperation flaggedOp = ternaryOp;
string message; string message;
if (stringSym.Matches(typeLHS)) if (typeLHS.SpecialType is SpecialType.System_String)
{ {
flaggedOp = rhs; flaggedOp = rhs;
message = ERR_MSG_OBJECT; message = ERR_MSG_OBJECT;
} }
else if (stringSym.Matches(typeRHS)) else if (typeRHS.SpecialType is SpecialType.System_String)
{ {
flaggedOp = lhs; flaggedOp = lhs;
message = ERR_MSG_OBJECT; message = ERR_MSG_OBJECT;
} }
else if (objectSym.Matches(typeTernary)) else if (typeTernary.SpecialType is SpecialType.System_Object)
{ {
fatal = true; fatal = true;
message = "ternary branches are upcast to object! add ToString calls, or convert one to the other's type"; message = "ternary branches are upcast to object! add ToString calls, or convert one to the other's type";

View File

@ -28,7 +28,6 @@ public sealed class UseSimplerBoolFlipAnalyzer : DiagnosticAnalyzer
{ {
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution(); context.EnableConcurrentExecution();
ISymbol? boolSym = null;
context.RegisterOperationAction( context.RegisterOperationAction(
oac => oac =>
{ {
@ -43,8 +42,7 @@ public sealed class UseSimplerBoolFlipAnalyzer : DiagnosticAnalyzer
} }
var operation = (ICompoundAssignmentOperation) oac.Operation; var operation = (ICompoundAssignmentOperation) oac.Operation;
if (operation.OperatorKind is not BinaryOperatorKind.ExclusiveOr) return; if (operation.OperatorKind is not BinaryOperatorKind.ExclusiveOr) return;
boolSym ??= oac.Compilation.GetTypeByMetadataName("System.Boolean")!; if (operation.Type?.SpecialType is not SpecialType.System_Boolean) return;
if (!boolSym.Matches(operation.Type)) return;
if (operation.Value.Kind is not OperationKind.Literal) return; if (operation.Value.Kind is not OperationKind.Literal) return;
var lhsOp = operation.Target; var lhsOp = operation.Target;
bool lhsIsSimpleExpr; bool lhsIsSimpleExpr;

View File

@ -39,7 +39,7 @@ public sealed class UseTypeofOperatorAnalyzer : DiagnosticAnalyzer
{ {
var operation = (IInvocationOperation) oac.Operation; var operation = (IInvocationOperation) oac.Operation;
if (operation.IsImplicit || operation.Instance is null) return; if (operation.IsImplicit || operation.Instance is null) return;
objectDotGetTypeSym ??= oac.Compilation.GetTypeByMetadataName("System.Object")!.GetMembers("GetType")[0]; objectDotGetTypeSym ??= oac.Compilation.GetSpecialType(SpecialType.System_Object).GetMembers("GetType")[0];
if (!objectDotGetTypeSym.Matches(operation.TargetMethod)) return; if (!objectDotGetTypeSym.Matches(operation.TargetMethod)) return;
if (operation.Instance.Syntax is not ThisExpressionSyntax and not IdentifierNameSyntax { Identifier.Text: "GetType" }) return; // called on something that isn't `this` if (operation.Instance.Syntax is not ThisExpressionSyntax and not IdentifierNameSyntax { Identifier.Text: "GetType" }) return; // called on something that isn't `this`
var enclosingType = operation.SemanticModel!.GetDeclaredSymbol( var enclosingType = operation.SemanticModel!.GetDeclaredSymbol(

Binary file not shown.