# Remove Unused Local Variables

# Description

This rule finds declarations of local variables that are never used and removes them. Reassignments as well as increment and decrement operations are not counted as active usages. Any annotation except for @Deprecated and @SuppressWarnings prevents the local variable from being considered as unused.

# Avoiding Side Effects

In general, any method or constructor invocation may cause side effects that may be necessary for the correct behavior of the program execution. Therefore, the initializers in local variable declarations or in the right-hand sides of reassignments that may cause side effects are preventing the local variable from being removed by this rule.
Some exceptions are made for some popular Java built-in constructors and methods. For example:

  • constructors of collections, maps, sets, and other popular Java classes, e.g., new ArrayList<>(), new HashMap<>(), new Object(), etc...
  • methods for retrieving information about a collection without modifying it, e.g., contains(), isEmpty(), size(), lastIndexOf(), etc...
  • factory methods for collections, e.g., List.of(), Set.of(), etc...

# Benefits

Some benefits of removing unused code, and in particular unused local variables, are:

  • Reduces maintenance costs.
  • Reduces the compilation time.
  • May potentially eliminate unnecessary computations for unwanted side effects.

# Tags

# Code Changes

# Remove Unused Variable Declaration

Pre

String usedLocalVariable = "";
String unusedLocalVariable = "";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable);

Post

String usedLocalVariable = "";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable);

# Selective Removing Of Variable Declaration Fragments

Pre

String usedLocalVariable1 = "",
		unusedLocalVariable2 = "",
		usedLocalVariable3 = "",
		unusedLocalVariable4 = "";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable1);
blackHole.use(usedLocalVariable3);

Post

String usedLocalVariable1 = "",
		usedLocalVariable3 = "";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable1);
blackHole.use(usedLocalVariable3);

# Remove Declaration Together With Re-Assignment

Pre

String usedLocalVariable = "";
String unusedLocalVariable = "";

unusedLocalVariable = "s";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable);

Post

String usedLocalVariable = "";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable);

# Remove Unused Variable With @SuppressWarnings

Pre

String usedLocalVariable = "";
@SuppressWarnings({ "unused" })
String unusedLocalVariable = "";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable);

Post

String usedLocalVariable = "";

BlackHole blackHole = new BlackHole();
blackHole.use(usedLocalVariable);

# Remove Unused Loop Counter

Pre

BlackHole blackHole = new BlackHole();
int counter = 0;

for (String s : strings) {
	counter++;
	blackHole.use(s);
}

Post

BlackHole blackHole = new BlackHole();

for (String s : strings) {
	blackHole.use(s);
}

You Want To Have Those Changes Done Automatically?

The automatic application of this rule is supported in the following jSparrow version:

# Properties

Property Value
Rule ID RemoveUnusedLocalVariables
First seen in jSparrow version 4.9.0
Minimum Java version 1.1
Remediation cost 2 min