Skip to content

Commit

Permalink
Remove try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthurvdv committed Jan 11, 2024
1 parent 78a9951 commit 4cb36ce
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 47 deletions.
27 changes: 10 additions & 17 deletions Design/Rule0003DoNotUseObjectIDsInVariablesOrProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,20 @@ private void CheckForObjectIDsInVariablesOrProperties(SyntaxNodeAnalysisContext
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" }));
}
}
try
{
IReturnValueSymbol returnValue = method.ReturnValueSymbol;
if (returnValue.ReturnType.NavTypeKind == NavTypeKind.DotNet)
{
return;
}
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)
{
correctName = returnValue.ReturnType.Name;
if (ctx.Node.GetLocation().SourceSpan.End == returnValue.DeclaringSyntaxReference.GetSyntax(CancellationToken.None).Span.End)
{
correctName = returnValue.ReturnType.Name;

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('"').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, "" }));
}
if (ctx.Node.GetLastToken().ToString().Trim('"') != correctName)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0005VariableCasingShouldNotDifferFromDeclaration, ctx.Node.GetLocation(), new object[] { correctName, "" }));
}
catch (System.NullReferenceException)
{ }
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions Design/Rule0004LookupPageIdAndDrillDownPageId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BusinessCentral.LinterCop.Design
[DiagnosticAnalyzer]
public class Rule0004LookupPageIdAndDrillDownPageId : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0004LookupPageIdAndDrillDownPageId);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0004LookupPageIdAndDrillDownPageId, DiagnosticDescriptors.Rule0000ErrorInRule);

public override void Initialize(AnalysisContext context)
=> context.RegisterSymbolAction(new Action<SymbolAnalysisContext>(this.CheckForLookupPageIdAndDrillDownPageId), SymbolKind.Page);
Expand All @@ -18,13 +18,13 @@ private void CheckForLookupPageIdAndDrillDownPageId(SymbolAnalysisContext contex
IPageTypeSymbol pageTypeSymbol = (IPageTypeSymbol)context.Symbol;
if (pageTypeSymbol.PageType != PageTypeKind.List || pageTypeSymbol.RelatedTable == null) return;
if (pageTypeSymbol.RelatedTable.ContainingModule != context.Symbol.ContainingModule) return;
CheckTable(pageTypeSymbol.RelatedTable, ref context);
CheckTable(pageTypeSymbol.RelatedTable, context);
}

private void CheckTable(ITableTypeSymbol table, ref SymbolAnalysisContext context)
private void CheckTable(ITableTypeSymbol table, SymbolAnalysisContext context)
{
if (table.IsObsoletePending || table.IsObsoleteRemoved) return;
if (!IsSymbolAccessible(table)) return;
if (!IsSymbolAccessible(table, context)) return;
if (table.TableType == TableTypeKind.Temporary) return;

bool exists = table.Properties.Where(e => e.PropertyKind == PropertyKind.DrillDownPageId || e.PropertyKind == PropertyKind.LookupPageId).Count() == 2;
Expand All @@ -34,24 +34,24 @@ private void CheckTable(ITableTypeSymbol table, ref SymbolAnalysisContext contex
Diagnostic.Create(
DiagnosticDescriptors.Rule0004LookupPageIdAndDrillDownPageId,
table.GetLocation(),
new object[] { GetDeclaration(table), table.Name, context.Symbol.Name }));
new object[] { GetDeclaration(table, context), table.Name, context.Symbol.Name }));
}

private static string GetDeclaration(ISymbol symbol)
=> symbol.Location.SourceTree.GetText(CancellationToken.None).GetSubText(symbol.DeclaringSyntaxReference.Span).ToString();
private static string GetDeclaration(ISymbol symbol, SymbolAnalysisContext context)
=> symbol.Location.SourceTree.GetText(context.CancellationToken).GetSubText(symbol.DeclaringSyntaxReference.Span).ToString();

private static bool IsSymbolAccessible(ISymbol symbol)
private static bool IsSymbolAccessible(ISymbol symbol, SymbolAnalysisContext context)
{
try
{
GetDeclaration(symbol);
GetDeclaration(symbol, context);
return true;
}
catch (Exception)
{
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0000ErrorInRule, context.Symbol.GetLocation(), new Object[] { "Rule0004", "Exception", "at Line 47" }));
return false;
}
}
}

}
}
13 changes: 4 additions & 9 deletions Design/Rule0012DoNotUseObjectIdInSystemFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,12 @@ private void CheckForObjectIdsInFunctionInvocations(OperationAnalysisContext con
if (context.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || context.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (context.ContainingSymbol.IsObsoletePending || context.ContainingSymbol.IsObsoleteRemoved) return;
IInvocationExpression operation = (IInvocationExpression)context.Operation;
RelevantFuntion CurrentFunction = null;
try
{
CurrentFunction = FunctionCallsWithIDParamaters.RelevantFunctions.First(o => (o.ObjectType.ToString().ToUpper() == operation.TargetMethod.ContainingSymbol.Name.ToUpper() && o.FunctionName == operation.TargetMethod.Name));
}
catch (System.InvalidOperationException)
{ }

SyntaxKind[] AllowedParameterKinds = { SyntaxKind.MemberAccessExpression, SyntaxKind.IdentifierName, SyntaxKind.InvocationExpression, SyntaxKind.QualifiedName };
RelevantFuntion CurrentFunction = FunctionCallsWithIDParamaters.RelevantFunctions.FirstOrDefault(o => (o.ObjectType.ToString().ToUpper() == operation.TargetMethod.ContainingSymbol.Name.ToUpper() && o.FunctionName == operation.TargetMethod.Name));
if (CurrentFunction == null) return;

if (CurrentFunction != null && operation.TargetMethod.Parameters.Length != 0 && !AllowedParameterKinds.Contains(operation.Arguments[0].Syntax.Kind) && (operation.Arguments[0].Syntax.ToString() != "0" || !CurrentFunction.ZeroIDAllowed))
SyntaxKind[] AllowedParameterKinds = { SyntaxKind.MemberAccessExpression, SyntaxKind.IdentifierName, SyntaxKind.InvocationExpression, SyntaxKind.QualifiedName };
if (operation.TargetMethod.Parameters.Length != 0 && !AllowedParameterKinds.Contains(operation.Arguments[0].Syntax.Kind) && (operation.Arguments[0].Syntax.ToString() != "0" || !CurrentFunction.ZeroIDAllowed))
{
if (operation.TargetMethod.Parameters[0].ParameterType.NavTypeKind == NavTypeKind.Integer)
{
Expand Down
15 changes: 5 additions & 10 deletions Design/Rule0016CheckForMissingCaptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,13 @@ private void CheckForMissingCaptions(SymbolAnalysisContext context)

private bool CaptionIsMissing(ISymbol Symbol, SymbolAnalysisContext context)
{
try
if (Symbol.ContainingType?.Kind == SymbolKind.Table)
{
if (Symbol.ContainingType.Kind == SymbolKind.Table)
{
if (((ITableTypeSymbol)Symbol.ContainingType).Id >= 2000000000)
return false;
if (((IFieldSymbol)Symbol).Id >= 2000000000)
return false;
}
if (((ITableTypeSymbol)Symbol.ContainingType).Id >= 2000000000)
return false;
if (((IFieldSymbol)Symbol).Id >= 2000000000)
return false;
}
catch (NullReferenceException)
{ }

if (Symbol.GetEnumPropertyValue<ShowAsKind>(PropertyKind.ShowAs) == ShowAsKind.SplitButton)
return false;
Expand Down

0 comments on commit 4cb36ce

Please sign in to comment.