The code contains too many switch cases with missing ending breaks

Why you should care

Ending switch branches with ‘breaks’ helps the code be more consistent, functional, and easier to debug. This allows developers in different teams navigate the code easier and be more productive in the development process. Lack of breaks makes the code difficult for other developers to understand.

How we detect

CAST Highlight counts one occurrence each time a non empty case/default statement is not ending with a break, continue, return, or throw statement.

int main()
{
int i = 2;
switch(i)
{
case 0:
cout << “0” << endl;
// Violation
case 1:
// No violation: Empty clause
case 2:
cout << “1 or 2” << endl;
break; // No violation: A break
default:
cout << “Other” << endl;
// No violation: Last clause is default
}
}

References

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.