Repository: dotnet/roslyn
File: src/Analyzers/CSharp/CodeFixes/MakeLocalFunctionStatic/PassInCapturedVariablesAsArgumentsCodeFixProvider.cs (C#)

Code before:
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.MakeLocalFunctionStatic;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.PassInCapturedVariables), Shared]
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class PassInCapturedVariablesAsArgumentsCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
    private const string CS8421 = nameof(CS8421); // A static local function can't contain a reference to <variable>.

    public override ImmutableArray<string> FixableDiagnosticIds { get; } = [CS8421];

    public override Task RegisterCodeFixesAsync(CodeFixContext context)
    {
        RegisterCodeFix(context, CSharpCodeFixesResources.Pass_in_captured_variables_as_arguments, nameof(CSharpCodeFixesResources.Pass_in_captured_variables_as_arguments));
        return Task.CompletedTask;
    }

    protected override async Task FixAllAsync(
        Document document,
        ImmutableArray<Diagnostic> diagnostics,
        SyntaxEditor editor,
        CancellationToken cancellationToken)
    {
        var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

        // Even when the language version doesn't support static local function, the compiler will still generate
        // this error. So we need to check to make sure we don't provide incorrect fix.
        if (!MakeLocalFunctionStaticHelper.IsStaticLocalFunctionSupported(root.SyntaxTree.Options.LanguageVersion()))
            return;

        // Find all unique local functions that contain the error.
        var localFunctions = diagnostics
            .Select(d => root.FindNode(d.Location.SourceSpan).AncestorsAndSelf().OfType<LocalFunctionStatementSyntax>().FirstOrDefault())
            .WhereNotNull()
            .Distinct()
            .ToImmutableArrayOrEmpty();

        if (localFunctions.Length == 0)
            return;

Review comment (refactor):
does this not need the logic in WrapFixAsync anymore, like checking if static local functions are supported or the checks in `MakeLocalFunctionStaticHelper.CanMakeLocalFunctionStaticByRefactoringCaptures`?

Code after:
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.MakeLocalFunctionStatic;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.PassInCapturedVariables), Shared]
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class PassInCapturedVariablesAsArgumentsCodeFixProvider() : SyntaxEditorBasedCodeFixProvider
{
    private const string CS8421 = nameof(CS8421); // A static local function can't contain a reference to <variable>.

    public override ImmutableArray<string> FixableDiagnosticIds { get; } = [CS8421];

    public override Task RegisterCodeFixesAsync(CodeFixContext context)
    {
        var diagnostic = context.Diagnostics.First();

        return WrapFixAsync(
            context.Document,
            [diagnostic],
            (document, localFunction, captures) =>
            {
                context.RegisterCodeFix(
                    CodeAction.Create(
                        CSharpCodeFixesResources.Pass_in_captured_variables_as_arguments,
                        cancellationToken => MakeLocalFunctionStaticCodeFixHelper.MakeLocalFunctionStaticAsync(document, localFunction, captures, cancellationToken),
                        nameof(CSharpCodeFixesResources.Pass_in_captured_variables_as_arguments)),
                    diagnostic);

                return Task.CompletedTask;
            },
            context.CancellationToken);
    }

    protected override Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
        => WrapFixAsync(
            document,
            diagnostics,
            (document, localFunction, captures) => MakeLocalFunctionStaticCodeFixHelper.MakeLocalFunctionStaticAsync(
                document, localFunction, captures, editor, cancellationToken),