# no-duplicate-cases

Disallow duplicate test cases in switch statements.

switch statement clauses can freely have duplicate tests, however this is almost always a mistake, because the second case is unreachable. It is likely that the programmer copied a case clause but did not change the test for it.

# Invalid Code Examples

switch (a) {
    case 1:
        break;
    case 2:
        break;
    case 1:
        break;
    default:
        break;
}
switch (a) {
    case foo.bar:
        break;

    case foo . bar:
        break;
}
More incorrect examples
switch (foo) {
    case foo. bar:
    break;

    case foo.bar:
    break;
}
switch foo {
    case 5:
    break;

    case 6:
    break;

    case 5:
    break;
}

Source (opens new window)

Last Updated: 11/18/2020, 9:36:33 PM