-1

I have the following code sample:

void some_function()
{
    int ret = do_something();

    if(ret == SOME_ERROR)
        return;
}

Afaik the last if is useless as the function returns no matter what value ret has. I'd expect a warning or at least an info by the compiler (I use GCC 6). I also tried to enable all warnings from this thread but still nothing. Is there any difference between returning with a return statement and returning at the end of a void function or does it get optimized anyways (but I still think a warning would be useful then)?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
jklmnn
  • 481
  • 1
  • 5
  • 11
  • 1
    You can [test](https://godbolt.org/g/IbkkIE) what code gets generated for various compilers and flags. – nwp May 15 '17 at 11:17
  • You can always overload the `operator==` with some side-effects in order to understand the importance of keeping this if condition. Then all that remains is an optional `return` statement - not a reason to raise warnings. – grek40 May 15 '17 at 11:26
  • 1
    There's no problem with this code. The compiler shouldn't warn for all sub-optimal code... – M.M May 15 '17 at 11:29
  • @grek40 I assume OP refers to the case of `==` not being overloaded – M.M May 15 '17 at 11:30
  • @M.M Yes, but since it is possible to overload it, the compiler would need to make some potentially confusing distinctions about when to display a warning for the code in question. So the possibility of this code having a side effect should be reason enough, even when there is no actual side effect. – grek40 May 15 '17 at 11:35
  • imho the code would be problematic if there was no `if (x) return;` because otherwise a innocent coder could add a line in the end and completely change the workings of the method without even being aware of it. If code was only about efficiency you could write it in assembler, but in reality code is to express what it is supposed to do and in this sense the code is perfeclty fine as is – 463035818_is_not_an_ai May 15 '17 at 11:36

3 Answers3

3

Compiler warnings should maintain the balance between false positives and false negatives.

Too many warnings - and you'll spend a lot of your time walking through them without any actual improvement (or, more realistically, you will disable them altogether).

It is hard enough to imagine a situation where such a warning shows a real problem with the code, and not just a minor stylistic issue.

Consider, for example, -Wunused. It is pretty often indicates that you misspelled some variable name, or forgot this:

float x = ...;
float y = ...;

return atan2(y, y);
//x is never used, probably an error

Your example could be a diagnostic for a static analysis tool, but too minor to be useful as a general compiler warning.

0

A void function can perfectly return.

That's why you don't see no error/warning. From my answer, even I use some useful flags, I will get nothing:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -pedantic -Wall -Wextra -Wconversion main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ 

You want to do this when you want to terminate the function, but you do not need to return anything at all.


Try to do return 5; and you will get an error, of course.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I see that the function does return without problems and I understand why. But the last `if` statement is completely useless. It behaves like a `if-else` block with identical statments inside the blocks which in my eyes should cause a warning about useless code. – jklmnn May 15 '17 at 11:22
  • 1
    @jklmnn imho your premise "last if statement completely useless" is wrong. Suppose you wanted to add more code at the end of the function at some point in the future, then you better dont remove that `if` – 463035818_is_not_an_ai May 15 '17 at 11:41
0

You have written fully legitimate code. So there is no error from the compiler.

!If! you enable optimizing it will be optimized away. You can see it here: https://godbolt.org/g/pUr7eP

Maybe this will be flagged by some static analyzer.

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • the question is about warnings not errors. A fully legitimate code may cause the compiler to produce lots of warnings – 463035818_is_not_an_ai May 15 '17 at 11:40
  • For me usually warnings signal errors. This code executes and finishes in all it's paths. That the code has seprate ways for exiting this function signifies for me that the function is legitimate. – Robert Andrzejuk May 15 '17 at 11:49