https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92194
Bug ID: 92194
Summary: maybe-uninitialized false positive with c++2a
Product: gcc
Version: 9.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: malcolm.parsons at gmail dot com
Target Milestone: ---
For this code:
#include <algorithm>
#include <optional>
#include <string>
void f(const std::string& t)
{
if (std::any_of(t.begin(), t.end(), [](const auto& c) {
return c == 0b01000000;
})) {
std::string s;
std::optional<std::string::const_iterator> f;
for (auto i = t.begin(); i != t.end(); ++i) {
const auto n = *i;
if (n == 0b01000000) {
f = i;
} else if (f) {
std::string_view key(&(**f), 2);
s += key;
f.reset();
}
}
}
}
With gcc 9.2.0,
g++ -std=c++17 -O3 -Wmaybe-uninitialized reports no warnings.
g++ -std=c++2a -O3 -Wmaybe-uninitialized reports:
<source>: In function 'void f(const string&)':
<source>:12:52: warning: 'f' may be used uninitialized in this function
[-Wmaybe-uninitialized]
12 | std::optional<std::string::const_iterator> f;
| ^
See https://gcc.godbolt.org/z/ph3l0u
With gcc trunk, the warning changes to:
<source>: In function 'void f(const string&)':
<source>:15:52: warning: 'f.__gnu_cxx::__normal_iterator<const char*,
std::__cxx11::basic_string<char> >::_M_current' may be used uninitialized in
this function [-Wmaybe-uninitialized]
15 | std::optional<std::string::const_iterator> f;
| ^
See https://gcc.godbolt.org/z/8rGDGi