Thread overview
[Issue 2197] New: Need warning on declared, but unaccessed, variables
Jul 06, 2008
d-bugmail
Apr 09, 2009
d-bugmail
Apr 09, 2009
d-bugmail
July 06, 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@digitalmars.com
        ReportedBy: business3@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?".


-- 

April 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2197


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com




------- Comment #1 from smjg@iname.com  2009-04-09 04:44 -------
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++).


-- 

April 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2197





------- Comment #2 from matti.niemenmaa+dbugzilla@iki.fi  2009-04-09 14:09 -------
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);
}


-- 

April 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=2197


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #3 from bearophile_hugs@eml.cc 2012-04-24 18:15:32 PDT ---
See also Issue 3960  and  Issue 4694

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------