https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96057
Bug ID: 96057 Summary: -Wreturn-type warning message disappears with -O1 to -Os in unnamed namespace definition Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: haoxintu at gmail dot com Target Milestone: --- Hi, all. This small code, bug.cc, the -Wreturn-type warning disappears when add -O1 or higher optimization option. $cat bug.cc namespace { int foo() { 1 ? 0 : throw 0 ; } } $g++ -c -Wreturn-type -O0 bug.cc bug.cc: In function 'int {anonymous}::foo()': bug.cc:2:35: warning: control reaches end of non-void function [-Wreturn-type] 2 | int foo() { 1 ? 0 : throw 0 ; } | $g++ -c -Wreturn-type -O1 bug.cc //emit nothing with -O1 to -Os option While in Clang, the output of -O0 to -Os is the same. For example, $clang++ -c -Wreturn-type -O1 bug.cc bug.cc:2:35: warning: non-void function does not return a value [-Wreturn-type] int foo() { 1 ? 0 : throw 0 ; } ^ 1 warning generated. $g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/home/haoxin/corpus-compilers/gcc-trunk/gcc-master/build/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../configure --prefix=/home/haoxin/corpus-compilers/gcc-trunk/gcc-master/build/ --enable-languages=c,c++ CC=gcc CXX=g++ Thread model: posix Supported LTO compression algorithms: zlib gcc version 11.0.0 20200630 (experimental) (GCC)