V2518. MISRA. The 'default' label should be either the first or the last label of a 'switch' statement.

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

This diagnostic rule is relevant only to C programs.

The 'default' label should be either the first or the last label of a 'switch' statement. Following this rule makes the code clearer.

Here is an example of code triggering this warning:

void example_1(int cond)
{
  switch (cond)
  {
  case 1:
    DoSmth();
    break;
  default:
    DoSmth2();
    break;
  case 3: 
    DoSmth3();
    break;
  }
}

To eliminate the warning, rewrite the code. For example:

void example_1(int cond)
{
  switch (cond)
  {
  case 1: 
    DoSmth();
    break;
  case 3: 
    DoSmth3();
    break;
  default: 
    DoSmth2();
    break;
  }
}

Bugs Found

Checked Projects
409
Collected Errors
14 072
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →
Accept