Why is this an issue?

An unused local variable is a variable that has been declared but is not used anywhere in the block of code where it is defined. It is dead code, contributing to unnecessary complexity and leading to confusion when reading the code. Therefore, it should be removed from your code to maintain clarity and efficiency.

What is the potential impact?

Having unused local variables in your code can lead to several issues:

In summary, unused local variables can make your code less readable, more confusing, and harder to maintain, and they can potentially lead to bugs or inefficient memory use. Therefore, it is best to remove them.

How to fix it

The fix for this issue is straightforward. Once you ensure the unused variable is not part of an incomplete implementation, you just need to remove it.

Java 22 introduces the unnamed variable pattern _. When a variable declared within a pattern match, an enhanced for loop, or a try with resource is unused, you should replace its name with the unnamed variable pattern to clearly indicate the intent not to use the variable.

Code examples

Noncompliant code example

public int numberOfMinutes(int hours) {
  int seconds = 0;   // Noncompliant: "seconds" is unused
  return hours * 60;
}
public String name(Person p) {
  return switch (p) {
    case User(String name, int age) -> name; // Noncompliant: "age" is unused replace it with the unnamed variable pattern (starting from Java 22)
    default -> throw new IllegalArgumentException();
  };
}
public String type(Person p) {
  return switch (p) {
    case User user -> "user"; // Noncompliant:  "user" is unused replace it with the unnamed variable pattern (starting from Java 22)
    default -> throw new IllegalArgumentException();
  };
}
public int age(Person p) {
  if (p instanceof User(String name, int age)) { // Noncompliant:  "name" is unused replace it with the unnamed variable pattern (starting from Java 22)
    return age;
  }
}
public static int count(int[] elements) {
  int count = 0;
  for (var el : elements) { // Noncompliant:  "el" is unused replace it with the unnamed variable pattern (starting from Java 22)
    count++;
  }
  return count;
}
public void foo() {
  try (var file = Files.createTempFile(directory, "temp", ".txt")) { // Noncompliant:  "file" is unused replace it with the unnamed variable pattern (starting from Java 22)
    System.out.println("file created");
  }
}

Compliant solution

public int numberOfMinutes(int hours) {
  return hours * 60;
}
public String name(Person p) {
  return switch (p) {
    case User(String name, _) -> name; // Compliant
    default -> throw new IllegalArgumentException();
  };
}
public String type(Person p) {
  return switch (p) {
    case User _ -> "user"; // Compliant
    default -> throw new IllegalArgumentException();
  };
}
public int age(Person p) {
  if (p instanceof User(String _, int age)) { // Compliant
    return age;
  }
}
public static int count(int[] elements) {
  int count = 0;
  for (var _ : elements) { // Compliant
    count++;
  }
  return count;
}
public void foo() {
  try (var _ = Files.createTempFile(directory, "temp", ".txt")) { // Compliant
    System.out.println("file created");
  }
}