简体   繁体   中英

How to get rid of this Eclipse warning?

I have a function like the one below in 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.

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.

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. I have had a similar warning from Visual Studio 2010, and adding an apparently extraneous return fixed the problem.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM