Wish: Variable Not Used Warning

Koroskin Denis 2korden at gmail.com
Wed Jul 9 02:00:56 PDT 2008


On Wed, 09 Jul 2008 12:49:34 +0400, Walter Bright  
<newshound1 at digitalmars.com> wrote:

> Here are some horrid examples from my own code which, to please the  
> client, had to compile with all warnings on for MSVC:
>
> ---
>    p = NULL;  // suppress spurious warning
> ---
>    b = NULL;  // Needed for the b->Put() below to shutup a compiler  
> use-without-init warning
> ---
>    #if _MSC_VER
>    // Disable useless warnings about unreferenced formal parameters
>    #pragma warning (disable : 4100)
>    #endif
> ---
>    #define LOG 0       // 0: disable logging, 1: enable it
>
>    #ifdef _MSC_VER
>    #pragma warning(disable: 4127)      // Caused by if (LOG)
>    #endif // _MSC_VER
> ---
>
> Note the uglification this makes for code by forcing useless statements  
> to be added. If I hadn't put in the comments (and comments are often  
> omitted) these things would be a mystery.

We don't have problems with most of these in D, since there is no C-style  
macros and uninitialized variables.

Moreover, I would be happy to have an `unused` modifier in addition to in,  
out and inout (doh!) to denote that a variable is not going to be used. In  
this case compiler will show an error if the variable is used by chance.  
It could help programmer to catch potential bugs at early stage once he  
eventually start using it. Besides, it really fits well into D, IMO:

void bar( unused int foo ) // no warning is generated
{
}



More information about the Digitalmars-d mailing list