C & C++

C & C++

Made by DeepSource

Missing unconditional break statement in switch clause CXX-C1001

Anti-pattern
Minor

Fall-through in switch cases is often considered risky. Hence consider adding an unconditional break for each switch clause.

You may want to ignore this issue if your code-style makes extensive use of fall-through pattern.

Bad practice

int main() {
    switch (1) {
        case 0: {
            // [fallthrough]
        };
        case 1: {
            // [fallthrough]
        };
        case 2: {
            // ..
        } break;
        default: {
            // ..
        }
    }
}

Recommended

int main() {
    switch (1) {
        case 0: {
            // ..
        } break;
        case 1: {
            // ..
        } break;
        case 2: {
            // ..
        } break;
        default: {
            // ..
        }
    }
}