Bug 81392

Summary: Improve diagnostics for [[fallthrough]] attribute that is missing a semicolon
Product: gcc Reporter: Cody Gray <cody>
Component: c++Assignee: David Malcolm <dmalcolm>
Status: ASSIGNED ---    
Severity: normal CC: daniel.kruegler, jaak, webrown.cpp
Priority: P3 Keywords: diagnostic
Version: 8.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2017-07-11 00:00:00

Description Cody Gray 2017-07-11 10:42:20 UTC
The C++1z/C++17 draft standard introduces a [[fallthrough]] attribute to explicitly document that fall-through behavior is intended in a switch-case block.

This works in conjunction with G++'s -Wimplicit-fallthrough option, which gives a warning about potentially unintended fall-through behaviors.

The [[fallthrough]] attribute is required to be applied to an empty statement (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0188r1.pdf), and therefore requires a terminating semicolon.

However, forgetting that semicolon is a common error. With the following code:

    #include <iostream>

    int main() {
        switch (0) {
            case 0:
                std::cout << "a\n";
                [[fallthrough]]
            case 1:
                std::cout << "b\n";
                break;
        }
    }

G++ (7.1 and the current trunk of 8.0) issues the following warning:

    warning: this statement may fall through [-Wimplicit-fallthrough=]
        std::cout << "a\n";
        ~~~~~~~~~~^~~~~~~~

This is less helpful than it could be. The current Clang trunk provides a substantially more helpful error message in this case:

    error: fallthrough attribute is only allowed on empty statements
        [[fallthrough]]
          ^
    note: did you forget ';'?
        [[fallthrough]]
                       ^
                       ;

It would be nice to have something similar in G++.
Comment 1 Jakub Jelinek 2019-05-03 09:14:27 UTC
GCC 9.1 has been released.
Comment 2 Jakub Jelinek 2019-08-12 08:53:47 UTC
GCC 9.2 has been released.
Comment 3 Jakub Jelinek 2020-03-12 11:58:42 UTC
GCC 9.3.0 has been released, adjusting target milestone.
Comment 4 Richard Biener 2021-06-01 08:09:02 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.