Why is GCC warning me about a fallthrough even when I use [[fallthrough]]?
Asked Answered
P

1

96

In the following piece of code, I use the standard [[fallthrough]] attribute from C++1z to document that a fallthrough is desired:

#include <iostream>

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

With GCC 7.1, the code compiles without an error. However, the compiler still warns me about a fallthrough:

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

Why?

Pathe answered 11/7, 2017 at 6:16 Comment(5)
And here I was, thinking you C++ folks couldn't possibly make things any uglier!Anagnos
@SnakeDoc: It's a gift!Lifesize
@Anagnos That should be a comment on the answer, which makes it even worse. :)Nonplus
I disagree with the close votes. The "a simple typographical error" reason exists for cases where the OP's problem only occurred because of some mistake unrelated to the question description and won't be useful for anybody else. In this case it's quite likely that other people forget the semicolon in that place, get the same error and find this question when looking for a solution.Tatiana
I strongly dislike that my comment was removed from this question so I am forced to repeat it again - while this is indeed a typographical error, it's very likely that other users will repeat it, search for it, and find this question with this answer. As such, it is both a good question and good answer and deserves to be left open.Wang
P
122

You are missing a semicolon after the attribute:

case 0:
    std::cout << "a\n";
    [[fallthrough]];
    //             ^
case 1:

The [[fallthrough]] attribute is to be applied to an empty statement (see P0188R1). The current Clang trunk gives a helpful error in this case:

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

Update: Cody Gray reported this issue to the GCC team.

Pathe answered 11/7, 2017 at 6:16 Comment(5)
Mentioning what the attribute applies to without the semicolon would be nice. I assume it's to the next case?Tatiana
@Tatiana fallthrough attribute is only allowed on empty statements; since it is not followed by an empty statement, gcc simply ignores itAcceleration
@Acceleration That... seems... wrong? It, seemingly, would be more sane to require a semicolon even when followed by an empty statement, and just refuse to compile otherwise.Anagnos
@Anagnos Apparently it's just a particular case of statement attributes, so the parser allows it through, but it doesn't have any semantics when applied to a non-empty statement. Consider it a bug if you wish, and clang fixed it.Confessor
@Tatiana Without the semicolon, the attribute would appertain to the label.Kinross

© 2022 - 2024 — McMap. All rights reserved.