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

Resolve false positive on Rule0048 #902

Merged
merged 2 commits into from
Jan 28, 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
43 changes: 43 additions & 0 deletions BusinessCentral.LinterCop.Test/Rule0048.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace BusinessCentral.LinterCop.Test;

public class Rule0048
{
private string _testCaseDir = "";

[SetUp]
public void Setup()
{
_testCaseDir = Path.Combine(Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName,
"TestCases", "Rule0048");
}

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

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

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

var fixture = RoslynFixtureFactory.Create<Rule0048ErrorWithTextConstant>();
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0048ErrorWithTextConstant.Id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
begin
[|Error('Something went wrong...')|];
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyTable: Record MyTable;
UnexpectedErr: Label '%1 is not an valid Record.';
begin
[|Error(StrSubstNo(UnexpectedErr, MyTable.MyField))|];
end;
}

table 50100 MyTable
{
fields
{
field(1; MyField; Integer) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
UnexpectedErr: Text;
begin
[|Error(UnexpectedErr)|];
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
MyErrorInfo: ErrorInfo;
begin
MyErrorInfo := ErrorInfo.Create(GetLastErrorText());
[|Error(MyErrorInfo)|];
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
codeunit 50100 MyCodeunit
{
procedure MyProcedure()
var
UnexpectedErr: Label 'Something went wrong...';
begin
[|Error(UnexpectedErr)|];
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
codeunit 50100 MyCodeunit
{
var
UnexpectedErr: Label 'Something went wrong...';

procedure MyProcedure()
begin
[|Error(this.UnexpectedErr)|];
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private void AnalyzeErrorMethod(OperationAnalysisContext ctx)
switch (operation.Arguments[0].Syntax.Kind)
{
case SyntaxKind.IdentifierName:
case SyntaxKind.MemberAccessExpression:
if (operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression)
break;

Expand Down
Loading