Bug 24066 - No warning if control reaches end of non-void function
Summary: No warning if control reaches end of non-void function
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: Red Hat Raw Hide
Classification: Retired
Component: gcc
Version: 1.0
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2001-01-15 20:37 UTC by Enrico Scholz
Modified: 2007-04-18 16:30 UTC (History)
0 users

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2004-02-20 14:42:52 UTC
Embargoed:


Attachments (Terms of Use)

Description Enrico Scholz 2001-01-15 20:37:39 UTC
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 18:50:26 UTC
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 20:42:49 UTC
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 14:42:52 UTC
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.