xcode return 语句永远不会被执行警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26254857/
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 05:48:40  来源:igfitidea点击:

Return statement will never be executed warning

iosxcodecompiler-warnings

提问by Injectios

I've recently come across with double returnstatement (one of them was added by mistake) in our project and wondering why compilerdoesn't show a warning for that?!

我最近return在我们的项目中遇到了双重声明(其中一个被错误添加),想知道为什么compiler不显示警告?!

Ok, I added -Wunreachable-codeto other warning flags, but still no luck.

好的,我添加-Wunreachable-code了其他警告标志,但仍然没有运气。

Got warning - with a code to execute after returnstatement:

收到警告 - 在return语句后执行代码:

enter image description here

在此处输入图片说明

Didn't get warning but still second returnstatement will never be executed. enter image description here

没有收到警告,但仍然return永远不会执行第二条语句。 在此处输入图片说明

Even if I add something like this, still no warning

即使我添加了这样的东西,仍然没有警告

enter image description here

在此处输入图片说明

Is there extra warning flag for that, or compiler isn't smart enough?

是否有额外的警告标志,或者编译器不够智能?

采纳答案by fluidsonic

Good catch!

接得好!

-Wunreachable-codedoes not report a warning and there is no other warning flag which would do.
Not even the Static Analyzer catches this mistake!

-Wunreachable-code不报告警告,并且没有其他警告标志可以这样做。
甚至静态分析器都没有发现这个错误!

(Tested with XCode 6.1 GM 2)

(使用 XCode 6.1 GM 2 测试)

回答by Albert Renshaw

Wrap your code in pragma flags that will suppress that warning between the push and pop

将您的代码包装在 pragma 标志中,这将抑制 push 和 pop 之间的警告

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunreachable-code"
//Your code goes here
#pragma GCC diagnostic pop

回答by atulkhatri

Use -Wno-unreachable-codefor your file in Build Phases.

使用-Wno-unreachable-code在构建阶段您的文件。

回答by Shaun Kelly

Try this...

尝试这个...

-(NSString*) makeSomeStringForMe {

    NSString *someString = @"some string";

    return someString;

}