Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MISRAC++2023-9.4.2_a

In this section:
Synopsis

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

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

Only simple declarations are allowed in switch condition statements.

Coding standards

This check does not correspond to any coding standard rules.

Code examples

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

int f();

int ex(int a, int b)
{
  int x;
  switch (x = f(); x) // Non-compliant - expression
  {
    case 1:
      return a; 
    default:
      return b;
  }
}

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

int f();

int ex(int a, int b)
{
  switch (int x = f(); x) // Compliant - declaration of x is simple
  {
    case 1:
      return a; 
    default:
      return b;
  }
}