Unused variables, better as error or warning?

bearophile bearophileHUGS at lycos.com
Fri Aug 20 14:06:49 PDT 2010


Jonathan M Davis:

I have seen you have commented in the enhancement request too, thank you for your answers.


>I agree with #1, #3, and #4 but not #2. It requires control flow analysis to get that to work, and Walter generally avoids that.<

The situation here is different.

Walter was opposed to forcing initialization of variables and then using flow analysis to spot all cases where the program isn't doing this. Walter has said that some cases can't be determined (and probably also to keep compler simpler and faster) has refused to do this (that the C# compiler does).

But what I am discussing there is not a change in the language, it's a diagnostic thing. A conformant D compiler is free to give or not give warning for unused variables (or variables unused after the last assignment. This is a different warning).

This means that it's OK in this case for the compiler to not perform an accurate analysis, because even if it misses some cases it's OK still. So it's a very different situation.


>In more complex cases, the compiler is not going to be able to accurately detect when something is or isn't going to be assigned to in ever code path, so you'd be forced to initialize it in spite of the fact that it's default initialized.<

I think we are talking about two different things.

The "unused last assignment" warning means that you assign something to a variable, and then you do nothing with it. So you have wasted efforts, and that is code smell of sloppy or code where you have forgotten to use the variable.

This has nothing to do with uninitialized variables, it's kind of the opposite problem. Sorry for lumping two different warnings into the same enhancement request.

Bye,
bearophile


More information about the Digitalmars-d mailing list