Search code examples
cmisra

MISRA Violation Rule 15.5 : Multiple points of exit detected. Function should have a single point of exit at the end of the function


I am trying to get rid of rule 15.5 from my code. This is basically because of multiple returns in the function.

Code looks like:

int32_t
do_test(int32_t array[])
{  
    for(int32_t i=0; i < VAL; i++)
    {
      if(array[i] == 2) {
        return 1;
      } 
    }
    return 0;
}

I have tried with a temp variable which store the return value and returning this variable at the end. But that didnt work.

Any suggestions ?


Solution

  • You need to store a temp variable and to break the loop

    int32_t
    do_test(int32_t array[])
    {
        int32_t result = 0;  
        for(int32_t i=0; i < VAL; i++)
        {
          if(array[i] == 2) {
            result = 1;
            break; // !!
          } 
        }
        return result;
    }