Skip to content

Commit

Permalink
Merge pull request #439 from StefanMaron/development
Browse files Browse the repository at this point in the history
New rule0048 Error with Text Constant
  • Loading branch information
Arthurvdv authored Dec 15, 2023
2 parents f336962 + 9b63fd7 commit 3fbc9d5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Design/Rule0048ErrorWithTextConstant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols;
using System.Collections.Immutable;

namespace BusinessCentral.LinterCop.Design
{
[DiagnosticAnalyzer]
public class Rule0048ErrorWithTextConstant : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0048ErrorWithTextConstant);

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

private void AnalyzeErrorMethod(OperationAnalysisContext ctx)
{
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;

IInvocationExpression operation = (IInvocationExpression)ctx.Operation;
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return;
if (!SemanticFacts.IsSameName(operation.TargetMethod.Name, "Error")) return;
if (operation.Arguments.Length == 0) return;

if (operation.Arguments[0].Syntax.Kind != SyntaxKind.IdentifierName) return;
if (operation.Arguments[0].Value.Type.GetNavTypeKindSafe() == NavTypeKind.ErrorInfo) return;

IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand;
if (operand.GetSymbol().OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.Label) return;

ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0048ErrorWithTextConstant, ctx.Operation.Syntax.GetLocation()));
}
}
}
5 changes: 5 additions & 0 deletions LinterCop.ruleset.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@
"id": "LC0047",
"action": "Info",
"justification": "Locked Label must have a suffix Tok."
},
{
"id": "LC0048",
"action": "Info",
"justification": "Use Error with a ErrorInfo or Label variable to improve telemetry details."
}
]
}
1 change: 1 addition & 0 deletions LinterCopAnalyzers.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ public static class DiagnosticDescriptors
public static readonly DiagnosticDescriptor Rule0045ZeroEnumValueReservedForEmpty = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0045", (LocalizableString)new LocalizableResourceString("Rule0045ZeroEnumValueReservedForEmptyTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0045ZeroEnumValueReservedForEmptyFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0045ZeroEnumValueReservedForEmptyDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0045");
public static readonly DiagnosticDescriptor Rule0046TokLabelsLocked = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0046", (LocalizableString)new LocalizableResourceString("Rule0046TokLabelsLockedTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0046TokLabelsLockedFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0046TokLabelsLockedDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0046");
public static readonly DiagnosticDescriptor Rule0047LockedLabelsTok = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0047", (LocalizableString)new LocalizableResourceString("Rule0047LockedLabelsTokTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0047LockedLabelsTokFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0047LockedLabelsTokDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0047");
public static readonly DiagnosticDescriptor Rule0048ErrorWithTextConstant = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0048", (LocalizableString)new LocalizableResourceString("Rule0048ErrorWithTextConstantTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0048ErrorWithTextConstantFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0048ErrorWithTextConstantDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0048");
}
}
9 changes: 9 additions & 0 deletions LinterCopAnalyzers.resx
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,13 @@
<data name="Rule0047LockedLabelsTokDescription" xml:space="preserve">
<value>Locked Label must have a suffix Tok.</value>
</data>
<data name="Rule0048ErrorWithTextConstantTitle" xml:space="preserve">
<value>Use Error with a ErrorInfo or Label variable to improve telemetry details.</value>
</data>
<data name="Rule0048ErrorWithTextConstantFormat" xml:space="preserve">
<value>Use Error with a ErrorInfo or Label variable to improve telemetry details.</value>
</data>
<data name="Rule0048ErrorWithTextConstantDescription" xml:space="preserve">
<value>Use Error with a ErrorInfo or Label variable to improve telemetry details.</value>
</data>
</root>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Further note that you should have BcContainerHelper version 2.0.16 (or newer) in
|[LC0045](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0045)|Zero (0) `Enum` value should be reserved for Empty Value.|Info|
|[LC0046](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0046)|`Label` with suffix Tok must be locked.|Info|
|[LC0047](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0047)|Locked `Label` must have a suffix Tok.|Info|
|[LC0048](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0048)|Use Error with a `ErrorInfo` or `Label` variable to improve telemetry details.|Info|


## Configuration
Expand Down

0 comments on commit 3fbc9d5

Please sign in to comment.