C#

C#

Made by DeepSource

Explicitly defined case labels are redundant if they fallthrough to default case CS-R1107

Anti-pattern
Major
Autofix

C# allows you to chain one or more cases in a switch statement. This is particularly useful if you have code that can be reused for multiple cases. However, these cases become redundant if the controller eventually falls through them and reaches the default case. In such scenarios, such cases are redundant and can be safely removed.

Bad Practice

switch (node.Kind())
{
    SyntaxKind.ArrayCreationExpression:
        // ...
        break;

    SyntaxKind.MethodDeclaration:
        // ...
        break;

    // These cases eventually lead to the `default` case.
    SyntaxKind.ClassDeclaration:
    SyntaxKind.StructDeclaration:
    default:
        // ...
        break;
}

Recommended

switch (node.Kind())
{
    SyntaxKind.ArrayCreationExpression:
        // ...
        break;

    SyntaxKind.MethodDeclaration:
        // ...
        break;

    // Cases `ClassDeclaration` and `StructDeclaration` are removed.
    default:
        // ...
        break;
}