MISRA Discussion Forums
15.1 and "switch(0) case 0:" - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.15 Switch Statements (https://forum.misra.org.uk/forumdisplay.php?fid=45)
+---- Thread: 15.1 and "switch(0) case 0:" (/showthread.php?tid=873)



15.1 and "switch(0) case 0:" - gs - 10-01-2012

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?


Re: 15.1 and "switch(0) case 0:" - misra-c - 13-01-2012

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 */
  }
}