-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Rule0081: Use Rec.IsEmpty() for checking record existence (#833)
- Loading branch information
Showing
15 changed files
with
312 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace BusinessCentral.LinterCop.Test; | ||
|
||
public class Rule0081 | ||
{ | ||
private string _testCaseDir = ""; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_testCaseDir = Path.Combine(Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName, | ||
"TestCases", "Rule0081"); | ||
} | ||
|
||
[Test] | ||
[TestCase("OneGreaterThanRecordCount")] | ||
[TestCase("RecordCountEqualsZero")] | ||
[TestCase("RecordCountGreaterThanOrEqualZero")] | ||
[TestCase("RecordCountGreaterThanZero")] | ||
[TestCase("RecordCountLessThanOne")] | ||
[TestCase("RecordCountLessThanOrEqualZero")] | ||
[TestCase("RecordCountLessThanZero")] | ||
[TestCase("RecordCountNotEqualsZero")] | ||
public async Task HasDiagnostic(string testCase) | ||
{ | ||
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "HasDiagnostic", $"{testCase}.al")) | ||
.ConfigureAwait(false); | ||
|
||
var fixture = RoslynFixtureFactory.Create<Rule0081UseIsEmptyMethod>(); | ||
fixture.HasDiagnostic(code, Rule0081UseIsEmptyMethod.DiagnosticDescriptors.Rule0081UseIsEmptyMethod.Id); | ||
} | ||
|
||
[Test] | ||
[TestCase("RecordCountEqualsOne")] | ||
[TestCase("RecordTemporaryCountEqualsZero")] | ||
public async Task NoDiagnostic(string testCase) | ||
{ | ||
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "NoDiagnostic", $"{testCase}.al")) | ||
.ConfigureAwait(false); | ||
|
||
var fixture = RoslynFixtureFactory.Create<Rule0081UseIsEmptyMethod>(); | ||
fixture.NoDiagnosticAtMarker(code, Rule0081UseIsEmptyMethod.DiagnosticDescriptors.Rule0081UseIsEmptyMethod.Id); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/OneGreaterThanRecordCount.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|1 > MyTable.Count()|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/RecordCountEqualsZero.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() = 0|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...tral.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/RecordCountGreaterThanOrEqualZero.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() >= 0|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...nessCentral.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/RecordCountGreaterThanZero.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() > 0|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/RecordCountLessThanOne.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() < 1|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...Central.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/RecordCountLessThanOrEqualZero.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() <= 0|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/RecordCountLessThanZero.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() < 0|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0081/HasDiagnostic/RecordCountNotEqualsZero.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() <> 0|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0081/NoDiagnostic/RecordCountEqualsOne.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
if [|MyTable.Count() = 1|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...sCentral.LinterCop.Test/TestCases/Rule0081/NoDiagnostic/RecordTemporaryCountEqualsZero.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
TempMyTable: Record MyTable temporary; | ||
begin | ||
if [|TempMyTable.Count() = 0|] then; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
BusinessCentral.LinterCop/Design/Rule0081UseIsEmptyMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using BusinessCentral.LinterCop.AnalysisContextExtension; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Syntax; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Utilities; | ||
using System.Collections.Immutable; | ||
|
||
namespace BusinessCentral.LinterCop.Design | ||
{ | ||
[DiagnosticAnalyzer] | ||
public class Rule0081UseIsEmptyMethod : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = | ||
ImmutableArray.Create(DiagnosticDescriptors.Rule0081UseIsEmptyMethod); | ||
|
||
public override void Initialize(AnalysisContext context) => | ||
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeCountMethod), OperationKind.InvocationExpression); | ||
|
||
private void AnalyzeCountMethod(OperationAnalysisContext ctx) | ||
{ | ||
if (ctx.IsObsoletePendingOrRemoved()) | ||
return; | ||
|
||
if (ctx.Operation is not IInvocationExpression operation) | ||
return; | ||
|
||
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod || | ||
operation.TargetMethod.Name != "Count" || | ||
operation.TargetMethod.ContainingSymbol?.Name != "Table") | ||
return; | ||
|
||
if (operation.Instance?.GetSymbol() is not IVariableSymbol { Type: IRecordTypeSymbol recordTypeSymbol } || recordTypeSymbol.Temporary) | ||
return; | ||
|
||
if (operation.Syntax.Parent is not BinaryExpressionSyntax binaryExpression) | ||
return; | ||
|
||
if (IsLiteralExpressionValue(binaryExpression.Left, 0) || | ||
IsLiteralExpressionValue(binaryExpression.Right, 0)) | ||
{ | ||
ReportDiagnostic(ctx, operation); | ||
return; | ||
} | ||
|
||
if (binaryExpression.OperatorToken.Kind == SyntaxKind.LessThanToken && | ||
IsLiteralExpressionValue(binaryExpression.Right, 1)) | ||
{ | ||
ReportDiagnostic(ctx, operation); | ||
return; | ||
} | ||
|
||
if (binaryExpression.OperatorToken.Kind == SyntaxKind.GreaterThanToken && | ||
IsLiteralExpressionValue(binaryExpression.Left, 1)) | ||
{ | ||
ReportDiagnostic(ctx, operation); | ||
} | ||
} | ||
|
||
private static bool IsLiteralExpressionValue(CodeExpressionSyntax codeExpression, int value) => | ||
codeExpression is LiteralExpressionSyntax { Literal: { Kind: SyntaxKind.Int32SignedLiteralValue } literal } | ||
&& literal.GetLiteralValue() is int literalvalue && literalvalue == value; | ||
|
||
private static void ReportDiagnostic(OperationAnalysisContext ctx, IInvocationExpression operation) | ||
{ | ||
ctx.ReportDiagnostic(Diagnostic.Create( | ||
DiagnosticDescriptors.Rule0081UseIsEmptyMethod, | ||
operation.Syntax.Parent.GetLocation(), | ||
new object[] { operation.Instance?.GetSymbol()?.Name.QuoteIdentifierIfNeeded() ?? string.Empty })); | ||
} | ||
|
||
public static class DiagnosticDescriptors | ||
{ | ||
public static readonly DiagnosticDescriptor Rule0081UseIsEmptyMethod = new( | ||
id: LinterCopAnalyzers.AnalyzerPrefix + "0081", | ||
title: LinterCopAnalyzers.GetLocalizableString("Rule0081UseIsEmptyMethodTitle"), | ||
messageFormat: LinterCopAnalyzers.GetLocalizableString("Rule0081UseIsEmptyMethodFormat"), | ||
category: "Design", | ||
defaultSeverity: DiagnosticSeverity.Info, isEnabledByDefault: true, | ||
description: LinterCopAnalyzers.GetLocalizableString("Rule0081UseIsEmptyMethodDescription"), | ||
helpLinkUri: "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0081"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters