Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MISRAC++2023-9.4.2_c

In this section:
Synopsis

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

Enabled by default

Yes

Severity/Certainty

Medium/High

mediumhigh.png
Full description

Switch with a default clause that is not first or last. This check is identical to MISRAC2012-Rule-16.5.

Coding standards
CWE 478

Missing Default Case in Switch Statement

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

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;  
  }
}