Bug 2197 – Need warning on declared, but unaccessed, variables

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2008-07-05T22:43:28Z
Last change time
2019-10-21T10:22:06Z
Keywords
diagnostic
Assigned to
No Owner
Creator
Nick Sabalausky

Comments

Comment #0 by bus_dbugzilla — 2008-07-05T22:43:28Z
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?".
Comment #1 by smjg — 2009-04-09T04:44:39Z
A nice idea, but one problem is that it would raise warnings for method parameters that are there just for those subclasses that need them. Either you'd need to add dummy statements to use them all, or we'd need to add something to the language to enable the warning to be suppressed (such as the ability to omit parameter names like in C++).
Comment #2 by matti.niemenmaa+dbugzilla — 2009-04-09T14:09:32Z
We already can omit parameter names, the following goes through 1.042 just fine: class Foo { int foo(int, int) { return 7; } } void main() { assert ((new Foo).foo(42, 19) == 7); }
Comment #3 by bearophile_hugs — 2012-04-24T18:15:32Z
See also Issue 3960 and Issue 4694
Comment #4 by razvan.nitu1305 — 2019-10-10T14:40:20Z
I think that this would be the job of a third party tool that uses dmd compiler as a library.
Comment #5 by razvan.nitu1305 — 2019-10-21T10:22:06Z
Closing as per comment.