Bug 51896 - Should gcc warn if a variable initializer inside a switch statement is never used?
Summary: Should gcc warn if a variable initializer inside a switch statement is never ...
Status: RESOLVED DUPLICATE of bug 49859
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 69527 (view as bug list)
Depends on:
Blocks:
 
Reported: 2012-01-19 01:35 UTC by Kai Engert
Modified: 2016-01-28 09:37 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-01-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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;
    default:
      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


Proposal: 

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

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

      _default:
      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 ***