Repository navigation
Introduce a suppressor for CA1515 - #3345
gauravchahal20 wants to merge 2 commits into
Conversation
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>
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
The ReferenceAssemblies ladder and AdditionalReferences are copied from AnalyzerTestFixture; share them via a static helper so the next TFM bump can't drift.
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.