如何在新的 xCode 中禁用“添加显式大括号以避免悬挂其他”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12023653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:16:06  来源:igfitidea点击:

How do I disable "Add explicit braces to avoid dangling else" in new xCode?

xcodesyntaxwarningscompiler-warnings

提问by David

Just updated to latest xCode 4.41 and now I'm getting a ton of warnings about perfectly fine code (see subject line). These warnings are in 3rd party source code that I'm using, I don't feel like going into that code and editing (I'd have to do it again next time I update that code) so I'd rather just disable that particular warning.

刚刚更新到最新的 xCode 4.41,现在我收到了大量关于完美代码的警告(请参阅主题行)。这些警告在我使用的 3rd 方源代码中,我不想进入该代码并进行编辑(下次更新该代码时我必须再次这样做)所以我宁愿禁用它特别警告。

By the way, I'm using xcode4.41 but apparently I don't have a good enough reputation to be able to create a new tag (interesting though that 4.41 is not already in there)

顺便说一句,我使用的是 xcode4.41,但显然我没有足够好的声誉来创建新标签(有趣的是 4.41 还没有在那里)

回答by PatchyFog

This worked for me:

这对我有用:

#ifdef __llvm__
#pragma GCC diagnostic ignored "-Wdangling-else"
#endif

回答by Debaprio B

Its not a bug. Apple LLVM warns you in cases where you have a nested if-else if-else block. Look at the code below:

它不是一个错误。如果您有嵌套的 if-else if-else 块,Apple LLVM 会警告您。看看下面的代码:

if (Condition1)
    if (Condition2)
        return Statement2;
    else if (Condition3)
        Statement3;
    else
        Statement4;

By looking at the above code, its confusing for the parser to understand to which 'if' the 'else' is connected with? Remember that its not mandatory to have a else for every else-if, so it can be very likely that the else statement might be linked to the if (Condition1) rather than if (Condition2).

通过查看上面的代码,解析器很难理解“else”与哪个“if”相关?请记住,对于每个 else-if 都不是强制性的,因此很可能 else 语句可能链接到 if (Condition1) 而不是 if (Condition2)。

Apple llvm Compiler is smart enough to not make this folly but warns the user to reconsider the code to make sure that the user didn't want it the other way.

Apple llvm Compiler 足够聪明,不会做出这种愚蠢的行为,但会警告用户重新考虑代码以确保用户不希望它以其他方式出现。

Generally the warning can be resolved by adding braces to the all the top level if statements. In the example above, the warning would go away by adding braces to if (Condition1). Check the more readable (warning less) code below:

通常可以通过在所有顶级 if 语句中添加大括号来解决警告。在上面的示例中,警告将通过向 if (Condition1) 添加大括号而消失。检查下面更具可读性(警告更少)的代码:

if (Condition1)
{
    if (Condition2)
        return Statement2;
    else if (Condition3)
        Statement3;
    else
        Statement4;
}

回答by David

OK --- there's an option under Apple LLVM compiler 4.0 - Warnings called

好的 --- Apple LLVM 编译器 4.0 下有一个选项 - 称为警告

Missing Braces and Parentheses

缺少大括号和圆括号

Setting that to NO gets rid of this warning.

将其设置为 NO 可消除此警告。

Unfortunately, there doesn't seem to be a way to JUST get rid of the braces warning.

不幸的是,似乎没有办法摆脱大括号警告。

Apple, you're starting to impose too much on how I develop. Stop!

Apple,你开始对我的开发方式施加过多的影响。停止!

回答by Gabriel

An "else" that was meant for the "other" 'if' is a really hard-to-find bug! I know from personal experience. So, the "dangling else" warning can be really helpful.

用于“其他”“如果”的“else”是一个很难找到的错误!我从个人经历中知道。所以,“dangling else”警告真的很有帮助。

回答by Kostiantyn Ponomarenko

You can also use -Wno-dangling-elseflag.

您也可以使用-Wno-dangling-else标志。

http://clang.llvm.org/docs/UsersManual.html#options-to-control-error-and-warning-messages

http://clang.llvm.org/docs/UsersManual.html#options-to-control-error-and-warning-messages

-Wno-foo
        Disable warning “foo”.

-Wno-foo
        禁用警告“foo”。

clang++ -Wno-dangling-else test.cpp