<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - switch case with assert(false) causes -Wimplicit-fallthrough warnings in both debug and release builds"
   href="https://llvm.org/bugs/show_bug.cgi?id=25966">25966</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>switch case with assert(false) causes -Wimplicit-fallthrough warnings in both debug and release builds
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>cpeterson@mozilla.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>A not uncommon code pattern is to assert(false) in a switch statement's default
case but then fall through to a handled case.

Unfortunately, the assert(false) causes an "unannotated fall-through between
switch labels" warning in release (NDEBUG) builds and a "fallthrough annotation
in unreachable code" warning in DEBUG builds if you add a
[[clang::fallthrough]] annotation.

Possible workarounds:

1. Wrap the assert(false) in #ifdef DEBUG so there is no inert code in release
(NDEBUG) builds to cause an "unannotated fall-through between switch labels"
warning.

2. #define a special FALLTHROUGH_ASSERT() macro omits the unreachable
[[clang::fallthrough]] annotation in DEBUG builds. I took this approach in
Firefox, for the time being:

<a href="https://hg.mozilla.org/integration/mozilla-inbound/rev/d0a8d632dce5#l1.45">https://hg.mozilla.org/integration/mozilla-inbound/rev/d0a8d632dce5#l1.45</a>

3. clang could allow a [[clang::fallthrough]] annotation after a call to a
noreturn function like abort(), but still warn about unreachable
[[clang::fallthrough]] annotations after an early break or return.

TEST CASE:

#include <assert.h>

int main(int argc, const char**)
{
    // clang -Wimplicit-fallthrough -std=c++11 -DNDEBUG test.cpp
    // warning: unannotated fall-through between switch labels
[-Wimplicit-fallthrough]
    switch (argc) {
        default:
            assert(false); // assert on an unexpected switch value
        case 0:
            break;
    }

    // clang -Wimplicit-fallthrough -std=c++11 -DDEBUG test.cpp
    // warning: fallthrough annotation in unreachable code
[-Wimplicit-fallthrough]
    switch (argc) {
        default:
            assert(false); // assert on an unexpected switch value
            [[clang::fallthrough]];
        case 0:
            break;
    }

    // Workaround #1:
    switch (argc) {
        default:
#ifdef DEBUG
            assert(false); // assert on an unexpected switch value
#endif
        case 0:
            break;
    }

    // Workaround #2:

#ifdef DEBUG
#define FALLTHROUGH_ASSERT() assert(false)
#else
#define FALLTHROUGH_ASSERT() [[clang::fallthrough]]
#endif

    switch (argc) {
        default:
            FALLTHROUGH_ASSERT(); // assert on an unexpected switch value
        case 0:
            break;
    }

    return 0;
}</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>