Repository: dotnet/aspnetcore
File: src/Framework/AspNetCoreAnalyzers/src/CodeFixes/PublicPartialProgramClassFixer.cs (C#)

Code before:
    public override ImmutableArray<string> FixableDiagnosticIds { get; } = [DiagnosticDescriptors.PublicPartialProgramClassNotRequired.Id];

    public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

    public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
    {
        foreach (var diagnostic in context.Diagnostics)
        {
            context.RegisterCodeFix(
                CodeAction.Create("Fix unnecessary public partial class Program",
                    async cancellationToken =>
                    {
                        var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false);
                        var root = await context.Document.GetSyntaxRootAsync(cancellationToken);
                        if (root is null)
                        {
                            return context.Document;
                        }

                        var classDeclaration = root.FindNode(diagnostic.Location.SourceSpan)
                            .FirstAncestorOrSelf<ClassDeclarationSyntax>();
                        if (classDeclaration is null)
                        {
                            return context.Document;
                        }
                        editor.RemoveNode(classDeclaration, SyntaxRemoveOptions.KeepNoTrivia);
                        return editor.GetChangedDocument();
                    },
                    equivalenceKey: DiagnosticDescriptors.PublicPartialProgramClassNotRequired.Id),
                diagnostic);
        }

        return Task.CompletedTask;
    }
}


Review comment (suggestion):
Does `KeepNoTrivia` do something smart about comments that a user wouldn't regard as attached to the declaration?  e.g. 
```csharp
// Copyright 2022

public partial class Program;
```

(I think top-of-file might get special handling, but I couldn't think of a better example for the end of a file.)

Code after:
    public override ImmutableArray<string> FixableDiagnosticIds { get; } = [DiagnosticDescriptors.PublicPartialProgramClassNotRequired.Id];

    public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

    public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
    {
        foreach (var diagnostic in context.Diagnostics)
        {
            context.RegisterCodeFix(
                CodeAction.Create("Remove unnecessary public partial class Program declaration",
                    async cancellationToken =>
                    {
                        var editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(false);
                        var root = await context.Document.GetSyntaxRootAsync(cancellationToken);
                        if (root is null)
                        {
                            return context.Document;
                        }

                        var classDeclaration = root.FindNode(diagnostic.Location.SourceSpan)
                            .FirstAncestorOrSelf<ClassDeclarationSyntax>();
                        if (classDeclaration is null)
                        {
                            return context.Document;
                        }
                        editor.RemoveNode(classDeclaration, SyntaxRemoveOptions.KeepExteriorTrivia);
                        return editor.GetChangedDocument();
                    },
                    equivalenceKey: DiagnosticDescriptors.PublicPartialProgramClassNotRequired.Id),
                diagnostic);
        }

        return Task.CompletedTask;
    }
}