Static analysis for C# to indicate regions where you missed handling an enum value
Sometimes I code up an if/else block or a switch statement to handle all the different values of an enum and I think, "It would be great if the compiler would warn me if I ever added another value to that enum and forgot to handle it here."
So I wrote this analyzer. You have to opt-in to this check, and you do it with:
throw new net.ajennings.EnumNotExhaustedException<T>();
where T is the enum type that you want to make sure you handled all the values for.
-
Download and double-click the VSIX file (in Releases) and it should install as a visual studio extension. (I think Visual Studio 2017 is required.)
-
Include the definition of EnumNotExhaustedException in your project. Either include this definition:
namespace net.ajennings
{
class EnumNotExhaustedException<T> : System.Exception
{
}
}
or include the EnumNotExhausted.cs file (included with the release) in your project.
Here is an example:
If the analyzer sees you throw the EnumNotExhaustedException
, it checks the surrounding code block to make sure
each possible enum value is mentioned somewhere. If one or more enum values aren't ever mentioned, it raises a diagnostic
error. I don't think it will prevent the project from compiling, though.
If enums are cast from integers or if they are received from other assemblies, they can take values other than the named constants in the enum definition. Enums are really just integers with some syntactic sugar. If you're doing that stuff, this analyzer can't keep you safe at run-time.
I would recommend putting the throw new EnumNotExhaustedException
right after a return
statement. It will still
trigger the static analysis even if it is in unreachable code.