From 32def3717020d803c02cfda5cd26893484f30399 Mon Sep 17 00:00:00 2001 From: Arthur van de Vondervoort Date: Wed, 3 Jan 2024 16:57:03 +0100 Subject: [PATCH] Temporary add Try/Catch to resolve exception --- ...le0039ArgumentDifferentTypeThenExpected.cs | 49 +++++++++++-------- Design/Rule0048ErrorWithTextConstant.cs | 43 +++++++++------- 2 files changed, 53 insertions(+), 39 deletions(-) diff --git a/Design/Rule0039ArgumentDifferentTypeThenExpected.cs b/Design/Rule0039ArgumentDifferentTypeThenExpected.cs index 3371528d..27783c22 100644 --- a/Design/Rule0039ArgumentDifferentTypeThenExpected.cs +++ b/Design/Rule0039ArgumentDifferentTypeThenExpected.cs @@ -8,7 +8,7 @@ namespace BusinessCentral.LinterCop.Design [DiagnosticAnalyzer] public class Rule0039ArgumentDifferentTypeThenExpected : DiagnosticAnalyzer { - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected); + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, DiagnosticDescriptors.Rule0000ErrorInRule); private static readonly List referencePageProviders = new List { @@ -54,32 +54,39 @@ private void AnalyzeRunPageArguments(OperationAnalysisContext ctx) private void AnalyzeSetRecordArgument(OperationAnalysisContext ctx) { - if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; - if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; + try + { + if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; + if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; - IInvocationExpression operation = (IInvocationExpression)ctx.Operation; - if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return; + IInvocationExpression operation = (IInvocationExpression)ctx.Operation; + if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return; - if (operation.TargetMethod.ContainingType.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.Page) return; - string[] procedureNames = { "GetRecord", "SetRecord", "SetSelectionFilter", "SetTableView" }; - if (!procedureNames.Contains(operation.TargetMethod.Name)) return; - if (operation.Arguments.Count() != 1) return; + if (operation.TargetMethod.ContainingType.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.Page) return; + string[] procedureNames = { "GetRecord", "SetRecord", "SetSelectionFilter", "SetTableView" }; + if (!procedureNames.Contains(operation.TargetMethod.Name)) return; + if (operation.Arguments.Count() != 1) return; - if (operation.Arguments[0].Syntax.Kind != SyntaxKind.IdentifierName || operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression) return; + if (operation.Arguments[0].Syntax.Kind != SyntaxKind.IdentifierName || operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression) return; - IOperation pageReference = ctx.Operation.DescendantsAndSelf().Where(x => x.GetSymbol() != null) - .Where(x => x.Type.GetNavTypeKindSafe() == NavTypeKind.Page) - .SingleOrDefault(); - if (pageReference == null) return; - IVariableSymbol variableSymbol = (IVariableSymbol)pageReference.GetSymbol().OriginalDefinition; - IPageTypeSymbol pageTypeSymbol = (IPageTypeSymbol)variableSymbol.GetTypeSymbol().OriginalDefinition; - ITableTypeSymbol pageSourceTable = pageTypeSymbol.RelatedTable; + IOperation pageReference = ctx.Operation.DescendantsAndSelf().Where(x => x.GetSymbol() != null) + .Where(x => x.Type.GetNavTypeKindSafe() == NavTypeKind.Page) + .SingleOrDefault(); + if (pageReference == null) return; + IVariableSymbol variableSymbol = (IVariableSymbol)pageReference.GetSymbol().OriginalDefinition; + IPageTypeSymbol pageTypeSymbol = (IPageTypeSymbol)variableSymbol.GetTypeSymbol().OriginalDefinition; + ITableTypeSymbol pageSourceTable = pageTypeSymbol.RelatedTable; - IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand; - ITableTypeSymbol recordArgument = ((IRecordTypeSymbol)operand.GetSymbol().GetTypeSymbol()).BaseTable; + IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand; + ITableTypeSymbol recordArgument = ((IRecordTypeSymbol)operand.GetSymbol().GetTypeSymbol()).BaseTable; - if (!AreTheSameNavObjects(recordArgument, pageSourceTable)) - ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 1, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" })); + if (!AreTheSameNavObjects(recordArgument, pageSourceTable)) + ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 1, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" })); + } + catch (NullReferenceException) + { + ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, ctx.Operation.Syntax.GetLocation(), new Object[] { "Rule0039", "NullReferenceException" })); + } } private void AnalyzeTableReferencePageProvider(SymbolAnalysisContext ctx) diff --git a/Design/Rule0048ErrorWithTextConstant.cs b/Design/Rule0048ErrorWithTextConstant.cs index 9eb99b3d..9f0f6ebf 100644 --- a/Design/Rule0048ErrorWithTextConstant.cs +++ b/Design/Rule0048ErrorWithTextConstant.cs @@ -9,34 +9,41 @@ namespace BusinessCentral.LinterCop.Design [DiagnosticAnalyzer] public class Rule0048ErrorWithTextConstant : DiagnosticAnalyzer { - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0048ErrorWithTextConstant); + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0048ErrorWithTextConstant, DiagnosticDescriptors.Rule0000ErrorInRule); public override void Initialize(AnalysisContext context) => context.RegisterOperationAction(new Action(this.AnalyzeErrorMethod), OperationKind.InvocationExpression); private void AnalyzeErrorMethod(OperationAnalysisContext ctx) { - if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; - if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; + try + { + if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; + if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; + + IInvocationExpression operation = (IInvocationExpression)ctx.Operation; + if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return; + if (!SemanticFacts.IsSameName(operation.TargetMethod.Name, "Error")) return; + if (operation.Arguments.Length == 0) return; - IInvocationExpression operation = (IInvocationExpression)ctx.Operation; - if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return; - if (!SemanticFacts.IsSameName(operation.TargetMethod.Name, "Error")) return; - if (operation.Arguments.Length == 0) return; + if (operation.Arguments[0].Value.Type.GetNavTypeKindSafe() == NavTypeKind.ErrorInfo) return; - if (operation.Arguments[0].Value.Type.GetNavTypeKindSafe() == NavTypeKind.ErrorInfo) return; + switch (operation.Arguments[0].Syntax.Kind) + { + case SyntaxKind.IdentifierName: + IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand; + if (operand.GetSymbol().OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.Label) return; + break; + case SyntaxKind.LiteralExpression: + if (operation.Arguments[0].Syntax.GetIdentifierOrLiteralValue() == "") return; + break; + } - switch (operation.Arguments[0].Syntax.Kind) + ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0048ErrorWithTextConstant, ctx.Operation.Syntax.GetLocation())); + } + catch (InvalidCastException) { - case SyntaxKind.IdentifierName: - IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand; - if (operand.GetSymbol().OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.Label) return; - break; - case SyntaxKind.LiteralExpression: - if (operation.Arguments[0].Syntax.GetIdentifierOrLiteralValue() == "") return; - break; + ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, ctx.Operation.Syntax.GetLocation(), new Object[] { "Rule0048", "InvalidCastException" })); } - - ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0048ErrorWithTextConstant, ctx.Operation.Syntax.GetLocation())); } } } \ No newline at end of file