Unused variables, better as error or warning?

bearophile bearophileHUGS at lycos.com
Fri Aug 20 15:11:35 PDT 2010


Jonathan M Davis:

> The compile must be just as accurate when dealing with warnings as when dealing 
> with errors.

Nope. If your language (like C#) specs say that uninitialized variables are not allowed, the compiler has to catch them all and report them as errors.
If your language (C, D, etc) allows you to not use a variable the last time you have initialized it, and then you want to add a warning that finds such situations, you don't need the compiler to be 100% accurate, even if some of such situations are not detected then it's perfectly OK.


> The reason is that any good project is not going to have _any_ 
> warnings in it when it's done. And forcing the programmer to do extra, pointless 
> stuff just to get rid of them is not good. Java and C# make you do that in 
> various places with variable initialization because they do code flow analysis 
> for variable rather than default initialize variables. Because D default 
> initialiazes everething, that sort of code flow analysis becomes unnecessary.

Note that in my last posts I have never asked for uninitialized variables warnings or errors, so I think you have misread my posts (I have asked for that months ago).


> Your example for this exact problem involved a default-initialized variable 
> rather than double-assignment, so if you didn't intend to lump default-
> initialized variables in with double-assigned variables, it was a poor example.

That example was not about default-initialized variables nor double-assigned ones, it was about a variable that is not used after the last time it is assigned.
(I agree that was not a good example.)


> or have the compiler not complain when it couldn't be 100% certain. If an 
> algorithm could be devised which was 100% certain and the warning were only 
> given in such cases, then it would likely be acceptable.

In the meantime I have seen you have already commented on the bug report and I agree with with you write there. Thank you. For the In this specific case I think "zero false positives" is doable :-)

Bye,
bearophile


More information about the Digitalmars-d mailing list