![]() |
Rule 15.4 - no boolean switch expressions - 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: Rule 15.4 - no boolean switch expressions (/showthread.php?tid=821) |
Rule 15.4 - no boolean switch expressions - eddieh - 08-04-2011 I don't believe my compiler's interpretation of rule 15.4 is consistent with the Standard's... The Standard says: Code: switch( x == 0 ) is non-compliant, which seems reasonable. However, my compiler complains about: Code: enum_type x; Because 'x' is an enumerated type, I like to make it clear that we do_stuff for a particular subset of possible values. What is intended interpretation of this rule? There is no explanation in the Standard. Many thanks in advance for any comments. Cheers, Eddie. Re: Rule 15.4 - no boolean switch expressions - misra-c - 11-04-2011 Rule 15.4 relates to the effective type of the controlling expression of the switch statement. The intent is to identify a switch statement that can have only two possible cases. Such switch statements are unusual and may be indicative of a programming error. The same effect could be achieved more clearly using if ... else. Since an enumerated type is not effectively Boolean, there is no violation of Rule 15.4 in this case. The body of the switch statement contains at least one case clause so there is no violation of Rule 15.5 either. |