[Issue 4694] Unused last assignment warning

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Aug 20 14:54:08 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4694



--- Comment #1 from bearophile_hugs at eml.cc 2010-08-20 14:54:07 PDT ---
Answers to a comment by Jonathan M Davis:

Okay. Giving a warning for an unused variable makes sense. However, giving a
warning for setting it somewhere that is not where it is declared doesn't make
sense. Sure, in this case, it's dumb of the programmer to have done that, but

1. The compiler should be able to optimize out such a simple case.

2. More complex cases much harder to detect, and it's not all that hard for you
to _want_ to disconnect the declaration of the variable and initializing it -
likely because of scoping issues or conditional blocks. The compiler is only
going to be able to detect the simplest cases, and as I said in #1, such simple
cases will likely get optimized away. Cases too complex to optimize away are
going to be much harder for the compiler to detect, and if you start going that
far, you're getting to the point where you just about should have done what
Java did and force initialization of variables rather than default initialize
them.

Not initializing a variable yourself, and the setting it later with a value
that you could have set it to at its declaration is not a coding error. It may
not be best practice, but it's not going to result in an error. So, I don't
think that it makes any sense to make it a warning.

---------------

> However, giving a warning for setting it somewhere that is not where it is
> declared doesn't make sense.

This is not the purpose of this warning.

The purpose of this warning is to spot situations where you assign something to
a variable and then you forget to use such contents. It's kind of the opposite
of the unused variable.


> 1. The compiler should be able to optimize out such a simple case.

The compiler is often able to remove dead code, but the purpose of this warning
is to detect a point where code is written badly (no need for an operation) or
it's a possible sign of a spot where the programmer has forgotten to use a
variable.


> 2. More complex cases much harder to detect,

This is true, but this is just a warning, it's not a change in the language, so
even if the compiler is not able to spot the most complex situations, it's OK
still. A partial help against bugs is better than no help.

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