gpt4 book ai didi

c - 如何从 GCC-6 中捕获类似 `-Wduplicated-cond` 的重复项

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:46 25 4
gpt4 key购买 nike

正如标题所说,对于那些已经知道以 GCC-6 开头的人你可以在 if 中找到重复项使用此标志的语句 -Wduplicated-cond像这样:

#include <stdio.h>

int main(void){
int a = 5;

if( a == 5){
printf("First condition is True, A = %d\n", a);
}else if( a == 5 ){
printf("Second condition is True, A = %d\n", a);
}
}

输出将是:

program.c:8:17: warning: duplicated ‘if’ condition [-Wduplicated-cond]
}else if( a == 5 ){
~~^~~~
program.c:6:11: note: previously used here
if( a == 5){
~~^~~~

现在我知道以下内容:

else if( (a > 4) && (a < 6) )

不一样

else if( a == 5 )

但如果 a == 5,我会检查相同的条件.

我的问题是,是否有机会捕捉(避免)这种重复?

最佳答案

-Wduplicated-cond 确实捕获了这种情况。您必须明确启用它。

main.cpp: In function 'int main()':

main.cpp:8:11: warning: duplicated 'if' condition [-Wduplicated-cond]

 }else if( a == 5 ){
^~

main.cpp:6:5: note: previously used here

 if( a == 5){

原来是enabled by -Wall但不再是因为 PR67819 :

Given that the new warning currently breaks bootstrap [*] and I see no simple way how to resolve PR67819 derived out of the bootstrap failure, I'm moving -Wduplicated-cond out of -Wall and skipping one of the tests for the time being. I'm also reverting the hack in genemit.c.

关于c - 如何从 GCC-6 中捕获类似 `-Wduplicated-cond` 的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37283128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com