diff --git a/TUnit.Core/ContextProvider.cs b/TUnit.Core/ContextProvider.cs index e0e4cf0772..8d502991a6 100644 --- a/TUnit.Core/ContextProvider.cs +++ b/TUnit.Core/ContextProvider.cs @@ -51,13 +51,11 @@ public class ContextProvider(IServiceProvider serviceProvider, string testSessio /// public AssemblyHookContext GetOrCreateAssemblyContext(Assembly assembly) { - return _assemblyContexts.GetOrAdd(assembly, asm => - { - return new AssemblyHookContext(TestSessionContext) + return _assemblyContexts.GetOrAdd(assembly, static (assembly, context) => + new AssemblyHookContext(context) { Assembly = assembly - }; - }); + }, TestSessionContext); } /// diff --git a/TUnit.Core/ObjectInitializer.cs b/TUnit.Core/ObjectInitializer.cs index a8ef5f3378..198e499aef 100644 --- a/TUnit.Core/ObjectInitializer.cs +++ b/TUnit.Core/ObjectInitializer.cs @@ -106,9 +106,10 @@ private static async ValueTask InitializeCoreAsync( // is called exactly once, even under contention. GetOrAdd's factory may be // called multiple times, but Lazy ensures only one initialization runs. var lazyTask = InitializationTasks.GetOrAdd(obj, - _ => new Lazy( + static (_, asyncInitializer) => new Lazy( asyncInitializer.InitializeAsync, - LazyThreadSafetyMode.ExecutionAndPublication)); + LazyThreadSafetyMode.ExecutionAndPublication) + , asyncInitializer); try { diff --git a/TUnit.Core/TestContext.StateBag.cs b/TUnit.Core/TestContext.StateBag.cs index 5d7ef3d0ac..291b2157c9 100644 --- a/TUnit.Core/TestContext.StateBag.cs +++ b/TUnit.Core/TestContext.StateBag.cs @@ -25,7 +25,7 @@ public partial class TestContext /// T ITestStateBag.GetOrAdd(string key, Func valueFactory) { - var value = ObjectBag.GetOrAdd(key, k => valueFactory(k)!); + var value = ObjectBag.GetOrAdd(key, static (k, valueFactory) => valueFactory(k)!, valueFactory); if (value is T typedValue) {