From 1f9165a5a87c33b1705336f836eb46f08a55fee8 Mon Sep 17 00:00:00 2001 From: Arthur van de Vondervoort Date: Fri, 12 Jan 2024 11:41:35 +0100 Subject: [PATCH] Resolve NullReferenceException --- ...oNotUseObjectIDsInVariablesOrProperties.cs | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/Design/Rule0003DoNotUseObjectIDsInVariablesOrProperties.cs b/Design/Rule0003DoNotUseObjectIDsInVariablesOrProperties.cs index 7487a59d..ff9e499e 100644 --- a/Design/Rule0003DoNotUseObjectIDsInVariablesOrProperties.cs +++ b/Design/Rule0003DoNotUseObjectIDsInVariablesOrProperties.cs @@ -1,5 +1,6 @@ using Microsoft.Dynamics.Nav.CodeAnalysis; using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics; +using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols; using System.Collections.Immutable; namespace BusinessCentral.LinterCop.Design @@ -7,7 +8,7 @@ namespace BusinessCentral.LinterCop.Design [DiagnosticAnalyzer] public class Rule0003DoNotUseObjectIDsInVariablesOrProperties : DiagnosticAnalyzer { - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration); + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, DiagnosticDescriptors.Rule0000ErrorInRule); public override void Initialize(AnalysisContext context) { @@ -15,21 +16,20 @@ public override void Initialize(AnalysisContext context) SyntaxKind.ObjectReference, SyntaxKind.PermissionValue }); - } + private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext ctx) { if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; - var correctName = ""; - + string correctName; if (ctx.ContainingSymbol.Kind == SymbolKind.LocalVariable || ctx.ContainingSymbol.Kind == SymbolKind.GlobalVariable) { IVariableSymbol variable = (IVariableSymbol)ctx.ContainingSymbol; - if (variable.Type.NavTypeKind == NavTypeKind.DotNet) return; + if (variable.Type.GetNavTypeKindSafe() == NavTypeKind.DotNet) return; - if (variable.Type.NavTypeKind == NavTypeKind.Array) + if (variable.Type.GetNavTypeKindSafe() == NavTypeKind.Array) correctName = ((IArrayTypeSymbol)variable.Type).ElementType.Name.ToString(); else correctName = variable.Type.Name; @@ -43,7 +43,6 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext if (ctx.ContainingSymbol.Kind == SymbolKind.Property) { IPropertySymbol property = (IPropertySymbol)ctx.ContainingSymbol; - if (ctx.Node.Kind == SyntaxKind.PermissionValue) { var nodes = ctx.Node.ChildNodesAndTokens().GetEnumerator(); @@ -60,7 +59,6 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, nodes.Current.GetLocation(), new object[] { "", "the object name" })); }; } - }; } @@ -80,15 +78,12 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext foreach (IParameterSymbol parameter in method.Parameters) { - if (parameter.ParameterType.NavTypeKind == NavTypeKind.DotNet) - { - continue; - } + if (parameter.ParameterType.GetNavTypeKindSafe() == NavTypeKind.DotNet) continue; if (ctx.Node.GetLocation().SourceSpan.End == parameter.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End) { - if (parameter.ParameterType.NavTypeKind == NavTypeKind.Array) - correctName = ((IArrayTypeSymbol)(parameter.ParameterType)).ElementType.Name.ToString(); + if (parameter.ParameterType.GetNavTypeKindSafe() == NavTypeKind.Array) + correctName = ((IArrayTypeSymbol)parameter.ParameterType).ElementType.Name.ToString(); else correctName = parameter.ParameterType.Name; @@ -99,21 +94,27 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" })); } } - IReturnValueSymbol returnValue = method.ReturnValueSymbol; - if (returnValue == null || returnValue.ReturnType.NavTypeKind == NavTypeKind.DotNet) - return; - - if (ctx.Node.GetLocation().SourceSpan.End == returnValue.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End) + try { - correctName = returnValue.ReturnType.Name; + IReturnValueSymbol returnValue = method.ReturnValueSymbol; + if (returnValue == null || returnValue.ReturnType.GetNavTypeKindSafe() == NavTypeKind.DotNet) return; - if (ctx.Node.GetLastToken().ToString().Trim('"').ToUpper() != correctName.ToUpper()) - ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, ctx.Node.GetLocation(), new object[] { ctx.Node.ToString().Trim('"'), correctName })); + if (ctx.Node.GetLocation().SourceSpan.End == returnValue.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End) + { + correctName = returnValue.ReturnType.Name; - if (ctx.Node.GetLastToken().ToString().Trim('"') != correctName) - ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" })); + if (ctx.Node.GetLastToken().ToString().Trim('"').ToUpper() != correctName.ToUpper()) + ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0003DoNotUseObjectIDsInVariablesOrProperties, ctx.Node.GetLocation(), new object[] { ctx.Node.ToString().Trim('"'), correctName })); + + if (ctx.Node.GetLastToken().ToString().Trim('"') != correctName) + ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" })); + } + } + catch (NullReferenceException) + { + ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, ctx.Node.GetLocation(), new Object[] { "Rule0003/Rule0005", "NullReferenceException", "" })); } } } } -} +} \ No newline at end of file