This does not seem a language problem, so I don't think GCC will ever react; actually, it is defining a value for a
that might be warned against by some linter:
int a = 0;
^-- warning: redundant initialisation (value is immediately discarded)
a= i%2 ? 0x42 : 42;
What you can, perhaps, do - but this will not catch all possible cases - is recognize the issue at the static level.
I myself do this for some specific cases (which is why I have trained myself to write code in what is sometimes a unusual way -- for example, free(p); p = NULL;
on a single line, or if p is to be immediately discarded, then free(p); // p = NULL;
, so no pointer can ever point to freed memory, and I track this using grep
. The same with fclose(fp); fp = NULL
).
In this case, at least for the common data types and simple initializations, you can recognize the unassignment with a regex too:
((unsigned\\s+)?(long\\s+)?(int|char|float|double)...
(Not a real regex, just throwing in tokens at random. You may want to look at other questions to this effect).
Once you have a means of singling out all definitions, any definition that does not include an equal sign is suspect and can be printed for further analysis. You can avoid a "unneeded initialization" by writing int a; //OK
and ignore the lines containing '//OK'.