Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comments are needed for Rule 6-4-2? and should be placed inside the "else" block?
#1
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!
<t></t>
Reply
#2
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!
<t></t>
Reply
#3
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
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)