It looks like the warning flags you want are -Wuninitialized -Winit-self
(see it live):
Warn about uninitialized variables that are initialized with themselves. Note this option can only be used with the -Wuninitialized option.
For example, GCC warns about i being uninitialized in the following snippet only when -Winit-self has been specified:
int f()
{
int i = i;
return i;
}
This warning is enabled by -Wall in C++.
Based on the comments below there may be some version dependencies. Note, that clang
generates a warning for this just using -Wall
, which seems more sensible to me:
warning: variable 'x' is uninitialized when used within its own initialization [-Wuninitialized]
int x = x;
~ ^
The live example I linked above also includes a commented out clang
command line.