Red Hat Bugzilla – Bug 24066
No warning if control reaches end of non-void function
Last modified: 2007-04-18 12:30:42 EDT
When compiling the following code with gcc-2.96-70 no warning will be
generated although it should be (older g++ do it):
----- warn.cc -------
$ g++ warn.cc -Wall
$ egcs++ warn.cc -Wall
warn.cc: In method `int foo::bar()':
warn.cc:5: warning: control reaches end of non-void function `foo::bar()'
AFAIK neither ISO C++ nor ISO C99 mandate a warning in this case, and as the
warning is output when working with function in RTL representation and g++ 2.96
(and in 2.97 now both C and C++) only issue functions as RTL when actually
they will be output (inlining is done on TREE representation), there is not
much which can be done about this, unless this case detection would be
done on TREE representation.
Well, although C++ standard C.1.4 [diff.stat]
2 Change: It is now invalid to return (explicitly or implicitly) from a
function which is declared to return a value without actually return-
ing a value
and 6.6.3 [stmt.return]
2 Flowing off the end of a function is equivalent to a return with
no value; this results in undefined behavior in a value-returning function.
labels such a program as invalid and having an undefined behavior, you are right
that there is not required a diagnostic.
But let me reopen it as a documentation bug ;) :
gcc.info, Options to Request or Suppress Warnings says:
Print extra warning messages for these events:
* A function can return either with or without a value.
(Falling off the end of the function body is considered
returning without a value.) For example, this function would
evoke such a warning:
if (a > 0)
Modifying foo::bar() that it has the body above won't produce a warning as said
by the documentation. That why the doc or the compiler is erroneous.
This works fine with gcc-c++-3.3.2-1.