2

In IntelliJ, in a .java file, some unused code is greyed out indicating that the declared variable or function is never used. Unused imports are removed using Ctrl+Alt+O. Is there any shortcut to remove those unused declarations?

mustang
  • 105
  • 1
  • 2
  • 11
  • I doubt it. Plus it may be used by something else even if it's not used by the project. – Dave Newton Dec 09 '19 at 12:34
  • 2
    Be aware of false positives; annotations like `@RestController` where an annotations scanning process uses the classes often also result in an "unused" grey-out. – Joop Eggen Dec 09 '19 at 13:32
  • `@RestController` should be known by the IntelliJ IDEA spring support, but I get the idea @joop-eggen. – Peter Dec 09 '19 at 13:34

3 Answers3

9

As @Magnilex pointed out, hover over the unused variable / method and hit alt + enter to open the intentions menu (if that's how its called).

But in addition to this, don't just remove / safe delete this on variable, but choose "Fix all 'Unused declaration' problems in this file" which will not only get rid of this one, but of all.

See here: IDEA safe delete interactions menu

An alternative approach might be to us the "Run inspections by name" and run the "Unused declaration" inspection for your whole project / module / Package, whatever.

This can be achieved by either right clicking on the editor, and chosing >Analyze >Run Inspection by Name from the context menu (or the keyboard short cut, which is ctrl+alt+shift+i by default). Then type "Unused declaration":

IDEA run inspections Unused declaration

This will inspect your code for unused declarations and display them in an overview:

Idea inspection overview

See also https://stackoverflow.com/a/56593832/2987273

Alexander
  • 2,925
  • 3
  • 33
  • 36
2

Check the Code-Cleanup documentation, see if any of those solutions fit your needs. Creating a profile for that task might be a decent idea, as you can trigger it from anywhere, and hit all relevant occurrences. Just be aware that his approach might "eat up" some new / uncompleted code, or even raise false-positives, e.g. due to a child project which is not in scope. So: handle with care.

Nikolas
  • 2,066
  • 1
  • 19
  • 20
  • 1
    The link points to JetBrains Rider. Does this feature exists in IntelliJ IDEA? I only know about the `inspect code` analysis. – Peter Dec 09 '19 at 13:31
  • YES, you are right! This one appears to be Rider-Specific! – Nikolas Dec 09 '19 at 16:07
1

There is not shortcut to remove unused code in a file. Just to make that sure: IntelliJ IDEA is only able to check if the code is currently used in your project.

If the declaration is used by another consumer, that is not part of your project, you may break the consumer when removing that piece of code.

Due to that fact, it's not possible to implement a general automatic remove unused declaration.

There may be cases where this is more safe like an unused private method, but there is no save automatic way to do that.

Therefore, there is no shortcut.

What you can do in order to check your code base and decide whether or not to remove code marked as unused is to run a Inspect Code analysis.

Just right click on you file or source code folder, got to Analyze and Inspect Code. Choose the scope, like a folder or a specific file and press Ok.

After that you can scroll through the warnings aso.

Peter
  • 4,752
  • 2
  • 20
  • 32