Unused variables, better as error or warning?

bearophile bearophileHUGS at lycos.com
Fri Aug 20 15:48:10 PDT 2010


Walter Bright:

> Not every problem is worth using a sledgehammer to deal with.

Giving a warning where you have not used a variable is not a sledgehammer, it's a light thing, that helps keep code tidy and once in a while helps avoid bugs.

I have not your experience with the troubles caused by warnings, so the last word is of course yours.

But I have another idea, static reflection, similar to the one in the Mozilla Treehydra project. DMD may add a __traits(allVariables, foo) that lists all variables used inside the function foo, and then another __traits(unusedVariable, foo, x) that returns true or false if the variable x inside the function foo is never used :-)

With few similar traits used by a (even in Phobos, if you want) library it's possible to analyse code and implement warnings as desired.

This static reflection, combined with user defined @attributes allows to create simple extensions to the type system, as Treehydra does, but with no need to use another language (as JavaScript, used by the Mozilla project).

Bye,
bearophile


More information about the Digitalmars-d mailing list