digitalmars.D.learn - Detect unused variables
After a short search I found a post which says, that currently and in future no warning is shown if unused variables exist. Is that correct?
May 10 2012
"Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, May 10, 2012 22:30:14 Namespace wrote:After a short search I found a post which says, that currently and in future no warning is shown if unused variables exist. Is that correct?
There is not currently any such warning, and I wouldn't expect there to ever be one. As I understand it, Walter Bright finds them to be annoying, so they're unlikely to be added, just because of that. But it would also cause issues for some generic/generated code to have to worry about unused variables, so there are also technical issues why warning about them would be a problem. The online documentation _does_ incorrectly state that unused local variables is an error, which caused some debate when that was brought up. There's also a related bug report ( http://d.puremagic.com/issues/show_bug.cgi?id=3960 ). So, it's not 100% certain that there will never be a warning for unused variables, but I'd be surprised if such a warning was ever added. - Jonathan M Davis
May 10 2012
Ah, I see. I wrote a short code that detects "normal" unused variables. "Normal" means those variables, which has built-in types. This means, that no types created by the user/programmer will detect. Furthermore, it doesn't detect arrays yet, but maybe there are some people here, who would help me to complete it. unused_vars.d: http://codepad.org/K5OCjlF3 unused_test.d: http://codepad.org/Bk9cXqD2 vertex.d: http://codepad.org/G1HH31Kx Compile and run with: unused_vars <file>, e.g. unused_vars unused_test.d At this point I would also like to ask if there is something that identifies unused imports?
May 10 2012
No interest, I see. I improved the code again. Now, even user-defined types, and (associative) arrays detected. I'm sure there are a lot of situations where my code will fail, but I will continue working on it. Therefore criticisms and suggestions were really helpful. The new code: http://codepad.org/lvAFx4oP And two new test files: http://codepad.org/SgezNiYm http://codepad.org/y2NYrEgt
May 11 2012