Wish: Variable Not Used Warning

Markus Koskimies markus at reaaliaika.net
Thu Jul 10 05:00:15 PDT 2008


On Thu, 10 Jul 2008 04:16:06 -0700, Era Scarecrow wrote:

[...]
> //basic declaration to be finished later int isPrime(int number, int[]
> primesList) {
>     int cnt;
> 
>     return 0; //shut the compiler up for now.
> }
[...]
> //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 "C++'ish" / D way, this is normally dealt like this:

int isPrime(int number, int[] /*primesList*/)
{
	//int cnt;

	return 0;
}

Hard & ugly? I think warnings are not meant to be used to remember you 
that you have something unfinished. I regularly tag those parts with "//
todo", which is easily grep'd from code.

>  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.

It is not about consuming memory, it's about compiling code that won't 
work. It is not about making intentionally dead code, it's about 
accidentally having dead code.

>  I don't see why everyone is so nit-picky about this specific topic.

Because syntactic salts and more pedantic checkings save a lots of 
debugging time.

>  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.

Warnings & checkings does not make C++ ugly :)

Constant conditionals:

> const DEBUG = 0;
> 
> ..
> 
> if (DEBUG) {
>     //put debugging information
> }

1) Use static if?

2) Anyway, it is not constant conditional in that way that it is normally 
warned - you have intentionally set the value so, and thus the compiler 
could optimize the dead code away without problem. Normally it is warned, 
if you have an expression:

	for(...; (a + b) < 8; ...) { ... }

...if the compiler recognizes, that the condition cannot never be 
anything else than true or false, do you think it is intentional?

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

The same hold here; "while(true)", "for(;;)" are intentionally written to 
be infinite loops. I regularly define in C/C++:

#define forever for(;;)

I really don't mean, that the "warning system" should be something like 
"code smelling analyzing" for refactoring, but I think that most of those 
basic things could easily be catched at compile time (w/ warnings) 
without any negative side effects.



More information about the Digitalmars-d mailing list