MISRA Discussion Forums
M5-0-15 + range-based for loops - 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.5 Expressions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=134)
+---- Thread: M5-0-15 + range-based for loops (/showthread.php?tid=1599)



M5-0-15 + range-based for loops - cgpzs - 15-02-2022

Hi,

Our static analyzer is reporting M5-0-15 "this is using pointer arithmetic" in the following code:

Code:
int x[3] = {1,2,3};

int z = 0;
for (int y : x)  // M5-0-15 reported here
{
    z += y;
}

Most likely due to the range-based for loop expanding to an iterator-based for loop.

However, we would argue the MISRA rules apply to code written by users, not code generated by the compiler behind the scenes. 

What do you think, is the above code violating M5-0-15?

Thanks!


RE: M5-0-15 + range-based for loops - misra cpp - 23-02-2022

When the MISRA 2008 rule was written range-based for loops had not been defined, so were not considered by the rule. It was adopted by Autosar unmodified.

The view of the committee is that use of range-based for loops with objects of array type should be compliant with this rule.


RE: M5-0-15 + range-based for loops - cgpzs - 23-02-2022

Great, thanks for the clarification!


RE: M5-0-15 + range-based for loops - misra cpp - 13-05-2022

This thread is now closed.

For discussion on related topics, please start a new thread