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

Rule0075 raise diagnostic on non-matching Enum parameter #899

Merged
merged 1 commit 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/Rule0075.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void Setup()
[TestCase("ImplicitConversiontCodeToEnum")]
[TestCase("ImplicitConversiontEnumToAnotherEnum")]
[TestCase("RecordGetCodeFieldLengthTooLong")]
[TestCase("RecordGetEnum")]
[TestCase("RecordGetGlobalVariable")]
[TestCase("RecordGetLocalVariable")]
[TestCase("RecordGetMethod")]
Expand All @@ -41,6 +42,7 @@ public async Task HasDiagnostic(string testCase)
[TestCase("RecordGetBuiltInMethodRecordId")]
[TestCase("RecordGetCode10ToCode20")]
[TestCase("RecordGetDecimalToInteger")]
[TestCase("RecordGetEnum")]
[TestCase("RecordGetFieldRecordId")]
[TestCase("RecordGetGlobalVariable")]
[TestCase("RecordGetLocalVariable")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyTable: Record MyTable;
MyEnum: Enum MyEnum;
begin
[|MyTable.Get(MyEnum::MyValue)|];
end;
}

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

table 50100 MyTable
{
fields { field(1; MyField; Enum MyEnum) { } }
}
enum 50100 MyEnum
{
value(0; MyValue) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ public class Rule0075RecordGetProcedureArguments : DiagnosticAnalyzer
{ NavTypeKind.Text, new HashSet<NavTypeKind> { NavTypeKind.Code } },

// String(literal) can be converted to Text and/or Code
{ NavTypeKind.String, new HashSet<NavTypeKind> { NavTypeKind.Text, NavTypeKind.Code } }
{ NavTypeKind.String, new HashSet<NavTypeKind> { NavTypeKind.Text, NavTypeKind.Code } },

// Explicity set Enum can not be converted
{ NavTypeKind.Enum, new HashSet<NavTypeKind>() }
};

public override void Initialize(AnalysisContext context)
{
public override void Initialize(AnalysisContext context) =>
context.RegisterOperationAction(AnalyzeAssignmentStatement, OperationKind.InvocationExpression);
}

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

if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod ||
!SemanticFacts.IsSameName(operation.TargetMethod.Name, "Get"))
operation.TargetMethod.Name != "Get")
return;

// Skip unsupported single argument scenarios, like Record.Get(RecordId)
Expand Down
Loading