Analyzer to improve safety and convenience of switch statements in C#
An analyzer to improve safety and convenience of switch statements in C#.
case
labels for all enum members.case
labels to such switch statements, preserving order.case
labels by default, it doesn’t care order of labels.In the following sample, a warning is reported on the switch statement because of missing the case for Priority.Middle
.
public enum Priority
{
Low,
Middle,
High,
}
// WARNING: Missing the case for Priority.Middle.
switch (priority)
{
case Priority.Low:
// ...
case Priority.High:
// ...
}
Therefore the codefix generates the case in the middle:
switch (priority)
{
case Priority.Low:
// ...
// *GENERATED*
case Priority.Middle:
throw new NotImplementedException();
case Priority.High:
// ...
}
Note that if the labels aren’t sorted by value then generated labels are added before default label (if exists) or on bottom of switch statement.