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?
The warning is not about the constant value returned by
Func1, but about the fact that you compareFunc1withou, (presumably0u) and this test is always false becauseFunc1is 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
mainwithout arguments should beint main(void).Here is a modified version: