Skip to content

Commit a688920

Browse files
committed
Adjust wording of "ClassWithGenericTypeArgumentsAttributeMustBeNonAbstract" diagnostic and move trigger location to the attribute level
1 parent 3566b91 commit a688920

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/BenchmarkDotNet.Analyzers/BenchmarkDotNetAnalyzerResources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BenchmarkDotNet.Analyzers/BenchmarkDotNetAnalyzerResources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
<value>Benchmark class '{0}' cannot be static</value>
143143
</data>
144144
<data name="General_BenchmarkClass_ClassWithGenericTypeArgumentsAttributeMustBeNonAbstract_MessageFormat" xml:space="preserve">
145-
<value>Benchmark class '{0}' cannot be abstract</value>
145+
<value>Attribute [GenericTypeArguments] can only be applied to a non-abstract class</value>
146146
</data>
147147
<data name="BenchmarkRunner_Run_GenericTypeArgumentClassMustBeAnnotatedWithAGenericTypeArgumentsAttribute_MessageFormat" xml:space="preserve">
148148
<value>Referenced generic benchmark class '{0}' has no [GenericTypeArguments] attribute(s)</value>

src/BenchmarkDotNet.Analyzers/General/BenchmarkClassAnalyzer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
140140
var genericTypeArgumentsAttributes = AnalyzerHelper.GetAttributes("BenchmarkDotNet.Attributes.GenericTypeArgumentsAttribute", context.Compilation, classDeclarationSyntax.AttributeLists, context.SemanticModel);
141141
if (genericTypeArgumentsAttributes.Length > 0 )
142142
{
143-
if (classAbstractModifier.HasValue)
144-
{
145-
context.ReportDiagnostic(Diagnostic.Create(ClassWithGenericTypeArgumentsAttributeMustBeNonAbstractRule, classAbstractModifier.Value.GetLocation(), classDeclarationSyntax.Identifier.ToString()));
146-
}
147-
148143
foreach (var genericTypeArgumentsAttribute in genericTypeArgumentsAttributes)
149144
{
145+
if (classAbstractModifier.HasValue)
146+
{
147+
context.ReportDiagnostic(Diagnostic.Create(ClassWithGenericTypeArgumentsAttributeMustBeNonAbstractRule, genericTypeArgumentsAttribute.GetLocation()));
148+
}
149+
150150
if (classDeclarationSyntax.TypeParameterList == null || classDeclarationSyntax.TypeParameterList.Parameters.Count == 0)
151151
{
152152
context.ReportDiagnostic(Diagnostic.Create(ClassWithGenericTypeArgumentsAttributeMustBeGenericRule, genericTypeArgumentsAttribute.GetLocation()));

tests/BenchmarkDotNet.Analyzers.Tests/AnalyzerTests/General/BenchmarkClassAnalyzerTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public async Task Abstract_class_annotated_with_at_least_one_generictypeargument
4848
{
4949
const string benchmarkClassName = "BenchmarkClass";
5050

51-
var genericTypeArgumentsAttributeUsages = Enumerable.Repeat("[GenericTypeArguments(typeof(int))]", genericTypeArgumentsAttributeUsageCount);
51+
var genericTypeArgumentsAttributeUsages = Enumerable.Repeat("[{{|#{0}:GenericTypeArguments(typeof(int))|}}]", genericTypeArgumentsAttributeUsageCount).Select((a, i) => string.Format(a, i));
5252

5353
var testCode = /* lang=c#-test */ $$"""
5454
using BenchmarkDotNet.Attributes;
5555
5656
{{string.Join("\n", genericTypeArgumentsAttributeUsages)}}
57-
public {|#0:abstract|} class {{benchmarkClassName}}<TParameter>
57+
public abstract class {{benchmarkClassName}}<TParameter>
5858
{
5959
{{benchmarkAttributeUsage}}
6060
public void BenchmarkMethod()
@@ -65,7 +65,11 @@ public void BenchmarkMethod()
6565
""";
6666

6767
TestCode = testCode;
68-
AddDefaultExpectedDiagnostic(benchmarkClassName);
68+
69+
for (var i = 0; i < genericTypeArgumentsAttributeUsageCount; i++)
70+
{
71+
AddExpectedDiagnostic(i);
72+
}
6973

7074
await RunAsync();
7175
}

0 commit comments

Comments
 (0)