0

Let's report some diagnostic and register code fix for this diagnostic.

private static void OnCompilation(CompilationAnalysisContext context) {
    var compilation = context.Compilation;
    var diagnostic = Diagnostic.Create( Rule, (Location) null );
    context.ReportDiagnostic( diagnostic );
}

public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) {
    var document = context.Document;
    //var span = context.Span;
    var diagnostic = context.Diagnostics.Single();
    RegisterCodeFix( context, $"Fix compilation", diagnostic, Action );

    Task<Solution> Action(CancellationToken cancellationToken) {
        return Task.FromResult( document.Project.Solution );
    }
}

Problem is that: there is no way to execute code fix action. Light bulb is not shown. And even if I could execute it then document were null here.

So, is it possible to make code fix for compilation-level diagnostics?

Denis535
  • 3,407
  • 4
  • 25
  • 36

1 Answers1

0

Code fixes for project-level diagnostics are not supported. https://github.com/dotnet/roslyn/discussions/50087#discussioncomment-230367

Denis535
  • 3,407
  • 4
  • 25
  • 36