diff --git a/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs b/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs index b2d1f25479d08..a055b6fdfed47 100644 --- a/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs +++ b/src/Analyzers/Core/CodeFixes/NamingStyle/NamingStyleCodeFixProvider.cs @@ -18,6 +18,7 @@ using Microsoft.CodeAnalysis.Rename; using Microsoft.CodeAnalysis.Shared.Extensions; using Roslyn.Utilities; +using Microsoft.CodeAnalysis.Shared.Collections; #if !CODE_STYLE // https://github.com/dotnet/roslyn/issues/42218 removing dependency on WorkspaceServices. using Microsoft.CodeAnalysis.CodeActions.WorkspaceServices; @@ -163,12 +164,17 @@ protected override async Task> ComputeOperation #if CODE_STYLE // https://github.com/dotnet/roslyn/issues/42218 tracks removing this conditional code. return SpecializedCollections.SingletonEnumerable(codeAction); #else - var factory = _startingSolution.Services.GetRequiredService(); - return new CodeActionOperation[] + + using var operations = TemporaryArray.Empty; + + operations.Add(codeAction); + var factory = _startingSolution.Services.GetService(); + if (factory is not null) { - codeAction, - factory.CreateSymbolRenamedOperation(_symbol, _newName, _startingSolution, newSolution) - }; + operations.Add(factory.CreateSymbolRenamedOperation(_symbol, _newName, _startingSolution, newSolution)); + } + + return operations.ToImmutableAndClear(); #endif }