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

The analyzer has detected a situation where different case labels of a switch statement contain the same code. Those are often redundant code, which could be improved by merging the labels. On the other hand, identical code fragments may also result from the use of the copy-paste technique, in which case they are errors rather than simply redundant code.

Consider the following example of redundant code:

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

Indeed, different mouse events may trigger the same actions, so the code can be rewritten in a more compact form:

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

The next example is taken from a real application and 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 code of the JCS_RGB label contains a typo. The developer should have passed the value JCS_RGB, not JCS_YCbCr.

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:

CWE-691

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