From bf03e99e4a125079ab73065a21b133268ca12a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Fri, 8 Aug 2025 19:03:31 +0200 Subject: [PATCH] Improved style checker --- ScriptCore/StyleChecker/StyleChecker.cs | 2 +- .../CatchUnmanagedCallersOnlyAnalyzer.cs | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/ScriptCore/StyleChecker/StyleChecker.cs b/ScriptCore/StyleChecker/StyleChecker.cs index 70a41a6..d9f884f 100644 --- a/ScriptCore/StyleChecker/StyleChecker.cs +++ b/ScriptCore/StyleChecker/StyleChecker.cs @@ -10,7 +10,7 @@ using Microsoft.CodeAnalysis.Diagnostics; namespace GlitchyEngine.StyleChecker; -[DiagnosticAnalyzer(LanguageNames.CSharp)] +//[DiagnosticAnalyzer(LanguageNames.CSharp)] public class StyleChecker : DiagnosticAnalyzer { public const string DiagnosticId = "MakeConst2"; diff --git a/ScriptCoreGenerator/StyleCheckers/CatchUnmanagedCallersOnlyAnalyzer.cs b/ScriptCoreGenerator/StyleCheckers/CatchUnmanagedCallersOnlyAnalyzer.cs index 606d1fb..69f67fa 100644 --- a/ScriptCoreGenerator/StyleCheckers/CatchUnmanagedCallersOnlyAnalyzer.cs +++ b/ScriptCoreGenerator/StyleCheckers/CatchUnmanagedCallersOnlyAnalyzer.cs @@ -66,14 +66,7 @@ public class CatchUnmanagedCallersOnlyAnalyzer : DiagnosticAnalyzer if (method.Body is not null) { // Check if the outer most statement is a try-catch block. - var tryStatement = method.Body.Statements.FirstOrDefault(s => s is TryStatementSyntax) as TryStatementSyntax; - if (tryStatement is null) - { - // If there is no try-catch block, report a diagnostic. - ReportDiagnostic(); - return; - } - else + if (method.Body.Statements.FirstOrDefault() is TryStatementSyntax tryStatement) { // Check if the try block has a catch clause. if (tryStatement.Catches.Count == 0) @@ -81,7 +74,7 @@ public class CatchUnmanagedCallersOnlyAnalyzer : DiagnosticAnalyzer ReportDiagnostic(); return; } - + // Report a diagnostic if there is no catch block that catches System.Exception. if (tryStatement.Catches.All(c => { @@ -98,6 +91,12 @@ public class CatchUnmanagedCallersOnlyAnalyzer : DiagnosticAnalyzer return; } } + else + { + // If there is no try-catch block, report a diagnostic. + ReportDiagnostic(); + return; + } } else if (method.ExpressionBody is not null) {