c++clangcompiler-warningsuninitialized-variable

Clang doesn't warn about uninitialized stack variable


Had a bug in this code:

bool x;
    
for(const auto a : b)
{
    x = x && a.b;
}

x should have initialized to true.

I'd like to raise a compiler error that the bool wasn't explicitly initialized, but I cannot find a warning which covers this. We have the obvious flags:

-Wall \
-pedantic \
-Werror=uninitialized \ 

Is there another Clang warning that would catch this and other uninitialized variables?


Solution

  • For this particular issue, compile with -Werror=conditional-uninitialized and you'll get a clear warning, turned into an error:

    <source>:10:13: error: variable 'x' may be uninitialized when used here [-Werror,-Wconditional-uninitialized]
       10 |         x = x && a.b;
          |             ^
    <source>:7:11: note: initialize the variable 'x' to silence this warning
        7 |     bool x;
          |           ^
          |            = false