MISRA 违反规则 15.5:检测到多个出口点。函数应该在函数末尾有一个单一的退出点

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

我正在尝试从我的代码中删除规则 15.5。这基本上是因为函数中有多个return。

代码如下:

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

我尝试使用一个存储 return 值的临时变量,并在最后 returning 这个变量。但是那没有用。

有什么建议吗?

你需要存储一个临时变量来打破循环

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;
}