[Issue 2197] New: Need warning on declared, but unaccessed, variables
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Jul 5 20:43:30 PDT 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2197
Summary: Need warning on declared, but unaccessed, variables
Product: D
Version: 1.029
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: business3 at twistedpairgaming.com
When a variable is declared, but never accessed, a warning should be issued.
I've spent a lot of time tracking down errors that resulted from forgetting to
actually use a variable that I needed (such as an important function
parameter). A warning would help prevent that.
--------------------
An example scenario (sorry if it's extensive): You're working on a comedy club
simulation that contains the following code:
void main()
{
// Bunch of other code here
// Respond to joke
Stdout.format("{}", laugh(Language.English));
// Bunch of other code here
}
// Bunch of other code here
enum Statement { Greeting, LaughPortion }
enum Language { English, Alien }
char[] laugh(Language lang)
{
Statement s = Statement.LaughPortion;
return repeat(getInternational(s, lang), 3);
}
char[] getInternational(Statement s, Langauge lang)
{
if(lang == Language.English)
{
if(s == Statement.Greeting)
return "Hello";
else if(s == Statement.LaughPortion)
return "Ha";
}
else if(lang == Language.Alien)
{
if(s == Statement.Greeting)
return "Blorg";
else if(s == Statement.LaughPortion)
return "Ook";
}
}
You run it and realize that "HaHaHa" isn't appropriate for all the jokes
because some are side-splitting and others are lame. So you decide to add a
parameter to laugh() that takes the number of "Ha"'s.
So you add ", int numLaughs" to the laugh() parameter list. As you're doing
that you think, "That chicken joke's only a one 'Ha', I don't want to forget to
fix that." So you go to the chicken joke section and change
"laugh(Language.English, 1)". The alien joke section is right next to that, and
it has a couple of real knee-slappers, "laugh(Language.Alien, 6)". Then one
other place needs to be refactored to be able to use this new parameter. (More
distractions here). Then you finally try to compile and it fails because you
forgot to add the parameter ", 3" in a few more places. Now it compiles, you
test it and you think, "Why the heck isn't this new numLaughs parameter
working?!?!" and go off debugging.
If the compiler had this warning, the problem would have been obvious:
"contrived.d(743): Warning: In function 'laugh', 'numLaughs' is declared but
never used."
--------------------
Another benefit of this warning is that it would help in code cleanup. For
instance, you might forget that a particular variable is no longer needed (ex,
'statementIntesity' eventually gets added into the 'international' module), or
be trying to cleanup and think "Do I need this variable or not?".
--
More information about the Digitalmars-d-bugs
mailing list