“Expression Used in the condition always yields the same result”(条件中使用的表达式总是产生相同的结果)“C 语言中的警告 [已关闭]

"Expression Used in the condition always yields the same result" warning in C [closed]

提问人:user2986042 提问时间:7/21/2021 最后编辑:user2986042 更新时间:7/21/2021 访问量:451

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将有助于其他人回答这个问题。

2年前关闭。

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

#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_status10x0

c if 语句 表达式 布尔逻辑 misra

评论

0赞 pmg 7/21/2021
什么?涉及哪些类型?True
0赞 the busybee 7/21/2021
Misra 检查器能否通过在此之前的东西推断出始终具有相同的值?-- Misra 消息是否包含行号?如果是这样,您可以说出这两种表达方式中的哪一个。mystruct.bool_u8_status1if
0赞 user2986042 7/21/2021
True 是一个布尔值。
0赞 user2986042 7/21/2021
True 是一个布尔值,它将检查该值是 True/False。还更新了行号。
4赞 Yunnosch 7/21/2021
我感到困惑,因为我的印象是它永远无法成功编译。请提供一个最小的可重复示例4sec

答:

1赞 Support Ukraine 7/21/2021 #1

查看您的代码:

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

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

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

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

归根结底,这很简单

((bool_old_timer == True)) ||

因此,请尝试编写如下代码:

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