the following code gets compiled without any warnings (even with -Wall -Wextra): std::string foo(foo); and the resulting code segfaults (clang++ is also silent on this but the code throws std::length_error). i am aware that the example is ridiculous but it comes from a large real-life project and is possibly a result of negligent copy-paste operations, so at least issuing a warning would be nice. full example: #include <iostream> #include <string> using namespace std; int main() { const string foo(foo); cout << foo << endl; return 0; }
You need -Winit-self, but it doesn't work for class types.
Clang++ 3.0 warns: /tmp/webcompile/_14716_2.cc:8:20: warning: variable 'foo' is uninitialized when used within its own initialization [-Wuninitialized] const string foo(foo); ~~~ ^~~ 1 warning generated. Clang improves a lot every 6 months, you should always check the latest version (or SVN if possible).
Related to PR 48829 and PR 48483.
*** Bug 53287 has been marked as a duplicate of this bug. ***
*** Bug 70991 has been marked as a duplicate of this bug. ***
*** Bug 82900 has been marked as a duplicate of this bug. ***
Let me adjust the Summary to better reflect the request.
GCC 11 (since g:b825a22890740f341eae566af27e18e528cd29a7) diagnoses passing an uninitialized object by const reference by -Wmaybe-uninitialized: $ g++ -S -Wall pr52167.C pr52167.C: In function ‘int main()’: pr52167.C:8:23: warning: ‘foo’ may be used uninitialized [-Wmaybe-uninitialized] 8 | const string foo(foo); | ^ In file included from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/string:55, from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/locale_classes.h:40, from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/ios_base.h:41, from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/ios:42, from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/ostream:38, from /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/iostream:39, from pr52167.C:1: /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:448:7: note: by argument 2 of type ‘const std::__cxx11::basic_string<char>&’ to ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ declared here 448 | basic_string(const basic_string& __str) | ^~~~~~~~~~~~ pr52167.C:8:16: note: ‘foo’ declared here 8 | const string foo(foo); | ^~~