Main Content

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:

RuleSyntax
switch-statementswitch (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-labelcase const-expression
case-clause

case-block-seqopt break ;

case-block-seqopt throw assignment-expressionopt ;

{ statement-seqopt break ; }

{ statement-seqopt throw assignment-expressionopt ; }

default-label-clausedefault-label default-clause
default-label default-clausedefault : 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 a case-label or default-label.

  • case-clause — The code between any two switch-labels.

  • default-clause — The code between the default-label and the end of the switch statement.

  • switch-clause — Either a case-clause or a default-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 first case statement.

    For instance:

    switch(ch) {
      int temp;
      case 1:
        break;
      default:
        break;
    }

  • A label or a jump statement such as goto or return occurs in the switch 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

expand all

The example fails to follow two of the well-formed switch statement rules from MISRA.

  • The statement int temp; occurs between the switch statement and the first case statement.

  • case 2 contains a return statement.

Polyspace reports a single violation for the entire switch statement despite multiple violations within the switch statement.

int example(int x)
{
	switch (x) {			//Noncompliant
		int temp;
		case 0:
			break;
		case 1:
		case 2:
			return x;
		default:  
			break;
	}

	return x;
}

Check Information

Group: Statements
Category: Required, Automated

Version History

Introduced in R2019a