Bug 81392 - Improve diagnostics for [[fallthrough]] attribute that is missing a semicolon
Summary: Improve diagnostics for [[fallthrough]] attribute that is missing a semicolon
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: David Malcolm
Keywords: diagnostic
Depends on:
Reported: 2017-07-11 10:42 UTC by Cody Gray
Modified: 2022-05-27 09:00 UTC (History)
3 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-11 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
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";
            case 1:
                std::cout << "b\n";

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
    note: did you forget ';'?

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.