How to write a code fix for an analyzer
As discussed earlier, the .NET Compiler SDK supports more than just writing analyzers that analyze your code. You can also provide code that quickly fixes any errors that occur. These are understood by most editors and IDEs and will automatically be loaded and presented when applicable.
You’ll be reusing the Roslyn.Extensions project for the code fix as well. The code fix needs to tap into specific APIs and needs another package reference. Add a reference to Microsoft.CodeAnalysis.CSharp.Workspaces by running the following in your terminal within the Roslyn.Extensions folder:
dotnet add package Microsoft.CodeAnalysis.CSharp.Workspaces
With the package reference in place, it’s time for the implementation of the code fix:
- Start by adding a file called CodeFix.cs within the CodeAnalysis/ExceptionShouldNotBeSuffixed folder in the Roslyn.Extensions project folder. Add the following to it:
using System.Collections.Immutable...