$ cat return.c
#include <assert.h>

int f(int x) {
    if(x)
        return x;
    assert(x);
}

$ gcc -O2 -c -Wall -W return.c 
return.c: In function ‘int f(int)’:
return.c:9: warning: control reaches end of non-void function

The call to assert expands to (glibc 2.4):

    (static_cast<void> ((x) ? 0 : (__assert_fail ("x", "return.c", 8,
__PRETTY_FUNCTION__), 0)));

So unless I'm missing something f() is equivalent to:

if(x)
  return x;
else
  __assert_fail(/* etc */);

However when explicitly written in this way, we don't get a warning:

$ cat return2.c
#include <assert.h>

int f(int x)
{
    if(x)
        return x;
    else
        __assert_fail ("x", __FILE__, __LINE__, __PRETTY_FUNCTION__);
}

$ gcc -O2 -Wall -c return2.c
$

Looking at the produced assembly on an x86-64 system, GCC is only performing
the comparison once (when optimizing), so it should see that there is no
codepath that doesn't either return or call a noreturn function. However I'm
guessing warnings are generated prior to the optimizer running and thus don't
see this?

Marked as Severity: trivial because one can always insert a dummy return after
the assert. Which is good because AFAICT fixing this would require rewriting
GCC. :)


-- 
           Summary: Spurious warnings generated due to not optimizing first
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: trivial
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lloyd at randombit dot net
 GCC build triplet: x86_64-redhat-linux
  GCC host triplet: x86_64-redhat-linux
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31878

Reply via email to