Jump to page: 1 24  
Page
Thread overview
[Issue 3960] New: Unused variable warning
Aug 20, 2010
Jonathan M Davis
Jun 10, 2011
yebblies
[Issue 3960] Unused local variables not reported
Jun 10, 2011
Stewart Gordon
Jun 10, 2011
Jonathan M Davis
Jun 11, 2011
yebblies
Jun 11, 2011
Stewart Gordon
Jun 11, 2011
yebblies
Jun 11, 2011
Stewart Gordon
Jun 11, 2011
yebblies
Feb 02, 2012
Marco Leise
Feb 06, 2012
timon.gehr@gmx.ch
Feb 09, 2012
Marco Leise
Feb 09, 2012
Stewart Gordon
Feb 09, 2012
Marco Leise
Feb 09, 2012
Jonathan M Davis
Feb 18, 2012
timon.gehr@gmx.ch
Apr 26, 2012
Jonathan M Davis
Apr 26, 2012
Stewart Gordon
Apr 26, 2012
Stewart Gordon
Aug 23, 2012
Stewart Gordon
March 14, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3960

           Summary: Unused variable warning
           Product: D
           Version: 2.041
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-03-13 16:48:40 PST ---
This is C program:

#include "stdio.h"
#include "stdlib.h"
int main(int argc, char** argv) {
    int x, y;
    x = (argc > 1) ? atoi(argv[1]) : 10;
    printf("%d\n", x);
    return 0;
}


Compiled with gcc v.4.4.1 with:
gcc -Wall foo.c -o foo

The compiler prints:
foo.c: In function 'main':
foo.c:4: warning: unused variable 'y'


The Intel C/C++ compiler gives a similar error (this is a mockup, but it's
equal or very close to the real one):
foo.c(4): warning #177: variable "y" was declared but never referenced
    int x, y;
           ^
-----------------------

This is C# code:

using System;
public class Foo {
    public static void Main() {
        int x, y;
        x = int.Parse(Console.ReadLine());
        Console.WriteLine(x);
    }
}


With default settings the C# compiler prints:
prog.cs(4,16): warning CS0168: The variable `y' is declared but never used
Compilation succeeded - 1 warning(s)


This explains the warning CS0168: http://msdn.microsoft.com/en-us/library/tf85t6e4.aspx

-----------------------

In Java all the most important editors/IDEs detect unused local variables: JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, etc.


If the most important compilers/IDEs of the most important and used languages (C/C++, C#, Java) give warnings for unused variables, then it can be an useful warning.

By itself an unused variable is not an error, but its presence is often caused due to oversight (either you declared a variable you didn't need or you refactored and a variable that was once needed now is no long needed). While coding in C the GCC compiler has avoided me 2 possible bugs thanks to that unused variable warning, because I was forgetting some part of the code.

So I believe this warning can be useful in D too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 17, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3960



--- Comment #1 from bearophile_hugs@eml.cc 2010-03-17 05:30:02 PDT ---
A related warning can be see with this C# program:


using System;
namespace Test {
    class Foo {
        int x;
        private void Add(int y) {
            x += y;
        }
        static void Main() {
            System.Console.WriteLine("test");
        }
    }
}



Compiled with mono:
...gmcs test.cs
test.cs(5,22): warning CS0169: The private method `Test.Foo.Add(int)' is never
used
Compilation succeeded - 1 warning(s)


In D private methods can be used by other functions in the same module, so it's less easy to produce a warning like this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3960



--- Comment #2 from bearophile_hugs@eml.cc 2010-08-20 09:04:27 PDT ---
There is more than just unused variables, there are also unused last assignments:


void main() {
    int x;
    x = 10;
}


Here 'x' is not an unused variable, because the code does something with it, but the code deserves a warning anyway (and one C compiler-like shows this warning) because the last value assigned to it gets unused; and this is wasted coding/running effort at best, or sign of a possible latent bug (just like unused variables).

This warning isn't necessary if the variable address is assigned to a pointer or similar things.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3960


Jonathan M Davis <jmdavisProg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmail.com


--- Comment #3 from Jonathan M Davis <jmdavisProg@gmail.com> 2010-08-20 09:54:44 PDT ---
Okay. Giving a warning for an unused variable makes sense. However, giving a warning for setting it somewhere that is not where it is declared doesn't make sense. Sure, in this case, it's dumb of the programmer to have done that, but

1. The compiler should be able to optimize out such a simple case.

2. More complex cases much harder to detect, and it's not all that hard for you to _want_ to disconnect the declaration of the variable and initializing it - likely because of scoping issues or conditional blocks. The compiler is only going to be able to detect the simplest cases, and as I said in #1, such simple cases will likely get optimized away. Cases too complex to optimize away are going to be much harder for the compiler to detect, and if you start going that far, you're getting to the point where you just about should have done what Java did and force initialization of variables rather than default initialize them.

Not initializing a variable yourself, and the setting it later with a value that you could have set it to at its declaration is not a coding error. It may not be best practice, but it's not going to result in an error. So, I don't think that it makes any sense to make it a warning.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3960



--- Comment #4 from bearophile_hugs@eml.cc 2010-08-20 14:48:59 PDT ---
Sorry, my mistake, I have lumped two different warnings into this enhancement request, so please ignore comment 2

I have moved the second warning to bug 4694

Look at bug 4694 for an answer to comment 3 regarding the second warning.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 01, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3960



--- Comment #5 from bearophile_hugs@eml.cc 2010-09-30 18:16:17 PDT ---
See also "Using Redundancies to Find Errors", by Yichen Xie and Dawson Engler,
2002:
www.stanford.edu/~engler/p401-xie.pdf

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3960



--- Comment #6 from bearophile_hugs@eml.cc 2011-03-27 08:15:13 PDT ---
Two new good related flags in GCC 4.6:

>New -Wunused-but-set-variable and -Wunused-but-set-parameter warnings were added for C, C++, Objective-C and Objective-C++. These warnings diagnose variables respective parameters which are only set in the code and never otherwise used. Usually such variables are useless and often even the value assigned to them is computed needlessly, sometimes expensively. The -Wunused-but-set-variable warning is enabled by default by -Wall flag and -Wunused-but-set-parameter by -Wall -Wextra flags.<


Some discussions in this thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=132909

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 10, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3960


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |peng2cheng2@yahoo.com


--- Comment #7 from yebblies <yebblies@gmail.com> 2011-06-10 11:14:55 PDT ---
*** Issue 3163 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 10, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3960


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|diagnostic                  |accepts-invalid
            Summary|Unused variable warning     |Unused local variables not
                   |                            |reported
           Severity|enhancement                 |normal


--- Comment #8 from Stewart Gordon <smjg@iname.com> 2011-06-10 12:00:38 PDT ---
This is accepts-invalid, not an enhancement request.

http://www.digitalmars.com/d/1.0/function.html
"It is an error to declare a local variable that is never referred to."

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 10, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3960



--- Comment #9 from Jonathan M Davis <jmdavisProg@gmx.com> 2011-06-10 12:13:16 PDT ---
I would point out that if it is made an error to have unused local variables, function parameters must _not_ be included in it. That would be a major problem in cases where overridden functions don't use all of their parameters, but they have to have them because the base class' function does.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2 3 4