MISRA C++:2008 Rule 6-5-2
If loop-counter is not modified by -- or ++, then, within condition, the loop-counter shall only be used as an operand to <=, <, > or >=
Description
Rule Definition
If loop-counter is not modified by -- or ++, then, within condition, the loop-counter shall only be used as an operand to <=, <, > or >=.
Rationale
If the loop counter increments or decrements by more than one per iteration, avoid using
==
or !=
in the loop termination condition. It is
unclear from visual inspection if such loop termination conditions are ever satisfied.
For instance, if a loop counter ctr
increments by more than one per
iteration, such as ctr+=2
, and the termination condition is ctr
== someVal
, the counter might skip the value someVal
,
resulting in a nonterminating loop.
Nonterminating loops can result in developer confusion or unexpected values.
Polyspace Implementation
Polyspace® raises this defect whenever all the following are true:
The loop-counter is not modified by
--
or++
The loop-condition does not contain
<=
,<
,>
or>=
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Statements |
Category: Required |
Version History
Introduced in R2013b