V2525. MISRA. Every 'switch' statement should contain non-empty switch-clauses.


This diagnostic rule is based on the software development guidelines developed by MISRA (Motor Industry Software Reliability Association).

This diagnostic varies for C and C++.

C: Every 'switch' statement should have at least two non-empty labels, such as 'case' or 'default'.

C++: Every 'switch' statement should have at least one non-empty label 'case'.

'switch' constructs that do not meet these requirements are redundant and may indicate a programming mistake.

Example 1:

void example_1(int param)
{
  switch(param)
  {
    case 0:
    default:
      Func();
      break;
  }
}

This 'switch' is redundant and meaningless. No matter the value of 'param', only the body of the 'default' label will be executed.

The following example does not trigger the warning:

void example_2(int param)
{
  switch(param)
  {
    case 0:
      DoSmth1();
      break;
    case 1:
      DoSmth2();
      break;
    ....
    default:
      Func();
      break;
  }
}

Here is an example where the analyzer issues the warning only when using a C compiler:

void example_3(int param)
{
  switch(param)
  {
    case 10:
    case 42:
      DoMath();
      break;
  }
}

Bugs Found

Checked Projects
363
Collected Errors
13 495