Skip to content

Introduce a suppressor for CA1515 - #3345

Open
gauravchahal20 wants to merge 2 commits into
dotnet:masterfrom
gauravchahal20:feature/ca1515-suppressor
Open

gauravchahal20 wants to merge 2 commits into
dotnet:masterfrom
gauravchahal20:feature/ca1515-suppressor

Conversation

@gauravchahal20

Copy link
Copy Markdown

Fixes #3343

Adds a \DiagnosticSuppressor\ (BDN1900) that suppresses CA1515 for classes containing benchmark methods. BenchmarkDotNet requires benchmark classes to be public, so CA1515's suggestion to make them internal cannot be followed.

The suppressor resolves the type at the diagnostic location and suppresses CA1515 only when the type or one of its base types declares a method annotated with [Benchmark]\ (or a derived attribute). Inherited benchmark methods are included, matching BenchmarkDotNet's runtime discovery. When BenchmarkDotNet.Annotations is not referenced, the suppressor does nothing.

Tests cover the suppressed and non-suppressed cases, derived attributes, partial classes, nested types, inheritance, and the missing-reference case.

Adds a DiagnosticSuppressor (BDN1900) that suppresses CA1515 for classes
containing benchmark methods, since BenchmarkDotNet requires benchmark
classes to be public. Follows the approach used by the MSTest team.

Fixes dotnet#3343

Signed-off-by: Gaurav Chahal <chhlgrv@gmail.com>
@gauravchahal20

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@timcassell timcassell left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two correctness bugs (nested-type containers, enum/delegate over-suppression) and a test-pack build break; rest are cleanups.

Reviewed by Claude (Opus 5.5), posted by @timcassell.


var root = sourceTree.GetRoot(context.CancellationToken);
var node = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
var typeDeclaration = node.FirstAncestorOrSelf<TypeDeclarationSyntax>();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum and delegate declarations aren't TypeDeclarationSyntax, so CA1515 on a nested public enum / public delegate inside a benchmark class climbs to the outer class and gets suppressed. Match BaseTypeDeclarationSyntax/DelegateDeclarationSyntax, or require the declaration's Identifier span to equal the diagnostic span.

}

var semanticModel = context.GetSemanticModel(sourceTree);
if (semanticModel.GetDeclaredSymbol(typeDeclaration, context.CancellationToken) is INamedTypeSymbol { TypeKind: TypeKind.Class } type

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A public type that only contains a benchmark class isn't suppressed, but BDN requires every declaring type to be public (CompilationValidator: "is within a non-visible class, all declaring types must be public"). So Outer in public class Outer { public class Bench { [Benchmark] ... } } can't follow the rule. Also suppress when any nested type (any depth) is a benchmark class.

Same situation, possibly a follow-up: types used in [GenericTypeArguments(typeof(X))] must be public too ("has non public generic argument(s)").


// BenchmarkDotNet discovers benchmarks through reflection over the type and its base types, so an inherited
// benchmark method makes a derived class a benchmark class as well.
private static bool ContainsBenchmarkMethod(INamedTypeSymbol type, INamedTypeSymbol benchmarkAttributeTypeSymbol)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates HasBenchmarkAttribute in RunAnalyzer.cs, and they already differ (that one stops at System.Object and walks OriginalDefinition.BaseType). Extract a shared AnalyzerHelper method and use it from both.

// benchmark method makes a derived class a benchmark class as well.
private static bool ContainsBenchmarkMethod(INamedTypeSymbol type, INamedTypeSymbol benchmarkAttributeTypeSymbol)
{
for (ITypeSymbol? current = type; current != null; current = current.BaseType)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: stop at SpecialType.System_Object rather than scanning its members. CA1515 fires on every public type, so caching per type (Dictionary<INamedTypeSymbol, bool> with SymbolEqualityComparer.Default) would also avoid re-walking shared base chains.

}
""";

AddExpectedDiagnostic(0, isSuppressed: false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be isSuppressed: true — see the nested-type comment on the suppressor.

}

[Fact]
public async Task Public_class_is_not_suppressed_when_benchmarkdotnet_is_not_referenced()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't fail: Helper has no benchmark method, so it's unsuppressed with or without the benchmarkAttributeTypeSymbol == null guard. Rename it to a no-crash test, or use source that would otherwise be suppressed.


_suppressorTest = new InternalSuppressorTest(_suppressedDiagnosticRule)
{
#if NET10_0_OR_GREATER

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ReferenceAssemblies ladder and AdditionalReferences are copied from AnalyzerTestFixture; share them via a static helper so the next TFM bump can't drift.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce a suppressor for CA1515

2 participants