MISRA C++:2008 Rule 6-4-3
A switch statement shall be a well-formed switch statement
Description
Rule Definition
A switch statement shall be a well-formed switch statement.
Rationale
In addition to the C++ standard syntax rules, MISRA defines their own syntax rules for creating well-formed switch statements. These additional syntax rules create a consistent structure for switch statements.
The additional MISRA syntax rules include:
Rule | Syntax |
---|---|
switch-statement | switch (condition) {case-label-clause-list
default-label-clauseopt} |
case-label-clause-list | case-label case-clauseopt case-label-clause-list case-label case-clauseopt |
case-label | case const-expression |
case-clause | case-block-seqopt
case-block-seqopt
{ statement-seqopt
{
statement-seqopt
|
default-label-clause | default-label default-clause |
default-label default-clause | default : case-clause |
case-block | expression_statement compound_statement selection_statement iteration_statement try_block |
case-block-seq | case-block case-block-seq case-block |
These terms from the table are defined as such:
switch-label
— Either acase-label
ordefault-label
.case-clause
— The code between any twoswitch-labels
.default-clause
— The code between thedefault-label
and the end of theswitch
statement.switch-clause
— Either acase-clause
or adefault-clause
.
The MISRA C++ switch
syntax rules do not include the following
statements, but do permit them within the compound statements that form the body of a
switch-clause
statement.
labelled_statement
jump_statement
declaration_statement
Polyspace Implementation
The rule checker reports a violation in these situations:
A statement occurs between the
switch
statement and the firstcase
statement.For instance:
switch(ch) { int temp; case 1: break; default: break; }
A label or a jump statement such as
goto
orreturn
occurs in theswitch
block.A variable is declared in a
case
statement that is outside any block.For instance:
switch(ch) { case 1: int temp; break; default: break; }
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Statements |
Category: Required |
Version History
Introduced in R2013b