Rule A6-4-1 (required, implementation, automated)
A switch statement shall have at least two case-clauses, distinct from the default label.
Rationale
A switch statement constructed with less than two case-clauses can be expressed as an if statement more naturally. Note that a switch statement with no case-clauses is redundant.
Example
// $Id: A6-4-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
void F1(std::uint8_t choice) noexcept
{
switch (choice)
{
default:
break;
} // Non-compliant, the switch statement is redundant
}
void F2(std::uint8_t choice) noexcept
{
switch (choice)
{
case 0:
// ...
break;
default:
// ...
break;
} // Non-compliant, only 1 case-clause
if (choice == 0) // Compliant, an equivalent if statement
{
// ...
}
else
{
// ...
}
// ...
switch (choice)
{
case 0:
// ...
break;
case 1:
// ...
break;
default:
// ...
break;
} // Compliant
}
See also
MISRA C++ 2008 [7]: Rule 6-4-8 Every switch statement shall have at least one case-clause. HIC++ v4.0 [9]: 6.1.4 Ensure that a switch statement has at least two case labels, distinct from the default label.