Alexandre Duret-Lutz <adl@xxxxxxxxxxxxx> writes: > The following file foo.c is a simplified version of a subtler bug I > found in my code. > int b;void bar(int a);void foo(int a){ bar(a); a = 42;} > > The line a = 42 is in fact a typo in my code: I meant b = 42. I do > *not* expect the compiler to detect that I made a typo, but I would > like the get a warning that I am assigning to a local variable (or a > function parameter) that is not going to be used anymore. If I compile > this file with > % gcc-4.6 -Wall -Wextra -pedantic -O3 -c foo.c > I get absolutely no warning. Inspecting the generated code shows that > the assignment a = 42 is not performed, so gcc is perfectly well aware > that this instruction is useless (hence potentially bogus). Commenting > the call to bar(a); does produce a "warning: parameter ‘a’ set but not > used [-Wunused-but-set-parameter]", so it seems like gcc will not warn > as long as variable a is used somewhere in the function, even if it is > before the assignment. > My questions: > 1. Is there a way to tell gcc to produce a warning for such case?2. Is > there a reason for the actual behavior? I.e, some cases were it is > actually desirable to assign to local variables that will be thrown > away by the optimizer? As far as I know way there is no way to get a warning for this case at present. GCC does not currently have a framework for tracking sets and uses which is independent of the optimizers. And warnings that are dependent on the optimizers are troublesome for many reasons. GCC does have some warnings like that today, and there are perennial sources of compliant. So there is unlikely to be an easy way to implement this warning. Ian