gcc failing to warn of uninitialized variable

It looks like you can’t – see this bug report. (And this one, which is marked as a dupe of that one – it has an identical test case to yours.) Since it looks like the root-cause bug is almost 10 years old, it would seem it’s not an easy problem to solve. In fact, the second bug I linked to has the phrase “Never going to be fixed” in the discussion, so that doesn’t look good.

If it’s really important to you, clang does catch this one with -Wsometimes-uninitialized, which is included with -Wall:

a.c:3:7: warning: variable 'a' is used uninitialized whenever 'if' condition is
      false [-Wsometimes-uninitialized]
  if (b)
      ^
a.c:5:10: note: uninitialized use occurs here
  return a;
         ^
a.c:3:3: note: remove the 'if' if its condition is always true
  if (b)
  ^~~~~~
a.c:2:8: note: initialize the variable 'a' to silence this warning
  int a;
       ^
        = 0
1 warning generated.

Leave a Comment