[Issue 3960] New: Unused variable warning

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 13 16:48:41 PST 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 at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at 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: -------


More information about the Digitalmars-d-bugs mailing list