Skip to content

Commit 3733777

Browse files
authored
Remove some redundant immutable array allocation (#4988)
* remove some redundant ImmutableArray allocation * remove some redundant ImmutableArray allocation
1 parent fa2aed7 commit 3733777

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

TUnit.Analyzers/TestDataAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private void AnalyzeMethod(SymbolAnalysisContext context)
5757

5858
var attributes = methodSymbol.GetAttributes();
5959

60-
Analyze(context, attributes, methodSymbol.Parameters.WithoutCancellationTokenParameter().ToImmutableArray(), null, methodSymbol.ContainingType);
60+
Analyze(context, attributes, methodSymbol.Parameters.WithoutCancellationTokenParameter(), null, methodSymbol.ContainingType);
6161
}
6262

6363
private void AnalyzeProperty(SymbolAnalysisContext context)

TUnit.Assertions.SourceGenerator/Generators/AssertionMethodGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2626
predicate: (node, _) => node is ClassDeclarationSyntax,
2727
transform: (ctx, _) => GetGenericCreateAssertionAttributeData(ctx, "AssertionFromAttribute"))
2828
.Where(x => x != null)
29-
.SelectMany((x, _) => x!.ToImmutableArray());
29+
.SelectMany((x, _) => x!);
3030

3131
// Combine all sources
3232
var allAttributeData = nonGenericAssertionFromData.Collect()

TUnit.Core.SourceGenerator/Generators/PropertyInjectionSourceGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
7373
.CreateSyntaxProvider(
7474
predicate: static (node, _) => node is TypeDeclarationSyntax or PropertyDeclarationSyntax,
7575
transform: static (ctx, _) => ExtractConcreteGenericTypes(ctx))
76-
.Where(static x => x.Length > 0)
76+
.Where(static x => x.Count > 0)
7777
.SelectMany(static (types, _) => types);
7878

7979
// Collect and deduplicate by fully qualified type name
@@ -326,17 +326,17 @@ private static IEnumerable<ClassPropertyInjectionModel> GroupPropertiesByClass(
326326
/// 1. Inheritance chains (base types)
327327
/// 2. IDataSourceAttribute type arguments
328328
/// </summary>
329-
private static ImmutableArray<ConcreteGenericTypeModel> ExtractConcreteGenericTypes(GeneratorSyntaxContext context)
329+
private static List<ConcreteGenericTypeModel> ExtractConcreteGenericTypes(GeneratorSyntaxContext context)
330330
{
331331
var semanticModel = context.SemanticModel;
332-
var results = new List<ConcreteGenericTypeModel>();
333332

334333
var dataSourceInterface = semanticModel.Compilation.GetTypeByMetadataName("TUnit.Core.IDataSourceAttribute");
335334
var asyncInitializerInterface = semanticModel.Compilation.GetTypeByMetadataName("TUnit.Core.Interfaces.IAsyncInitializer");
336335

337336
if (dataSourceInterface == null || asyncInitializerInterface == null)
338-
return ImmutableArray<ConcreteGenericTypeModel>.Empty;
337+
return [];
339338

339+
var results = new List<ConcreteGenericTypeModel>();
340340
// Discovery from type declarations (inheritance chains)
341341
if (context.Node is TypeDeclarationSyntax typeDecl)
342342
{
@@ -432,7 +432,7 @@ private static ImmutableArray<ConcreteGenericTypeModel> ExtractConcreteGenericTy
432432
}
433433
}
434434

435-
return results.Count > 0 ? results.ToImmutableArray() : ImmutableArray<ConcreteGenericTypeModel>.Empty;
435+
return results;
436436
}
437437

438438
/// <summary>

TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5039,8 +5039,7 @@ private static void GenerateConcreteTestMetadataForNonGeneric(
50395039

50405040
var attributes = filteredMethodAttributes
50415041
.Concat(filteredClassAttributes)
5042-
.Concat(testMethod.TypeSymbol.ContainingAssembly.GetAttributes())
5043-
.ToImmutableArray();
5042+
.Concat(testMethod.TypeSymbol.ContainingAssembly.GetAttributes());
50445043

50455044
testMethod.CompilationContext.AttributeWriter.WriteAttributes(writer, attributes);
50465045

0 commit comments

Comments
 (0)