AUTOSAR C++14 Rule M6-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-labelordefault-label.case-clause— The code between any twoswitch-labels.default-clause— The code between thedefault-labeland the end of theswitchstatement.switch-clause— Either acase-clauseor 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_statementjump_statementdeclaration_statement
Polyspace Implementation
The rule checker reports a violation in these situations:
A statement occurs between the
switchstatement and the firstcasestatement.For instance:
switch(ch) { int temp; case 1: break; default: break; }A label or a jump statement such as
gotoorreturnoccurs in theswitchblock.A variable is declared in a
casestatement 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, Automated |
Version History
Introduced in R2019a