perf: restrict AbstractTestClassWithDataSourcesAnalyzer to source assembly#4923
perf: restrict AbstractTestClassWithDataSourcesAnalyzer to source assembly#4923
Conversation
…embly types
The analyzer was recursively walking all types in the entire compilation
(including all referenced assemblies) to find concrete subclasses of
abstract test classes. This is O(n) over all types for every abstract
class analyzed.
Fix by using `Compilation.Assembly.GlobalNamespace` instead of
`Compilation.GlobalNamespace` to only scan types in the source assembly.
This also makes the redundant `IsInSource` location check unnecessary.
Additionally, remove the fragile string-based data source detection
(`typeName.Contains("DataSource") || typeName == "ArgumentsAttribute"`)
which walked the type hierarchy. The `IDataSourceAttribute` interface
check is comprehensive and authoritative since all data source attributes
implement it.
Closes #4864
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. SummaryThis is a well-targeted performance improvement to
|
Summary
GetAllNamedTypes(context.Compilation.GlobalNamespace)toGetAllNamedTypes(context.Compilation.Assembly.GlobalNamespace). Previously the analyzer recursively walked every type in the entire compilation (including all referenced assemblies) for each abstract test class analyzed. Now it only scans types defined in the source assembly, which is all that's needed since subclasses must be in the user's code.IsInSourcefilter: SinceAssembly.GlobalNamespaceonly contains types from the source assembly, the subsequenttype.Locations.Any(l => l.IsInSource)check was redundant and has been removed.typeName.Contains("DataSource") || typeName == "ArgumentsAttribute"string check that walked the type hierarchy. TheIDataSourceAttributeinterface check is comprehensive and authoritative -- all data source attributes (ArgumentsAttribute,MethodDataSourceAttribute,ClassDataSourceAttribute, etc.) implementIDataSourceAttributeeither directly or throughITypedDataSourceAttribute<T>/IAsyncUntypedDataSourceGeneratorAttribute.Closes #4864
Test plan
AbstractTestClassWithDataSourcesAnalyzerTestspass on both .NET 8 and .NET 9dotnet build TUnit.Analyzers/TUnit.Analyzers.csprojsucceeds with no errors