[英]How to get rid of this Eclipse warning?
I have a function like the one below in Eclipse. 我有一个类似Eclipse中下面的函数。 However, because there is no return outside the while loop, Eclipse assumes that I may have a logical error (probably it can't see that the condition of the while is always true. 但是,由于在while循环之外没有返回值,因此Eclipse假定我可能存在逻辑错误(可能无法看到while条件始终为真。
Switching to a do-while won't help. 切换为“做时制”无济于事。 Is there any way that I can get rid of this warning programmatically, ie, not by modifying settings of Eclipse IDE. 有什么方法可以通过编程方式消除此警告,即不能通过修改Eclipse IDE的设置来解决。
int foo(...)
{
while (1) {
...
if(...)
return -1;
...
if(...)
return 0;
}
}
I compiled the same function in the terminal and had no warning. 我在终端中编译了相同的函数,没有警告。
Add a return
statement before the closing brace, even if you expect it never to be reached. 即使您希望永远不会达到,也请在右花括号前添加return
语句。 I have had a similar warning from Visual Studio 2010, and adding an apparently extraneous return
fixed the problem. 我从Visual Studio 2010得到了类似的警告,并添加了一个明显无关的return
解决此问题。
Here's the fix, use a return value variable: 解决方法是使用返回值变量:
int foo(...)
{
int return_value = 0;
while (true)
{
//...
if (...)
{
return_value = -1;
break; // out of the while loop
}
if (...)
{
return_value = 0;
break;
}
}
return return_value;
}
One entry point, one exit point. 一个入口点,一个出口点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.