https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127254
Bug ID: 127254
Summary: False positive -Wmaybe-uninitialized with
std::optional
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: mskybert+gccbugs at gmail dot com
Target Milestone: ---
Created attachment 65523
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65523&action=edit
Preprocessed source
Similar to a few other "optional and false positive 'may be used uninit'" bugs,
but different enough I can't be sure it's the same.
#include <optional>
#include <ostream>
extern std::basic_ostream<char>
get_stream();
static void
call(std::optional<short> num, int cond)
{
auto os = get_stream();
if (cond == 234) num.reset();
os << 3 << "";
if (num) os << *num;
}
void
func(int arg)
{
call(std::nullopt, arg);
}
$ c++ -Wall -O2 -c bad_compile.cpp
bad_compile.cpp: In function ‘void call(std::optional<short int>, int)’:
bad_compile.cpp:14:21: warning: ‘<anonymous>’ may be used uninitialized
[-Wmaybe-uninitialized]
14 | if (num) os << *num;
| ^~~
cc1plus: note: ‘<anonymous>’ was declared here
Adding -O1 or -fno-tree-sra makes the warning go away. Changing almost
anything in the source makes it go away.
This is on Fedora 44 with gcc 16.2.1. Same warning on SLES 15.7 with gcc
15.3.0, but no warning on SLES 15.7 with gcc 14.3.0.
== Fedora 44, gcc 16:
$ c++ -v
Using built-in specs.
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/16/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,cobol,algol68,lto
--prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugzilla.redhat.com/ --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu
--enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-16.2.1-build/gcc-16.2.1-20260819/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none,amdgcn-amdhsa --enable-offload-defaulted
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-tls=gnu2 --with-arch_32=i686
--build=x86_64-redhat-linux --with-build-config=bootstrap-lto
--enable-link-serialization=1 --disable-libssp
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.2.1 20260819 (Red Hat 16.2.1-2) (GCC)
== SLES 15.7, gcc 15
$ g++-15 -v
Using built-in specs.
COLLECT_GCC=g++-15
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/15/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-suse-linux
Configured with: ../configure CFLAGS=' -fmessage-length=0 -grecord-gcc-switches
-O2 -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g'
CXXFLAGS=' -fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' XCFLAGS='
-fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' TCFLAGS='
-fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' GDCFLAGS='
-fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' --prefix=/usr
--infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64
--libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d,m2 --enable-host-pie
--enable-offload-targets=nvptx-none, --enable-offload-defaulted
--without-cuda-driver --enable-checking=release --disable-werror
--with-gxx-include-dir=/usr/include/c++/15
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --enable-ssp --disable-libssp
--disable-libvtv --enable-cet=auto --disable-libcc1 --disable-plugin
--with-bugurl=https://bugs.opensuse.org/ --with-pkgversion='SUSE Linux'
--with-slibdir=/lib64 --with-system-zlib --enable-libstdcxx-allocator=new
--disable-libstdcxx-pch --enable-libphobos
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-15 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic --enable-link-serialization
--build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 15.3.0 (SUSE Linux)
== SLES 15.7, gcc 14
$ g++-14 -v
Using built-in specs.
COLLECT_GCC=g++-14
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/14/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-suse-linux
Configured with: ../configure CFLAGS=' -fmessage-length=0 -grecord-gcc-switches
-O2 -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g'
CXXFLAGS=' -fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' XCFLAGS='
-fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' TCFLAGS='
-fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' GDCFLAGS='
-fmessage-length=0 -grecord-gcc-switches -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -g' --prefix=/usr
--infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64
--libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d,m2 --enable-host-pie
--enable-offload-targets=nvptx-none, --enable-offload-defaulted
--without-cuda-driver --enable-checking=release --disable-werror
--with-gxx-include-dir=/usr/include/c++/14
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --enable-ssp --disable-libssp
--disable-libvtv --enable-cet=auto --disable-libcc1 --disable-plugin
--with-bugurl=https://bugs.opensuse.org/ --with-pkgversion='SUSE Linux'
--with-slibdir=/lib64 --with-system-zlib --enable-libstdcxx-allocator=new
--disable-libstdcxx-pch --enable-libphobos
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-14 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic --enable-link-serialization
--build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.3.0 (SUSE Linux)