MISRA C:2012 Rule 14.3

232 views Asked by At

I am confused how to resolve MISRA 2012 14.3:

The condition in if, for, while, do-while, or ternary operator will never be true.

I am having code like this:

uint8 Func1(int a)
{
#if (SOME_MARCO == ON)
    return (a == somevalue ) ? 0u : 1u;
#else
    return 0u;
#endif
}

void main()
{
    if (Func1 == ou)
    {
        //do something
    }
}

If the MARCO is not defined, then I get a warning at line if (Func1 == ou), conditional expression always true.

I do not know how to resolve this issue since the marco has real meaning, and I can't remove it. I wonder what should I do to resolve warning like this?

/***************************************************************/

edit: Sorry about the typo and pseudo code.

The real code is :

uint8_t ucDetectionCheckAccStatus( void )
{
#if ( ACC_DETECTION == STD_ON )
    if ( Dio_ReadChannel( detectionDIO_CHANNEL_ACC ) == DETECTION_LEVEL_ACC_NORMAL )
    {
        return detectionSTATE_NORMAL;
    }
    else
    {
        return detectionSTATE_ABNORMAL;
    }
#else
    return detectionSTATE_NORMAL;
#endif
}

bool xDetectionCheckForNormal(void)
{
...
    if( ucDetectionCheckAccStatus( ) ==(uint8_t)detectionSTATE_ABNORMAL )
        {
            return false;
        }
...
}

And thanks for all the answers, I think I can try to do the conditional compile in xDetectionCheckForNormal to avoid this? Or there is a better way?

1

There are 1 answers

4
chqrlie On

The warning is not about the constant value returned by Func1, but about the fact that you compare Func1 with ou, (presumably 0u) and this test is always false because Func1 is a function so its address cannot be null.

You forgot the () for the function to be called instead of its address to be used in the expression.

Also note that the prototype for main without arguments should be int main(void).

Here is a modified version:

uint8 Func1(int a)
{
#if (SOME_MACRO == ON)
    return (a == somevalue ) ? 0u : 1u;
#else
    return 0u;
#endif
}

int main(void)
{
    if (Func1(0) == 0u)
    {
        //do something
    }
}