MISRA Discussion Forums

Full Version: 15.1 and "switch(0) case 0:"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
Given the following code:
Code:
...
switch(0)
case 0:
...
does the example presented violate rule #15.1? Syntactically, the "switch(expression)" is followed by a "statement" and not necessarily a "compound statement" as stated in the rule. Therefore, can we correctly presume said code violates said rule?
The standard does not require the "body" of the switch statement to be a compound statement, so it would indeed by syntactically permitted to write the example given.

However, Rule 14.8 requires that the body be a compound statement. Further, Technical Corrigendum 1 introduced Rule 15.0 which specifies a more restricted form of syntax for the switch statement and this also requires that the body of a switch statement be a compound statement. Therefore, in the presence of Rules 14.8 and 15.0, the example does violate Rule 15.1.

Note that even if Rules 14.8 and 15.0 were deviated, the example given would violate Rule 15.1 unless the containing compound statement is also a switch body. For example:

Code:
void f (void) {
  switch (0)
  case 0: S;  /* violates 15.1 - containing compound statement is a function body */
}
Code:
void g (void) {
  switch (1) {
  case 1: switch (0)
          case 0: { ... ; break; } /* complies with 15.1 but not 14.8 or 15.0 */
  }
}