MISRA Discussion Forums
Comments are needed for Rule 6-4-2? and should be placed inside the "else" block? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forum-18.html)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forum-19.html)
+---- Forum: 6.6 Statements (C++) (https://forum.misra.org.uk/forum-135.html)
+---- Thread: Comments are needed for Rule 6-4-2? and should be placed inside the "else" block? (/thread-1571.html)



Comments are needed for Rule 6-4-2? and should be placed inside the "else" block? - chenzhuowansui - 02-04-2021

Hi,

With regards to Rule 6-4-2
Quote:All if … else if constructs shall be terminated with an
else clause.
we have some different interpretations, so could you kindly help clarify the following questions for us:
1. The rule only talks about "if … else if constructs shall be terminated with an else clause." in the title, and doesn't mention anything about adding necessary comments for the else clause in the title. However, in the rationale part, it indeed mentions that the final else statement should "either take appropriate action or contain a suitable comment", the question is: shall we take the rationale part into account to interpret this rule, more specifically, is the following code snippet compliant?
Code:
if ( x < 0 )
{
log_error ( 3 );
x = 0;
}
else if ( y < 0 )
{
x = 3;
}
else
{
}

2. If the comments are necessary, where shall it be placed: right in the else block? or any places around the else clause, for example, are the following cases compliant?
Code:
if ( x < 0 )
{
log_error ( 3 );
x = 0;
}
else if ( y < 0 )
{
x = 3;
}
// No change in value of x
else
{
}
Code:
if ( x < 0 )
{
log_error ( 3 );
x = 0;
}
else if ( y < 0 )
{
x = 3;
}
else // No change in value of x
{
}
Code:
if ( x < 0 )
{
log_error ( 3 );
x = 0;
}
else if ( y < 0 )
{
x = 3;
}
else
// No change in value of x
{
}
Many thanks in advance!


Re: Comments are needed for Rule 6-4-2? and should be placed inside the "else" block? - chenzhuowansui - 13-04-2021

To make the question more clear, the main concerns we now have are:
1. Should the comments be inside the {}, just as the example in the official doc shows OR can also be outside the {}?
2. If the comments could be outside, where shall the comments be added? Are there any limitations that we shall put the comments around the `else` statement?

please help clarify whether the examples in my first post are compliant or not and also whether the following case is compliant or not:
Code:
if ( x < 0 )
{
    log_error ( 3 );
    x = 0;
}
else if ( y < 0 )
{
    x = 3;
}
else {} // No change in value of x

Many thanks!


RE: Comments are needed for Rule 6-4-2? and should be placed inside the "else" block? - misra cpp - 22-07-2021

For MISRA C++:2008 the rationale is normative. For the current version of MISRA C and the next version of MISRA C++, the rationale isn't normative. That's because these standards may have an 'Amplification' section for each rule, to normatively expand on the headline.

So a final 'else' clause containing at least one statement or comment is required. Our expectation is that tools will be looking for a comment included in the {...} clause of the else