How to write an analyzer
Writing an analyzer that gets automatically run as part of the compile process has been made very simple by Microsoft. It follows the same principles as source generators, as we saw in Chapter 16, Generating Code. Once you have the project set up, as we did back in Chapter 15, Roslyn Compiler Extensions, it’s all about dropping in a class that represents the analyzer.
In this chapter, all code assumes you have the Roslyn.Extensions project that we established in Chapter 15, Roslyn Compiler Extensions.
The analyzer we’re going to make is a highly opinionated one that affects the naming of exception types. One of the things we tend to do is to suffix our types with what they technically represent; for instance, exceptions are often suffixed with Exception. Looking at exceptions found in the .NET base class libraries, you’ll see things such as NotImplementedException, ArgumentException, or ArgumentNullException. This is something I personally...