Bug 24066 - No warning if control reaches end of non-void function
No warning if control reaches end of non-void function
Status: CLOSED CURRENTRELEASE
Product: Red Hat Raw Hide
Classification: Retired
Component: gcc (Show other bugs)
1.0
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
David Lawrence
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2001-01-15 15:37 EST by Enrico Scholz
Modified: 2007-04-18 12:30 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2004-02-20 09:42:52 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Enrico Scholz 2001-01-15 15:37:39 EST
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 -------
struct foo
{
        int bar()
        {
        }
};

int main()
{
}
-------------------

$ 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()'
$
Comment 1 Jakub Jelinek 2001-01-16 13:50:26 EST
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.
Comment 2 Enrico Scholz 2001-01-16 15:42:49 EST
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:

-------------------
`-W'
     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:

               foo (a)
               {
                 if (a > 0)
                   return a;
               }
-------------

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.
Comment 3 Miloslav Trmac 2004-02-20 09:42:52 EST
This works fine with gcc-c++-3.3.2-1.

Note You need to log in before you can comment on or make changes to this bug.