Bug 51896

Summary: Should gcc warn if a variable initializer inside a switch statement is never used?
Product: gcc Reporter: Kai Engert <kaie>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Severity: enhancement CC: mpolacek, pancik
Priority: P3 Keywords: diagnostic
Version: 4.6.2   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2012-01-19 00:00:00

Description Kai Engert 2012-01-19 01:35:30 UTC
While maintaining existing code, we encountered a snippet like this:

--------------- begin test.c -----------------------
#include "stdio.h"
#include "stdlib.h"

int main(int argc, char *argv[])
  int *a = (int*)0xaaaaaaaa;
  printf("a is: %p\n", a);
  int r = rand() % 2;

  switch (r)
    int *b = (int*)0xbbbbbbbb;
    case 2: break;
      printf("b is: %p\n", b);

  return 0;
--------------- end test.c -----------------------

$ gcc -o test -Wall test.c
test.c: In function ‘main’:
test.c:17:13: warning: ‘b’ may be used uninitialized in this function [-Wuninitialized]

$ ./test 
a is: 0xaaaaaaaa
b is: 0x434b5fc4


As gcc decides to ignore value 0xbbbbbbbb, it should print a warning message, informing the user, e.g.:

  test.c: warning: ignoring initialization value ‘0xbbbbbbbb’
Comment 1 Richard Biener 2012-01-19 10:04:01 UTC
Confirmed.  This is similar to

main(int argc, char *argv[])
  goto _default;
      int *b = (int*)0xbbbbbbbb;

      printf("b is: %p\n", b);

  return 0;

thus, when you jump across an init expression and still use the variable.
Comment 2 Marek Polacek 2016-01-28 09:35:36 UTC
*** Bug 69527 has been marked as a duplicate of this bug. ***
Comment 3 Marek Polacek 2016-01-28 09:37:42 UTC
This one also looks like a dup.

*** This bug has been marked as a duplicate of bug 49859 ***