From 26e02033f2061a4a81a4a0d0202d86f2e6828a3c Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 15 Jul 2022 20:50:31 +1000 Subject: [PATCH] Do the analysis for BHI1005 properly --- .../BizHawk.Analyzer/HawkSourceAnalyzer.cs | 31 +++++++++--------- .../BizHawk.Analyzer/RoslynUtils.cs | 18 ++++++++++ References/BizHawk.Analyzer.dll | Bin 12288 -> 12800 bytes 3 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 ExternalProjects/BizHawk.Analyzer/RoslynUtils.cs diff --git a/ExternalProjects/BizHawk.Analyzer/HawkSourceAnalyzer.cs b/ExternalProjects/BizHawk.Analyzer/HawkSourceAnalyzer.cs index 33eeda8dff..c5e38c007b 100644 --- a/ExternalProjects/BizHawk.Analyzer/HawkSourceAnalyzer.cs +++ b/ExternalProjects/BizHawk.Analyzer/HawkSourceAnalyzer.cs @@ -1,6 +1,5 @@ namespace BizHawk.Analyzers; -using System; using System.Collections.Immutable; using Microsoft.CodeAnalysis; @@ -66,9 +65,16 @@ public class HawkSourceAnalyzer : DiagnosticAnalyzer { context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); context.EnableConcurrentExecution(); + INamedTypeSymbol? invalidOperationExceptionSym = null; + INamedTypeSymbol? switchExpressionExceptionSym = null; context.RegisterSyntaxNodeAction( - static snac => + snac => { + if (invalidOperationExceptionSym is null) + { + invalidOperationExceptionSym = snac.Compilation.GetTypeByMetadataName("System.InvalidOperationException")!; + switchExpressionExceptionSym = snac.Compilation.GetTypeByMetadataName("System.Runtime.CompilerServices.SwitchExpressionException"); + } switch (snac.Node) { case AnonymousMethodExpressionSyntax: @@ -84,21 +90,9 @@ public class HawkSourceAnalyzer : DiagnosticAnalyzer snac.ReportDiagnostic(Diagnostic.Create(DiagNoQueryExpression, snac.Node.GetLocation())); break; case SwitchExpressionArmSyntax { WhenClause: null, Pattern: DiscardPatternSyntax, Expression: ThrowExpressionSyntax tes }: - if (tes.Expression is ObjectCreationExpressionSyntax oces) + var thrownExceptionType = snac.SemanticModel.GetThrownExceptionType(tes); + if (thrownExceptionType is null) { - // to resolve edge-cases involving aliases, you're supposed to use `snac.SemanticModel.GetTypeInfo(oces.Type).ConvertedType?.Name`, but I couldn't get it to work - if (((oces.Type as IdentifierNameSyntax)?.Identifier)?.ToString() is "SwitchExpressionException" or nameof(InvalidOperationException)) - { - // correct usage, do not flag - } - else - { - snac.ReportDiagnostic(Diagnostic.Create(DiagSwitchShouldThrowIOE, tes.GetLocation(), ERR_MSG_SWITCH_THROWS_WRONG_TYPE)); - } - } - else - { - // code reads `throw ` snac.ReportDiagnostic(Diagnostic.Create( DiagSwitchShouldThrowIOE, tes.GetLocation(), @@ -107,6 +101,11 @@ public class HawkSourceAnalyzer : DiagnosticAnalyzer properties: null, ERR_MSG_SWITCH_THROWS_UNKNOWN)); } + else if (!invalidOperationExceptionSym.Matches(thrownExceptionType) && switchExpressionExceptionSym?.Matches(thrownExceptionType) != true) + { + snac.ReportDiagnostic(Diagnostic.Create(DiagSwitchShouldThrowIOE, tes.GetLocation(), ERR_MSG_SWITCH_THROWS_WRONG_TYPE)); + } + // else correct usage, do not flag break; } }, diff --git a/ExternalProjects/BizHawk.Analyzer/RoslynUtils.cs b/ExternalProjects/BizHawk.Analyzer/RoslynUtils.cs new file mode 100644 index 0000000000..deaa57b34d --- /dev/null +++ b/ExternalProjects/BizHawk.Analyzer/RoslynUtils.cs @@ -0,0 +1,18 @@ +namespace BizHawk.Analyzers; + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +internal static class RoslynUtils +{ + private static ITypeSymbol? GetThrownExceptionType(this SemanticModel model, ExpressionSyntax exprSyn) + => exprSyn is ObjectCreationExpressionSyntax oces + ? model.GetTypeInfo(exprSyn).Type + : null; // code reads `throw ` + + public static ITypeSymbol? GetThrownExceptionType(this SemanticModel model, ThrowExpressionSyntax tes) + => model.GetThrownExceptionType(tes.Expression); + + public static bool Matches(this ITypeSymbol expected, ITypeSymbol? actual) + => SymbolEqualityComparer.Default.Equals(expected, actual); +} diff --git a/References/BizHawk.Analyzer.dll b/References/BizHawk.Analyzer.dll index 1d654f89a2ed075fbbb0846ffe0c401262b668c4..4c53a0a2e00ddac5cf40893cee3d6a1bcfdfb558 100644 GIT binary patch delta 5135 zcmaJ_4RBP|6+ZX9w{PFR4V&!S{1OrqLXsswOa#GzXdwI~#RwQcBWhTaMb>PxA-fS` zOm;C+#g=ON)X^F12xv#7Q=#~SZ53&h)^@a_wiRs|G*V0bX>qD{bkKhH>}C-<k=Jz-!b{>*WS37QrB^sT;dirL{$*b}km%=)t zusTxe%SaGqfZwx}Xq8>6Ot)J_rD;rsRD!$8zEeyS)9t54rKquwiOB`8BZ9nSp^$@W z%85EA6A62te@?tu@moY@)pf~I$=hBDQ+clX;GiJVs^+G2^<1h*Yq2t5%96-e7!^Mn>s7Od&O;qiiL{MR_5YUxZrRnDsg5&0<2Qf>|F)ee=6-^d&#c@m zo*#ARAnRwsF@?KFx}(gL<<04Zl`_^n*;?BFc0rUZ$Z}umnG{JsQ1l- zL*2ZjfZcEQ$@Lc*#@tjyWCrlO`udm91)9q}?Tl1IldQ(a_(SO+x(UT}|LCCU}SZ}ENhA75JHOSx*%u?Cv7J3uU4E76^EnVHT2<7|W z3A@!<8LtOJ4t$rQ%)yOp<>N1)phDKYoorX?R z9TFd7jH)e$kA8z7I=!q`={nuz=#4vd`is^JyoYfgW0ONF52z9qRfhPr;}JAC?vl7x zeMC2Df@8g5lG~7YiP7lt&^7cu#iVL;J^V9F>0|4SE|aD}G3j>HEqdBqVp`a>OH7k4 zRT^D!i*A8rQifByu3@K1Fza+eFF=T=pzAbO^#hAA0-Xjpm1h;1%3;)zic5Ccg4wpH zKzSZXA9Ts2*Se+84e<2PLbpsV;Fd|e$32d?&|{p&xZ^lF)@hM0!!<*(=swQ$C}SQH zvZ#~mv$#HkM^MjgZxqtMjyqmVO~ON6m(|HgRh((HE3YaPXfTs;`C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