Wish: Variable Not Used Warning

Era Scarecrow rtcvb32 at yahoo.com
Thu Jul 10 04:16:06 PDT 2008


> Manfred_Nowak wrote:
> > JAnderson wrote:
> > 
> >> The more warnings as errors the better.  If I have
> to suffer a
> >> little for false positives *shrug*
> > 
> > What do you understand by "a little"?
> 
> I don't understand what your asking.  I meant that if I
> have to fix it 
> because the compiler tells me its an error then so be it. 
> Its a little 
> pain for a lot of gain.

 Forcing all warnings to be errors will not always be nice or pretty in any given circumstance. And occasionally, the 'error's will appear on certain architectures and not others, am i right?

 Under a basic understanding, the compiler does the following at the start of a function/code block. First prepares the stack with space, then initializes if it's static, Etc.

 With an unused variable, it shouldn't be an error during certain phases, and should be on others. Example. During my first few phases i usually start by declaring my function, start by what it needs, the order of the parameters and names, then i put in several different variables declaring them.
//basic declaration to be finished later
int isPrime(int number, int[] primesList)
{
    int cnt;

    return 0; //shut the compiler up for now.
}

 simply as an example, i can declare primesList, which i intend to implement at a later time for performance and speed, but at the moment i'll implement a very basic formula that is simple and requires very little work.

 What's the worst case that primesList or cnt aren't used right now? 8? 12 bytes in the stack? Geez, you all make it sound like it should be an error and it's illegal to not touch something now because i'm working on it and other things at once.

 I think i'd rather have a simple warning saying 'oh by the way, you aren't using this' when i go through my warnings/error list and i'll say later on down the road 'oh yeah, here's something i need to finish' or 'oh i don't need that variable, let's remove it' rather than....

//basic declaration to be finished later
int isPrime(int number, int[] primesList)
{
    int cnt;
    unusedReferenceToShutUpErrors(cnt);
    unusedReferenceToShutUpErrors(primesList);

    return 0; //shut the compiler up for now.
}

 In my opinion that extra code is just taking up space to tell the compiler to shut the hell up, with no functional usage what-so-ever. Please don't say it isn't, i'm not going to fight and reply on the issue.

 I don't see why everyone is so nit-picky about this specific topic. My opinion? If the variable isn't used, have it as a warning but compile it, if you set warnings as errors, you can remove/comment the declarations when the time comes but i always work to get the code working before i re-factor it to make the 'by the ways' warnings go away, if they need it.


 I'm not sure about everyone else, but i've been reading a lot on how Walter has been defining and building D. The way it works, usage, declarations; There's a few minor things i am not sure yet about (bitfields, multi-dimensional arrays, Ect). However it's a big stepup from C, and looks much easier to understand without ugly operators and overloading that C++ has (::, friend functions, headers and preprocessing, Ect.).

 It was these very things that kept me from learning C++, because it was too hard for me to grasp because it was ugly, and i don't want to make something ugly.

> > Please look at the example from 
> > http://www.digitalmars.com/webnews/newsgroups.php?
> > art_group=digitalmars.D&article_id=73441 
> > 
> > Do you recognize how many warnings a lint tool might
> emit on that code?
> > Would you admit then, that a paranoic lint would be
> quite useless, even 
> > if it detects that the variable `p' should be
> accessed?
> 
> I don't understand?  With lint it just gives you hints
> about what could 
> be wrong.  You pick and choose what to fix.
> 
> > Would you 
> > admit, that you yourself are unable to decide whether
> the presence of 
> > some access statements to `p' should suppress the
> warning?
> 
> I would prefer this be an error like C#.  In C++ because
> all my warnings 
> are errors it would be an error too.  If you really want to
> use an 
> uninitialized variable there should be a work around but it
> should be 
> harder to do.

 Indeed. But some warnings you can't ignore without dropping out the code. Take the example.

const DEBUG = 0;

..

if (DEBUG) {
    //put debugging information
}

 The compiler SHOULD give you a happy warning of 'Always comes up False' or 'Always comes up True' when you're enabling/disabling code in this manner. Making all warnings as errors, simply removes the functionality of the code. I believe GCC does a good job at it, last i checked, and occasionally  notice i simply did the equation wrong when i see the warning where it shouldn't be there.

while(1)    //always true
{
    if (someCondition){
        break;
    }
}

 I've had to use this code once or twice, once again you'll have a warning, not strictly an error.

 Era


      



More information about the Digitalmars-d mailing list