Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V1037. Two or more case-branches...
menu mobile close menu
Additional information
toggle menu Contents

V1037. Two or more case-branches perform the same actions.

Apr 03 2019

The analyzer has detected that multiple case clauses of the switch statement contain the same code. It may often indicate redundant code and can be improved by merging the labels. Identical code fragments may also be copy-pasting errors.

The example of a redundant code:

switch (wParam)
{
case WM_MOUSEMOVE:
  ::PostMessage(hWndServer, wParam, 0, 0);
  break;
case WM_NCMOUSEMOVE:
  ::PostMessage(hWndServer, wParam, 0, 0);
  break;
....
default:
  break
}

Different mouse events may trigger the same actions, so the code can be simplified:

switch (wParam)
{
case WM_MOUSEMOVE:
case WM_NCMOUSEMOVE:
  ::PostMessage(hWndServer, wParam, 0, 0);
  break;
....
default:
  break
}

The next example, taken from a real application, demonstrates faulty behavior resulting from a typo:

GLOBAL(void)
jpeg_default_colorspace (j_compress_ptr cinfo)
{
  switch (cinfo->in_color_space) {
  case JCS_GRAYSCALE:
    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
    break;
  case JCS_RGB:
    jpeg_set_colorspace(cinfo, JCS_YCbCr);
    break;
  case JCS_YCbCr:
    jpeg_set_colorspace(cinfo, JCS_YCbCr);
    break;
  ....
  }
  ....
}

The JCS_RGB label contains a typo: JCS_RGB should be passed to the function instead of JCS_YCbCr.

The fixed code:

GLOBAL(void)
jpeg_default_colorspace (j_compress_ptr cinfo)
{
  switch (cinfo->in_color_space) {
  case JCS_GRAYSCALE:
    jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
    break;
  case JCS_RGB:
    jpeg_set_colorspace(cinfo, JCS_RGB);
    break;
  case JCS_YCbCr:
    jpeg_set_colorspace(cinfo, JCS_YCbCr);
    break;
  ....
  }
  ....
}

This diagnostic is classified as:

You can look at examples of errors detected by the V1037 diagnostic.