Maintainability rules

Switch coverage, reflection, and conditional sprawl.

Rules

RuleTitleCode fix
E128088Switch over an enum does not case every memberYes
E128090Avoid reflection in test methods (default: Info)No
E128199Method calls itself directly (default: Info)No
E128201Conditional directives past the ceiling (default: Info)No

Rule details

E128088: Switch over an enum does not case every member

Flags a switch statement or switch expression over a non-nullable enum. The rule reports when at least one member has no case and the switch has no default, discard, or var arm. An or pattern counts for each of its operands. A nullable enum is not checked. A new enum member otherwise falls through silently.

// Before (warns)
var label = color switch
{
    Color.Red => "red",
    Color.Green => "green"
};

// After
var label = color switch
{
    Color.Red => "red",
    Color.Green => "green",
    _ => "unknown"
};

E128090: Reflection in test methods (Info)

Flags GetMethod, GetProperty, and GetField reflection calls inside test methods, plus a use of the BindingFlags type. A type that derives from System.Type, such as TypeBuilder, counts as a reflection call. Reflection couples tests to private implementation detail; prefer testing through the public API. Default severity: Info. No code fix.

// Before (info)
var m = typeof(Service).GetMethod("Compute", BindingFlags.NonPublic | BindingFlags.Instance);

// After — exercise the public surface
var result = new Service().Run();

E128199: Method calls itself directly

Flags a method whose body invokes the same method. The call graph becomes cyclic, so the stack depth grows with the input. The rule matches the callee symbol, so an overload or a same-name method on another type is not reported. A local function that calls the outer method is reported, because that call also re-enters the method. No code fix.

// Before (warns)
int Down(int n) => n <= 0 ? 0 : Down(n - 1);

// After
int Down(int n)
{
    var total = 0;
    for (; n > 0; n--)
    {
        total += n;
    }

    return total;
}

E128201: Conditional directives past the ceiling

Flags a file that holds more #if directives than the configured ceiling. Each directive multiplies the set of build variants, so the test matrix grows past what a team can verify. The rule reports the first directive past the ceiling. The option e128_max_conditional_directives sets the ceiling and defaults to

  1. A missing or non-numeric value falls back to the default. No code fix.
// Before (warns when the file holds more than four #if directives)
#if FEATURE_A
    int A() => 1;
#endif