C中的“条件中使用的表达式总是产生相同的结果”警告

问题描述

我想知道 Expression Used in the condition always yields the same result Misra 警告的确切含义。这是我正在使用的一段代码

#define bool_new_timer_val    (Time_cnt < 4sec)

if( (bool_new_timer_val == True) ||
    (mystruct.bool_u8_status1 != 0x0) ||
    ((mystruct.bool_u8_status1 == 0x0) && (bool_old_timer == True)) ||
    (mystruct.bool_u8_status2 == True) ||
    (mystruct.bool_u8_status3 == True))
{
   // update the logic
}

在运行 Misra 时,我收到警告为 Expression 'mystruct.bool_u8_status1' used in the condition always yields the same result。 问题指向行 ((mystruct.bool_u8_status1 == 0x0) && (bool_old_timer == True))

我想知道这个警告是什么意思。我的猜测是值 mystruct.bool_u8_status1 始终设置为 0x0。这是正确的理解吗?有什么建议吗?

解决方法

查看您的代码:

(mystruct.bool_u8_status1 != 0x0) ||
((mystruct.bool_u8_status1 == 0x0) && (bool_old_timer == True)) ||

如果 mystruct.bool_u8_status1 != 0x0 的计算结果为 TRUE,则不计算第二行。

如果 mystruct.bool_u8_status1 != 0x0 的计算结果为 FALSE,则代码等效于

(FALSE) ||
((TRUE) && (bool_old_timer == True)) ||

到底是什么

((bool_old_timer == True)) ||

所以尝试编写如下代码:

(mystruct.bool_u8_status1 != 0x0) ||
(bool_old_timer == True) ||