Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MISRAC2012-Rule-16.5

In this section:
Synopsis

(Required) A default label shall appear as either the first or the last switch label of a switch statement

Enabled by default

Yes

Severity/Certainty

Medium/High

mediumhigh.png
Full description

A switch was found whose default label is neither the first nor the last label of the switch. This check is identical to MISRAC++2023-9.4.2_c.

Coding standards
MISRA C:2012 Rule-16.5

(Required) A default label shall appear as either the first or the last switch label of a switch statement

MISRA C++ 2023 9.4.2

(Required) The structure of a switch statement shall be appropriate

Code examples

The following code example fails the check and will give a warning:

void test(int a) {
  switch (a) {
    case 1:
      a = 1;
      break; 
    default: 
      a = 10;
      break; 
    case 2:
      a = 2;
      break;
  }
}

The following code example passes the check and will not give a warning about this issue:

void test(int a) {
  switch (a) {
    case 1:
      a = 1;
      break; 
    case 2:
      a = 2;
      break;
    default: 
      a = 10;
      break;  
  }
}