On the following example (missing return statement in a member function) GCC emits no warning: $ cat 1.cc #include <stdlib.h> // exit extern void *f(); class Bag { void *g() { void *p = f(); if (!p) exit(1); } }; $ g++ -c -Wall -Wextra -O2 1.cc $ If I comment out class definition, GCC emits warning: $ cat 1.cc #include <stdlib.h> // exit extern void *f(); //class Bag { void *g() { void *p = f(); if (!p) exit(1); } //}; $ g++ -c -Wall -Wextra -O2 1.cc 1.cc: In function ‘void* g()’: 1.cc:7:5: warning: control reaches end of non-void function [-Wreturn-type] } ^ $
The two cases are not equivalent, because the member function is also inline. If you add 'inline' to the second example you get no warning for that one too. In both cases you get a warning for the inline function if it is used.
I think this is because an unused inline function doesn't generate any code, so isn't seen by the compiler passes that produce the warning.
I think there is a dup of this bug already. Basically what Jonathan is saying is correct.
That sounds reasonable, however if I comment out 'if (!p) exit(1);', I get warning no matter inline or not: $ cat 1.cc #include <stdlib.h> // exit extern void *f(); inline void *g() { void *p = f(); // if (!p) exit(1); } skvadrik@skvadr_home ~ $ g++ -c -Wall -Wextra -O2 1.cc 1.cc: In function ‘void* g()’: 1.cc:4:11: warning: unused variable ‘p’ [-Wunused-variable] void *p = f(); ^ 1.cc:6:1: warning: no return statement in function returning non-void [-Wreturn-type] } ^
In that case no control flow analysis is needed, it's clear that the missing return is always reached. With the conditional exit it depends on whether p can be null, which requires more analysis.
I believe this is a duplicate of the ancient bug 21678 that affects both C and C++: $ cat pr80458.C && gcc -S -Wall -Wextra -xc pr80458.C __attribute__ ((always_inline)) inline int f (int i) { if (!i) __builtin_abort (); } Clang diagnoses the problem as expected, as does ICC: pr80458.C:5:1: warning: control may reach end of non-void function [-Wreturn-type] } ^ *** This bug has been marked as a duplicate of bug 21678 ***