[Bug c++/49508] Bogus "control reaches end of non-void function" warning

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Jan 29 15:04:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49508

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |redi at gcc dot gnu.org

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I'm seeing this frequently too, I was about to create a new PR as follows:

struct Y { };
struct X {
  X(short*);
};
X f() { int i; return &i; }


w.cc: In function ‘X f()’:
w.cc:5:24: error: could not convert ‘& i’ from ‘int*’ to ‘X’
 X f() { int i; return &i; }
                        ^
w.cc:5:27: warning: control reaches end of non-void function [-Wreturn-type]
 X f() { int i; return &i; }
                           ^

The warning is useless, because as soon as the error is fixed the warning will
go away, so it adds nothing but noise to the output.

And it's also confusing because there's a return statement *right* *there*! I
can see it and GCC even printed it out! :-) It only reaches the end because GCC
decides to ignore the return statement that fails in order to continue
compiling the rest of the function.

This doesn't happen in all cases where the return statement has an error, e.g.
if the example above does return i; instead of return &i; there's no warning.


More information about the Gcc-bugs mailing list