MISRA Discussion Forums
Rule 6-5-2: Non-Compliant Example Clarification - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.6 Statements (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=135)
+---- Thread: Rule 6-5-2: Non-Compliant Example Clarification (/showthread.php?tid=1642)



Rule 6-5-2: Non-Compliant Example Clarification - vapdrs - 09-02-2023

I am looking for some clarification on the first non-compliant example for Rule 6-5-2, and the overall definition of a loop-counter, which as part of its definition states it must be,

Quote:an operand to a relational operator in condition


Where relational operator is defined in Rule 4-5-2, 4-5-3, and §5.9 as <, >, <=, and >=.

In the non-compliant example
Code:
for ( i = 1; i != 10; i += 2 ) // Non-compliant

The variable i is clearly considered a loop-counter because the subject of Rule 6-5-2 is a loop-counter. I am confused as to how could i could be considered a loop-counter though, because it is not an operand to a relational operator in the for loop condition.

Moreover, if a loop-counter is defined as necessarily being an operand to a relational operator, then the point of Rule 6-5-2 is called into question. The only thing it could be applied to is when a variable is used as an operand to a relational operator (making it a loop-counter) and another expression in the conditions like,

Code:
for ( i = 1; i != 10 && i > 0 ; i += 2 ) // Non-compliant

Which is helpful for some bad behavior, but will clearly miss some fairly obvious bad behavior, like the original non-compliant example.

Tangentially related the definition of loop-counter contains a note explicitly saying that iterators are also valid as a loop-counter. However, this note would be odd, since typically when using STL iterators in loops equality operators are used and not relational operators. So Rule 6-5-1 would flag the following as non-compliant, even though it is a common idiom.

Code:
for (auto it = container.begin(); it != container.end(); ++it )



RE: Rule 6-5-2: Non-Compliant Example Clarification - misra cpp - 06-11-2023

You are correct. The definition of loop-counter on page 97, should say:
     b) an operand to a relational or equality operator in condition; and...

This has being addressed for the next release (due imminently)