Bug 80458 - [-Wreturn-type] false negative on missing return statement in a member function
Summary: [-Wreturn-type] false negative on missing return statement in a member function
Status: RESOLVED DUPLICATE of bug 21678
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-04-18 22:20 UTC by Ulya
Modified: 2019-02-03 23:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.1.3, 4.3.5, 4.4.7, 4.8.5, 4.9.4, 5.4.0, 6.4.0, 7.3.0, 8.2.0, 9.0
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ulya 2017-04-18 22:20:02 UTC
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]
     }
     ^
$
Comment 1 Jonathan Wakely 2017-04-18 22:36:32 UTC
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.
Comment 2 Jonathan Wakely 2017-04-18 22:40:52 UTC
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.
Comment 3 Andrew Pinski 2017-04-19 06:00:47 UTC
I think there is a dup of this bug already.  Basically what Jonathan is saying is correct.
Comment 4 Ulya 2017-04-19 07:17:54 UTC
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]
 }
 ^
Comment 5 Jonathan Wakely 2017-04-19 08:18:25 UTC
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.
Comment 6 Martin Sebor 2019-02-03 23:38:12 UTC
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 ***