No Duplicate Case

ID

csharp.no_duplicate_case

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

CSharp

Tags

dead-code, reliability, suspicious-comparison

Description

Reports switch statements where two case sections have an identical statement body. Because C# forbids implicit fall-through, repeating another section’s body is copy-paste weight, and frequently hides a typo where the body was meant to differ.

Rationale

When two cases run exactly the same statements, the intent is usually to share one branch — which C# expresses by stacking labels (case 1: case 2: …​). Writing the body out twice duplicates maintenance effort, and a duplicate just as often signals that the second body was supposed to be edited after a copy-paste but never was.

public class Router
{
    public string Broken(int code)
    {
        switch (code)
        {
            case 1:
                return "retry";
            case 2:               // FLAW — body identical to case 1; was case 2 meant to differ?
                return "retry";
            default:
                return "stop";
        }
    }

    public string Ok(int code)
    {
        switch (code)
        {
            case 1:
            case 2:               // OK — stacked labels share one body intentionally
                return "retry";
            default:
                return "stop";
        }
    }
}

Remediation

If the cases truly share behaviour, stack their labels onto a single section (case 1: case 2: …​). If the duplicate was meant to do something different, correct the body that was copied by mistake.