gsamaras
gsamaras

Reputation: 73366

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, i.e., 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.

Upvotes: 0

Views: 77

Answers (2)

Thomas Matthews
Thomas Matthews

Reputation: 57678

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.

Upvotes: 0

Logicrat
Logicrat

Reputation: 4468

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.

Upvotes: 2

Related Questions