gcc -Wunused-variable -c test.c, where test.c contains the following code, fails to warn that variable a is unused: --begin-test.c- static const int a = 27; static const int b = 42; const int *f(void) { return &b; } --end-test.c-- However, gcc -Wunused-variable -c -Dconst= test.c does produce the warning I wanted: test.c:1: warning: 'a' defined but not used
The problem is that for some reason, -Wunused-variable ignores const variables, and there doesn't seem to be any alternative warning option or variable attribute to request unused warnings for const variables. In the application where this was observed, there is a large number of initialised const arrays-of-structs encoding final-state information for a state transition engine. Other const variables and functions encode the fairly complex transition rules. A copy-paste error in the transition rules led to some final states not being reachable, i.e., there were no references to those variables, but -Wunused-variable (implied by -Wall) failed to notice that. -- Summary: -Wunused-variable ignores unused const initialised variables Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: mikpe at csd dot uu dot se http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28901