The newer versions of gcc offer the Wimplicit-fallthrough
, which is great to have for most switch statements. However, I have one switch statement where I want to allow fall throughs from all case-statements.
Is there a way to do an explicit fall through? I'd prefer to avoid having to compile with Wno-implicit-fallthrough
for this file.
EDIT: I'm looking for a way to make the fall through explicit (if it's possible), not to turn off the warning via a compiler switch or pragma.
Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add a break statement and in that case flow of control jumps to the next line.
A fallthrough statement may only be used in a switch statement, where the next statement to be executed is a statement with a case or default label for that switch statement.
Implicit fallthrough is when control flow transfers from one switch case directly into a following switch case without the use of the [[fallthrough]]; statement. This warning is raised when an implicit fallthrough is detected in a switch case containing at least one statement.
C++ Attributes [[fallthrough]]Whenever a case is ended in a switch , the code of the next case will get executed. This last one can be prevented by using the ´break` statement. As this so-called fallthrough behavior can introduce bugs when not intended, several compilers and static analyzers give a warning on this.
Use __attribute__ ((fallthrough))
switch (condition) { case 1: printf("1 "); __attribute__ ((fallthrough)); case 2: printf("2 "); __attribute__ ((fallthrough)); case 3: printf("3\n"); break; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With