MISRA Discussion Forums

Full Version: M5-0-15 + range-based for loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
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.
Great, thanks for the clarification!
This thread is now closed.

For discussion on related topics, please start a new thread