<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[MISRA Discussion Forums - 6.15 Switch Statements]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Thu, 05 Nov 2026 19:06:11 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[15.2 and 'break's inside if-else]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=913</link>
			<pubDate>Thu, 06 Sep 2012 19:26:44 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=64">gs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=913</guid>
			<description><![CDATA[Suppose I have the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
switch( x )<br />
{<br />
case 1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;if( y )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
default:<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
}<br />
...</code></div></div><br />
Does this code violate rule #15.2?]]></description>
			<content:encoded><![CDATA[Suppose I have the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
switch( x )<br />
{<br />
case 1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;if( y )<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
default:<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
}<br />
...</code></div></div><br />
Does this code violate rule #15.2?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Unreachable break in a case statement]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=906</link>
			<pubDate>Wed, 04 Jul 2012 08:14:21 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=2141">danielb</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=906</guid>
			<description><![CDATA[I have recently come across two different rule interpretations of the following code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch(cond)<br />
{<br />
&nbsp;&nbsp;case 0:<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;/* A deliberate infinite loop. */<br />
&nbsp;&nbsp;&nbsp;&nbsp;for ( ; ; ) { }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;/* Unreachable break statement. */<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;[ â€¦ ]<br />
}</code></div></div>One MISRA checker insists on the break statement referring to rule 15.2, while the other insists that the break statement is unreachable code according to rule 14.1.<br />
<br />
Both rules are required, however, both can not be enforced in this case. Which rule supersedes the other?]]></description>
			<content:encoded><![CDATA[I have recently come across two different rule interpretations of the following code:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch(cond)<br />
{<br />
&nbsp;&nbsp;case 0:<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;/* A deliberate infinite loop. */<br />
&nbsp;&nbsp;&nbsp;&nbsp;for ( ; ; ) { }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;/* Unreachable break statement. */<br />
&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;[ â€¦ ]<br />
}</code></div></div>One MISRA checker insists on the break statement referring to rule 15.2, while the other insists that the break statement is unreachable code according to rule 14.1.<br />
<br />
Both rules are required, however, both can not be enforced in this case. Which rule supersedes the other?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[15.1 and "switch(0) case 0:"]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=873</link>
			<pubDate>Tue, 10 Jan 2012 15:01:53 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=64">gs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=873</guid>
			<description><![CDATA[Hi,<br />
    Given the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
switch(0)<br />
case 0:<br />
...</code></div></div>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?]]></description>
			<content:encoded><![CDATA[Hi,<br />
    Given the following code:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>...<br />
switch(0)<br />
case 0:<br />
...</code></div></div>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?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 15.4 - no boolean switch expressions]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=821</link>
			<pubDate>Fri, 08 Apr 2011 13:02:18 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=1515">eddieh</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=821</guid>
			<description><![CDATA[I don't believe my compiler's interpretation of rule 15.4 is consistent with the Standard's...<br />
<br />
The Standard says:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch( x == 0 )<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;...</code></div></div><br />
is non-compliant, which seems reasonable. However, my compiler complains about:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum_type x;<br />
<br />
switch( x )<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;case ENUM_A :<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do_stuff;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;default :<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* No action */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
}</code></div></div><br />
Because 'x' is an enumerated type, I like to make it clear that we do_stuff for a particular subset of possible values.<br />
<br />
What is intended interpretation of this rule? There is no explanation in the Standard.<br />
<br />
Many thanks in advance for any comments.<br />
<br />
Cheers,<br />
Eddie.]]></description>
			<content:encoded><![CDATA[I don't believe my compiler's interpretation of rule 15.4 is consistent with the Standard's...<br />
<br />
The Standard says:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch( x == 0 )<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;...</code></div></div><br />
is non-compliant, which seems reasonable. However, my compiler complains about:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>enum_type x;<br />
<br />
switch( x )<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;case ENUM_A :<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do_stuff;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;default :<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* No action */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
}</code></div></div><br />
Because 'x' is an enumerated type, I like to make it clear that we do_stuff for a particular subset of possible values.<br />
<br />
What is intended interpretation of this rule? There is no explanation in the Standard.<br />
<br />
Many thanks in advance for any comments.<br />
<br />
Cheers,<br />
Eddie.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Implicit type conversions in switch statements]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=787</link>
			<pubDate>Tue, 02 Nov 2010 17:48:28 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=159">dcrocker</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=787</guid>
			<description><![CDATA[I've a question concerning the following example:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int8 x;<br />
...<br />
switch(x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 0:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* valid */<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 255:&nbsp;&nbsp;&nbsp;&nbsp;/* valid? */<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 1L:&nbsp;&nbsp;&nbsp;&nbsp; /* invalid, implicit cast from long to int */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br />
}</code></div></div><br />
I would expect any good static checker to report that x can never equal 255 here. But is 'case 255:' actually a MISRA 2004 rule violation? The C standard says that the switch expression x gets promoted to 'int' in this case, so the type of the case label 255 does match the type of the promoted switch expression. I can't find any MISRA rule that says that we should consider the underlying type (instead of the promoted type) of the switch variable when deciding whether a case label is allowed or not. If "case 255:" was the only case label on some code, then rule 14.1 (no unreachable code) would apply; but in this example, there is no unreachable code.]]></description>
			<content:encoded><![CDATA[I've a question concerning the following example:<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>int8 x;<br />
...<br />
switch(x)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 0:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* valid */<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 255:&nbsp;&nbsp;&nbsp;&nbsp;/* valid? */<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 1L:&nbsp;&nbsp;&nbsp;&nbsp; /* invalid, implicit cast from long to int */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br />
}</code></div></div><br />
I would expect any good static checker to report that x can never equal 255 here. But is 'case 255:' actually a MISRA 2004 rule violation? The C standard says that the switch expression x gets promoted to 'int' in this case, so the type of the case label 255 does match the type of the promoted switch expression. I can't find any MISRA rule that says that we should consider the underlying type (instead of the promoted type) of the switch variable when deciding whether a case label is allowed or not. If "case 255:" was the only case label on some code, then rule 14.1 (no unreachable code) would apply; but in this example, there is no unreachable code.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 15.2: MISRA warning is not generated]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=776</link>
			<pubDate>Mon, 27 Sep 2010 10:47:39 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=0">vastrad</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=776</guid>
			<description><![CDATA[Hi All,<br />
<br />
The Rule 15.2 says:<br />
"An unconditional <span style="font-style: italic;" class="mycode_i">break </span>statement shall terminate every non-empty switch clause"<br />
i.e. The last statement in every switch clause shall be a <span style="font-style: italic;" class="mycode_i">break </span>statement, or if the switch clause is a compound statement, then the last statement in the compound statement shall be a <span style="font-style: italic;" class="mycode_i">break </span>statement.<br />
<br />
I understand this as:<br />
"In a switch clause, whatever be the path traversed by the code, there shall be one break statement at the end of every path"<br />
<br />
In a project I have the following code for which I am not getting any MISRA warning (for case 1). If my understanding is incorrect, then somebody please correct me.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch (variable_1)<br />
{<br />
&nbsp;&nbsp;case 0: <br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// some code here;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;case 1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// some code here;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (variable_2 == 0)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;default:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
}</code></div></div><br />
Best Regards,<br />
Vinay Vastrad]]></description>
			<content:encoded><![CDATA[Hi All,<br />
<br />
The Rule 15.2 says:<br />
"An unconditional <span style="font-style: italic;" class="mycode_i">break </span>statement shall terminate every non-empty switch clause"<br />
i.e. The last statement in every switch clause shall be a <span style="font-style: italic;" class="mycode_i">break </span>statement, or if the switch clause is a compound statement, then the last statement in the compound statement shall be a <span style="font-style: italic;" class="mycode_i">break </span>statement.<br />
<br />
I understand this as:<br />
"In a switch clause, whatever be the path traversed by the code, there shall be one break statement at the end of every path"<br />
<br />
In a project I have the following code for which I am not getting any MISRA warning (for case 1). If my understanding is incorrect, then somebody please correct me.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch (variable_1)<br />
{<br />
&nbsp;&nbsp;case 0: <br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// some code here;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;case 1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// some code here;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (variable_2 == 0)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;default:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
}</code></div></div><br />
Best Regards,<br />
Vinay Vastrad]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 15.0 - Required or Adviosry?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=684</link>
			<pubDate>Sat, 24 Oct 2009 18:32:34 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=745">William Forbes</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=684</guid>
			<description><![CDATA[TC1 introduced the concept of rule 15.0.<br />
However it does not explicitly state if this rule is "Required" or "Advisory"<br />
Is rule 15.0 "required" or "advisory"?]]></description>
			<content:encoded><![CDATA[TC1 introduced the concept of rule 15.0.<br />
However it does not explicitly state if this rule is "Required" or "Advisory"<br />
Is rule 15.0 "required" or "advisory"?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Combining case and default?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=675</link>
			<pubDate>Fri, 07 Aug 2009 16:21:36 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=350">exoson</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=675</guid>
			<description><![CDATA[I would be interested in comments on the following simplified fragment.<br />
<br />
    switch (err) { <br />
        case 0:<br />
     ...<br />
     break;<br />
<br />
        case 1:<br />
        case 2:<br />
        case 3:<br />
        default:<br />
             ...<br />
             break; <br />
    }<br />
 <br />
I am seeing code examples like this where multiple cases are being combined with default.  Most compiler will simply optimize this out to a simple test for case 0.  The programmers are using this as a way to identify the results they might expect.  I think having the other cases in a comment would serve this as well.  MISRA C does not seem to mention this.<br />
From a test perspective I feel it is necessary to test all declared cases. <br />
Can anyone offer a comment on this?<br />
<br />
Scott<br />
Validated Software]]></description>
			<content:encoded><![CDATA[I would be interested in comments on the following simplified fragment.<br />
<br />
    switch (err) { <br />
        case 0:<br />
     ...<br />
     break;<br />
<br />
        case 1:<br />
        case 2:<br />
        case 3:<br />
        default:<br />
             ...<br />
             break; <br />
    }<br />
 <br />
I am seeing code examples like this where multiple cases are being combined with default.  Most compiler will simply optimize this out to a simple test for case 0.  The programmers are using this as a way to identify the results they might expect.  I think having the other cases in a comment would serve this as well.  MISRA C does not seem to mention this.<br />
From a test perspective I feel it is necessary to test all declared cases. <br />
Can anyone offer a comment on this?<br />
<br />
Scott<br />
Validated Software]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 15.3 - question over wording of rule]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=643</link>
			<pubDate>Wed, 01 Apr 2009 23:53:09 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=80">CarlinB</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=643</guid>
			<description><![CDATA[Hello,<br />
<br />
Rule 15.3 states that "The final clause of a <span style="font-style: italic;" class="mycode_i">switch</span> statement shall be the <span style="font-style: italic;" class="mycode_i">default</span> clause."<br />
<br />
Is the point of the rule to dictate <span style="font-style: italic;" class="mycode_i">where</span> the default clause should be located or only to make sure that every switch statement <span style="font-style: italic;" class="mycode_i">includes</span> a default clause?<br />
<br />
I would agree that every switch statement should have a default clause, but I am not clear on why the rule would dictate where this clause should be located.<br />
<br />
The rule description clearly states this is a "defensive programming" action.  Being such, it makes sense that many times the end of your clause list is the appropriate place for the default clause (there to catch any unhandled case).  However, there are times where the desired default behavior is the same as one of the case clauses.  Furthermore, the arrangement of the case clauses is often done in some logical order (i.e. progression through a menu).  It seems to me that reordering the list of cases just to satisfy this rule is unnecessary, particularly when it breaks the logical flow of the cases.<br />
<br />
As an example, here is a switch statement that might be used to determine the appearance of a list of items displayed to a user.  For this example, the desired <span style="font-style: italic;" class="mycode_i">default</span> behavior is to display the list same as if "case 0" was the selection.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch(itemSelected)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;default:<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 0:&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;/* show list with first item highlighted */ } break;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;/* show list with second item highlighted */ } break;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;case n:<br />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;/* show list with n-th item highlighted */ } break;<br />
}</code></div></div><br />
Logically, I feel it makes more sense to locate the default clause with the first case clause rather than moving "case 0" to the end of the clause list or repeating the contents of the "case 0" clause in the default clause.<br />
<br />
I realize that in practice a deviation can be recorded for why this rule is not followed.  But, if the real intent of the rule is to ensure a default clause is always provided then it seems the rule should be reworded to emphasize the need for a default clause and leave the location of the default clause as advisory.  However, if the rule really is meant to dictate where the default clause is located, I would appreciate if someone could provide an explanation of why this location is important enough to be a "required" rule.]]></description>
			<content:encoded><![CDATA[Hello,<br />
<br />
Rule 15.3 states that "The final clause of a <span style="font-style: italic;" class="mycode_i">switch</span> statement shall be the <span style="font-style: italic;" class="mycode_i">default</span> clause."<br />
<br />
Is the point of the rule to dictate <span style="font-style: italic;" class="mycode_i">where</span> the default clause should be located or only to make sure that every switch statement <span style="font-style: italic;" class="mycode_i">includes</span> a default clause?<br />
<br />
I would agree that every switch statement should have a default clause, but I am not clear on why the rule would dictate where this clause should be located.<br />
<br />
The rule description clearly states this is a "defensive programming" action.  Being such, it makes sense that many times the end of your clause list is the appropriate place for the default clause (there to catch any unhandled case).  However, there are times where the desired default behavior is the same as one of the case clauses.  Furthermore, the arrangement of the case clauses is often done in some logical order (i.e. progression through a menu).  It seems to me that reordering the list of cases just to satisfy this rule is unnecessary, particularly when it breaks the logical flow of the cases.<br />
<br />
As an example, here is a switch statement that might be used to determine the appearance of a list of items displayed to a user.  For this example, the desired <span style="font-style: italic;" class="mycode_i">default</span> behavior is to display the list same as if "case 0" was the selection.<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>switch(itemSelected)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;default:<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 0:&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;/* show list with first item highlighted */ } break;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;case 1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;/* show list with second item highlighted */ } break;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;case n:<br />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;/* show list with n-th item highlighted */ } break;<br />
}</code></div></div><br />
Logically, I feel it makes more sense to locate the default clause with the first case clause rather than moving "case 0" to the end of the clause list or repeating the contents of the "case 0" clause in the default clause.<br />
<br />
I realize that in practice a deviation can be recorded for why this rule is not followed.  But, if the real intent of the rule is to ensure a default clause is always provided then it seems the rule should be reworded to emphasize the need for a default clause and leave the location of the default clause as advisory.  However, if the rule really is meant to dictate where the default clause is located, I would appreciate if someone could provide an explanation of why this location is important enough to be a "required" rule.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Switches, default, and enums]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=362</link>
			<pubDate>Thu, 24 May 2007 12:38:54 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=289">ChrisNelson</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=362</guid>
			<description><![CDATA[Rule 15.3 requires every switch to end with a default clause.  While this is usually a good idea, it undermines the technique of using enums for switch control and having the compiler catch unused or unhandled values.  For example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef enum {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;red,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;green,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blue<br />
&nbsp;&nbsp; } color_t;<br />
<br />
&nbsp;&nbsp; void f(color_t color) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (color) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case red:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* do something */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case green:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* do something */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />
&nbsp;&nbsp; }</code></div></div>Many compilers and certainly most lints will generate a warning when compiling the function f noting that not all enum values are handled.  When the typedef and the switch are in different files, this can be quite helpful.  When the enum is handled by switches in multiple source files, this structure allows the compiler to catch unmodified switches when a new enum value is added.  If there was a default case in this switch, the compiler would not complain.<br />
<br />
With that in mind, I suggest that rule 15.3 be revised by adding, \"except where the switch expression is an enum\" to the end.  I might also add a advisory rule encouraging switch expressions to be enums.<br />
<br />
Any thoughts?]]></description>
			<content:encoded><![CDATA[Rule 15.3 requires every switch to end with a default clause.  While this is usually a good idea, it undermines the technique of using enums for switch control and having the compiler catch unused or unhandled values.  For example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>typedef enum {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;red,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;green,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blue<br />
&nbsp;&nbsp; } color_t;<br />
<br />
&nbsp;&nbsp; void f(color_t color) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (color) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case red:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* do something */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case green:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* do something */<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />
&nbsp;&nbsp; }</code></div></div>Many compilers and certainly most lints will generate a warning when compiling the function f noting that not all enum values are handled.  When the typedef and the switch are in different files, this can be quite helpful.  When the enum is handled by switches in multiple source files, this structure allows the compiler to catch unmodified switches when a new enum value is added.  If there was a default case in this switch, the compiler would not complain.<br />
<br />
With that in mind, I suggest that rule 15.3 be revised by adding, \"except where the switch expression is an enum\" to the end.  I might also add a advisory rule encouraging switch expressions to be enums.<br />
<br />
Any thoughts?]]></content:encoded>
		</item>
	</channel>
</rss>