https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103159
Bug ID: 103159
Summary: -Wuninitialized should not depend on optimization
level
Product: gcc
Version: 10.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: mathieu.malaterre at gmail dot com
Target Milestone: ---
Consider the following:
```
% cat undef.cxx
#include <iostream>
class C
{
int I;
public:
int getI() { return I; }
};
int main()
{
C c;
std::cout << c.getI() << std::endl;
return 0;
}
```
It is counter-intuitive to explicitely use -O2 to have a warning reported.
Compare the output of:
```
% g++ -o undef -Wuninitialized undef.cxx && ./undef
0
```
While:
```
% g++ -O2 -o undef -Wuninitialized undef.cxx && ./undef
undef.cxx: In function ‘int main()’:
undef.cxx:7:25: warning: ‘c.C::I’ is used uninitialized in this function
[-Wuninitialized]
7 | int getI() { return I; }
| ^
0
```