Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Rule0051 to analyze parameters of .Get() method #840

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions BusinessCentral.LinterCop.Test/Rule0051.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,33 @@ public void Setup()
}

[Test]
#if !LessThenSpring2024
[TestCase("GetMethodStringLiteral")]
[TestCase("GetMethodStrSubstNo")]
#endif
[TestCase("SetFilterFieldCode")]
public async Task HasDiagnostic(string testCase)
{
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "HasDiagnostic", $"{testCase}.al"))
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0051SetFilterPossibleOverflow>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0051SetFilterPossibleOverflow.Id);
var fixture = RoslynFixtureFactory.Create<Rule0051PossibleOverflowAssigning>();
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0051PossibleOverflowAssigning.Id);
}

[Test]
#if !LessThenSpring2024
[TestCase("GetMethodStringLiteral")]
[TestCase("GetMethodStrSubstNo")]
#endif
[TestCase("SetFilterFieldRef")]
public async Task NoDiagnostic(string testCase)
{
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "NoDiagnostic", $"{testCase}.al"))
.ConfigureAwait(false);

var fixture = RoslynFixtureFactory.Create<Rule0051SetFilterPossibleOverflow>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0051SetFilterPossibleOverflow.Id);
var fixture = RoslynFixtureFactory.Create<Rule0051PossibleOverflowAssigning>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0051PossibleOverflowAssigning.Id);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyTable: Record MyTable;
MyCodeFieldA, MyCodeFieldB : Code[20];
begin
MyTable.Get([|StrSubstNo('%1%2', MyCodeFieldA, MyCodeFieldB)|]);
end;
}

table 50100 MyTable
{
fields
{
field(1; MyField; Code[20]) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyTable: Record MyTable;
begin
MyTable.Get([|'ABCDEFGHIJKLMNOPQRSTU'|]);
end;
}

table 50100 MyTable
{
fields
{
field(1; MyField; Code[20]) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyTable: Record MyTable;
MyCodeFieldA, MyCodeFieldB : Code[10];
begin
MyTable.Get([|StrSubstNo('%1%2', MyCodeFieldA, MyCodeFieldB)|]);
end;
}

table 50100 MyTable
{
fields
{
field(1; MyField; Code[20]) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyTable: Record MyTable;
begin
MyTable.Get([|'ABCDEFGHIJKLMNOPQRST'|]);
end;
}

table 50100 MyTable
{
fields
{
field(1; MyField; Code[20]) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if !LessThenFall2023RV1
using BusinessCentral.LinterCop.AnalysisContextExtension;
using BusinessCentral.LinterCop.ArgumentExtension;
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols;
Expand All @@ -9,34 +10,40 @@
namespace BusinessCentral.LinterCop.Design
{
[DiagnosticAnalyzer]
public class Rule0051SetFilterPossibleOverflow : DiagnosticAnalyzer
public class Rule0051PossibleOverflowAssigning : DiagnosticAnalyzer
{
private readonly Lazy<Regex> strSubstNoPatternLazy = new Lazy<Regex>((Func<Regex>)(() => new Regex("[#%](\\d+)", RegexOptions.Compiled)));

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0051SetFilterPossibleOverflow, DiagnosticDescriptors.Rule0000ErrorInRule);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0051PossibleOverflowAssigning);

private Regex StrSubstNoPattern => this.strSubstNoPatternLazy.Value;

public override void Initialize(AnalysisContext context) => context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeInvocation), OperationKind.InvocationExpression);

private void AnalyzeInvocation(OperationAnalysisContext ctx)
public override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeSetFilter), OperationKind.InvocationExpression);
#if !LessThenSpring2024
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeGetMethod), OperationKind.InvocationExpression);
#endif
}
private void AnalyzeSetFilter(OperationAnalysisContext ctx)
{
if (ctx.IsObsoletePendingOrRemoved())
return;

if ((ctx.Operation is not IInvocationExpression operation)
|| operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod
|| operation.TargetMethod == null
|| !SemanticFacts.IsSameName(operation.TargetMethod.Name, "SetFilter")
|| operation.Arguments.Count() < 3
|| operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression)
if ((ctx.Operation is not IInvocationExpression operation) ||
operation.TargetMethod is null ||
operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod ||
!SemanticFacts.IsSameName(operation.TargetMethod.Name, "SetFilter") ||
operation.Arguments.Count() < 3 ||
operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression)
return;

var fieldOperand = ((IConversionExpression)operation.Arguments[0].Value).Operand;
if (fieldOperand.Type is not ITypeSymbol fieldType)
return;

if (fieldType.GetNavTypeKindSafe() == NavTypeKind.Text) return;
if (fieldType.GetNavTypeKindSafe() == NavTypeKind.Text)
return;

bool isError = false;
int typeLength = GetTypeLength(fieldType, ref isError);
Expand All @@ -46,16 +53,67 @@ private void AnalyzeInvocation(OperationAnalysisContext ctx)
foreach (int argIndex in GetArgumentIndexes(operation.Arguments[1].Value))
{
int index = argIndex + 1; // The placeholders are defines as %1, %2, %3, where in case of %1 we need the second (zero based) index of the arguments of the SetFilter method
if ((index < 2)
|| (index >= operation.Arguments.Count())
|| (operation.Arguments[index].Value.Kind != OperationKind.ConversionExpression))
if ((index < 2) ||
(index >= operation.Arguments.Count()) ||
(operation.Arguments[index].Value.Kind != OperationKind.ConversionExpression))
continue;

int expressionLength = this.CalculateMaxExpressionLength(((IConversionExpression)operation.Arguments[index].Value).Operand, ref isError);
if (operation.Arguments[index].Value is not IConversionExpression argValue)
continue;

int expressionLength = this.CalculateMaxExpressionLength(argValue.Operand, ref isError);
if (!isError && expressionLength > typeLength)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0051SetFilterPossibleOverflow, operation.Syntax.GetLocation(), GetDisplayString(operation.Arguments[index], operation), GetDisplayString(operation.Arguments[0], operation)));
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0051PossibleOverflowAssigning, operation.Syntax.GetLocation(), GetDisplayString(operation.Arguments[index], operation), GetDisplayString(operation.Arguments[0], operation)));
}
}

#if !LessThenSpring2024
private void AnalyzeGetMethod(OperationAnalysisContext ctx)
{
if (ctx.IsObsoletePendingOrRemoved())
return;

if ((ctx.Operation is not IInvocationExpression operation) ||
operation.TargetMethod is null ||
operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod ||
!SemanticFacts.IsSameName(operation.TargetMethod.Name, "Get") ||
operation.Arguments.Count() < 1)
return;

if (operation.Instance?.Type.GetTypeSymbol()?.OriginalDefinition is not ITableTypeSymbol table)
return;

if (operation.Arguments.Length < table.PrimaryKey.Fields.Length)
return;

for (int index = 0; index < operation.Arguments.Length; index++)
{
var fieldType = table.PrimaryKey.Fields[index].Type;
var argumentType = operation.Arguments[index].GetTypeSymbol();

if (fieldType is null || argumentType is null || argumentType.HasLength)
continue;

bool isError = false;
int fieldLength = GetTypeLength(fieldType, ref isError);
if (isError || fieldLength == 0)
continue;

if (operation.Arguments[index].Value is not IConversionExpression argValue)
continue;

int expressionLength = this.CalculateMaxExpressionLength(argValue.Operand, ref isError);
if (!isError && expressionLength > fieldLength)
{
ctx.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.Rule0051PossibleOverflowAssigning,
operation.Arguments[index].Syntax.GetLocation(),
$"{argumentType.ToDisplayString()}[{expressionLength}]",
fieldType.ToDisplayString()));
}
}
}
#endif

private static int GetTypeLength(ITypeSymbol type, ref bool isError)
{
Expand Down
Loading
Loading