项目作者: vain0x

项目描述 :
Analyzer to improve safety and convenience of switch statements in C#
高级语言: C#
项目地址: git://github.com/vain0x/SwitchCoverageAnalyzer.git
创建时间: 2017-08-22T15:07:15Z
项目社区:https://github.com/vain0x/SwitchCoverageAnalyzer

开源协议:MIT License

下载


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.

  1. public enum Priority
  2. {
  3. Low,
  4. Middle,
  5. High,
  6. }
  7. // WARNING: Missing the case for Priority.Middle.
  8. switch (priority)
  9. {
  10. case Priority.Low:
  11. // ...
  12. case Priority.High:
  13. // ...
  14. }

Therefore the codefix generates the case in the middle:

  1. switch (priority)
  2. {
  3. case Priority.Low:
  4. // ...
  5. // *GENERATED*
  6. case Priority.Middle:
  7. throw new NotImplementedException();
  8. case Priority.High:
  9. // ...
  10. }

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.