依赖于 C/C++ 运算符优先级;运算符 '&&' 和 '==' [MISRA 2012 规则 12.1,咨询] | pclint 9050

Dependence placed on C/C++ operator precedence; operators '&&' and '==' [MISRA 2012 Rule 12.1, advisory] | pclint 9050

uint8 config;    
#define ELEM1 1U

void test1(void)
{
    /*Check Bit 0 of Configuration*/
    if((config) == 2 && (config) == 4)
    {
        arr[ELEM1].status[0]          = 0x00;
    }

}

我需要试试吗

1)

if(((config) == 2) && ((config) == 4))
if((Boolean)((config) == 2) && ((config) == 4))

经过上述更改后,我现在观察到错误:

Conditional expression should have essentially Boolean type [MISRA 2012 Rule 14.4, required]

首先,config 不可能同时拥有值 24,现在可以吗?所以你可能打算使用 ||。或者按位 & 也许?

至于警告,如果您在同一个表达式中混合使用多个运算符,MISRA-C 要求您在子表达式周围添加括号。符合 MISRA 的代码将是:

if( (config==2) || (config==4) )

&& ||等运算符的结果本质上是布尔值,所以不需要转换。