Docs: Section on local variables

Stewart Gordon smjg_1998 at yahoo.com
Wed Apr 25 04:24:19 PDT 2012


On 21/04/2012 19:24, H. S. Teoh wrote:
<snip>
> In finished code, it's obviously a bad thing to have unused variables
> (unless the compiler optimizes them away,

Whether the compiler optimises it away or not, an unused variable is a code smell. 
Complaining about unused variables serves as a warning to the programmer that there's 
probably a bug in the program.  Even if it's left over from debugging, it looks silly, and 
might lead other people reading the code to believe something's wrong.

> but that's not happening 'cos
> it depends on flow analysis, which would have let us spit out warnings
> about it in the first place.)

How does seeing that there are no references to a variable anywhere in its scope depend on 
flow analysis?

>> So do you prefer just an unused variable warning that comes out only
>> when you use "-wi/-w"? A problem I've seen in D.learn is that lot of
>> people here doesn't seem to use -wi/-w.

So you think compiler warnings should be compulsory - with perhaps a CLO just to control 
whether they cause the compilation to fail?

>> Or maybe, on the contrary,
>> this unused variable error should be suppressed only if the D code is
>> compiled with "-debug"?
>
> I don't know if conflating unused variable warnings with -debug is a
> good thing. Just like the conflation of -release with the opposite of
> -debug or -unittest.
<snip>

I don't really like this idea either.  The point of -debug is to add code to the program 
for debugging.  You might need to switch this debugging code on/off independently of 
whether you have unused variables.

Two possibilities I can see:
- Keep the statement in the spec, and fix DMD to implement it properly.  Maybe add a CLO 
to suppress errors such as this one that are only there to catch bugs.
- Remove the statement from the spec, and implement a warning in DMD.

Stewart.


More information about the Digitalmars-d-learn mailing list