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 with Rec.Validate() method #897

Merged
merged 2 commits into from
Jan 27, 2025
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
2 changes: 2 additions & 0 deletions BusinessCentral.LinterCop.Test/Rule0051.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void Setup()
[TestCase("GetMethodStrSubstNo")]
#endif
[TestCase("SetFilterFieldCode")]
[TestCase("ValidateFieldCode")]
public async Task HasDiagnostic(string testCase)
{
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "HasDiagnostic", $"{testCase}.al"))
Expand All @@ -35,6 +36,7 @@ public async Task HasDiagnostic(string testCase)
[TestCase("GetMethodUserId")]
#endif
[TestCase("SetFilterFieldRef")]
[TestCase("ValidateFieldCode")]
public async Task NoDiagnostic(string testCase)
{
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "NoDiagnostic", $"{testCase}.al"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ codeunit 50100 MyCodeunit
MyTable: Record MyTable;
MyFilterValue: Code[50];
begin
[|MyTable.SetFilter(MyField, '<>%1', MyFilterValue)|];
MyTable.SetFilter(MyField, '<>%1', [|MyFilterValue|]);
end;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyTable: Record MyTable;
MyVar: Code[20];
begin
MyTable.Validate(MyField, [|MyVar|]);
end;
}

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

table 50100 MyTable
{
fields
{
field(1; MyField; Code[10]) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Rule0051PossibleOverflowAssigning : DiagnosticAnalyzer
public override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeSetFilter), OperationKind.InvocationExpression);
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeValidate), OperationKind.InvocationExpression);
#if !LessThenSpring2024
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeGetMethod), OperationKind.InvocationExpression);
#endif
Expand Down Expand Up @@ -68,10 +69,48 @@ private void AnalyzeSetFilter(OperationAnalysisContext ctx)

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

private void AnalyzeValidate(OperationAnalysisContext ctx)
{
if (ctx.IsObsoletePendingOrRemoved() || ctx.Operation is not IInvocationExpression operation)
return;

if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod ||
operation.TargetMethod.Name != "Validate" ||
operation.TargetMethod.ContainingSymbol?.Name != "Table" ||
operation.Arguments.Length < 2 ||
operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression)
return;

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

bool isError = false;
int typeLength = GetTypeLength(fieldType, ref isError);
if (isError || typeLength == int.MaxValue)
return;

if (operation.Arguments[1].Value is not IConversionExpression argValue)
return;

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

#if !LessThenSpring2024
private void AnalyzeGetMethod(OperationAnalysisContext ctx)
{
Expand Down
Loading