Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Analyzer to improve safety and convenience of switch statements in C#

License

Notifications You must be signed in to change notification settings

vain0x/SwitchCoverageAnalyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SwitchCoverageAnalyzer

An analyzer to improve safety and convenience of switch statements in C#.

Features

  • Reports a warning for each switch statement on enum value that doesn't contain case labels for all enum members.
  • Provides codefixes to insert missing case labels to such switch statements, preserving order.
    • NOTE: Although Visual Studio 2017 provides the codefix to insert missing case labels by default, it doesn't care order of labels.

Example

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.

About

Analyzer to improve safety and convenience of switch statements in C#

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published