Detector for unused variables
bearophile
bearophileHUGS at lycos.com
Wed Aug 1 07:52:22 PDT 2012
Regan Heath:
> If you make detecting un-used variables a /requirement/ for
> implementing a D compiler, you make it that much less likely
> someone will write one,
Detecting unused variables can be written as a part of the
front-end. It's meant to be shared by LDC and GDC that use the
same front-end.
>We may have gotten used to it being there, but it's not part of
>the "compilation" phase but rather part of the "development"
>phase and/or the "quality control" phase which are arguably the
>realm of the IDE or a "lint" checker.<
In theory you are right, in practice I don't know how much this
idea is applicable to D/DMD and how much good it does.
In Clang they have added a switch to perform extra static tests
on the code. It's a feature built in the compiler. I think this
is an acceptable place to put an unused variable warning in DMD.
They have also built a stand alone analyzer, as you say:
http://clang-analyzer.llvm.org/
But this isn't a fully isolated tool, it re-uses large parts of
the compiler, it's like a plug-in. I think you can't do this with
DMD (maybe it can be done with LDC, but we were talking about a
portable tool for D).
> It also doesn't matter what another language choses to do.
I think that detecting unused variables is part of all C#
compilers too. Looking at other languages is useful because the
C# designers are very smart, and C# share most needs with D. C#
doesn't have C++/D-style templates, this may add some unused
variables to D code, but the situation is not too much different.
>Why you'd want an unused variable to be an error is beyond me -
>it has absolutely no effect on the resulting executable, if it
>did it would be a bug.<
For a D detector of unused variables I was asking for a warning,
as in C#.
And regarding Go, I can't read the mind of the Go designers, but
I think they don't like warnings, on the other hand they want a
very tidy Go code, so they have made it an error. Go doesn't have
templates and it seems so far its programmers are surviving to
the presence of this error.
They want tidy code because experience shows that often variables
that are defined and not used are spots where the programmer has
forgotten something, and sometimes some code is missing.
Beside unused variables warning, I'd like another warning, unused
last assignment warning, it helps find other bugs:
http://d.puremagic.com/issues/show_bug.cgi?id=4694
Thank you,
bye,
bearophile
More information about the Digitalmars-d-learn
mailing list