Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 262
s3rvac Avatar asked Jul 11 '17 06:07

s3rvac


People also ask

What is fallthrough c++?

C++ Attributes [[fallthrough]] From C++17 on, a standard attribute was introduced to indicate that the warning is not needed when the code is meant to fall through. Compilers can safely give warnings when a case is ended without break or [[fallthrough]] and has at least one statement.

What is fallthrough in switch statement?

Code Inspection: Fallthrough in 'switch' statementReports a switch statement where control can proceed from a branch to the next one. Such "fall-through" often indicates an error, for example, a missing break or return .

What is implicit Fallthrough?

This check covers implicit fallthrough in switch statements. Implicit fallthrough is when control flow transfers from one switch case directly into a following switch case without the use of the [[fallthrough]]; statement.


1 Answers

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.

like image 179
s3rvac Avatar answered Sep 19 '22 07:09

s3rvac