-2

I am using latest PMD on a Java project to achieve dead code elimination. I used this rule UnusedLocalVariable and it worked perfectly producing as output the unused local variables. Is there any way to apply those findings directly in the code, like removing the unused variables that have been found?

thanks,

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
sid
  • 1
  • 1

1 Answers1

0

Is there any way to apply those findings directly in the code, like removing the unused variables that have been found?

AFAIK, no there isn't.

The fundamental problem is that most PMD rules either detect potential problems, or don't have a rewrite that can be automated reliably.

At any rate, PMD doesn't support rewriting.

On the other hand, IDEs like Eclipse and Intellij do offer wizards for performing refactoring ... without any direct integration with PMD or similar.

(For what it is worth, the "refactor" for a eliminating an unused local variable is so simple that it is hardly worth the effort of implementing it. And, more generally, if you have sufficient problems with your code that hooking PMD up to automated rewriting is worth doing, then you have "a problem" with the way that you are writing your code!)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • thanks for the prompt reply. are you aware of any tool able to do that? thanks – sid Jun 28 '18 at 13:11
  • thanks, I thought your AFAIK was referring specifically to PDM – sid Jun 28 '18 at 13:36
  • since you mentioned that IDEs are able to do that refactoring, would you suggest any of those plugins? I tried [unnecessary-code-detector](https://marketplace.eclipse.org/content/unnecessary-code-detector) but did not work. – sid Jun 28 '18 at 13:41
  • Nope. Questions that ask for software recommendations are off-topic. I only answered your question to point out that what you (originally!) asked 1) most likely does not exists, and 2) is probably a bad idea. – Stephen C Jun 28 '18 at 13:48
  • please elaborate more about 2. are you saying that dead code elimination is a bad idea by itself?? – sid Jun 28 '18 at 14:01
  • I said: *"The fundamental problem is that most PMD rules either detect (only) potential problems, or don't have a rewrite that can be automated reliably."* and *"And, more generally, if you have sufficient problems with your code that hooking PMD up to automated rewriting is worth doing, then you have "a problem" with the way that you are writing your code!"* – Stephen C Jun 28 '18 at 14:08
  • I am saying that hooking up PMD (or similar) to automatic rewriting is a bad idea. – Stephen C Jun 28 '18 at 14:11