Back to Intellij Community

UnnecessaryLocalVariable

java/java-impl/resources/inspectionDescriptions/UnnecessaryLocalVariable.html

2025.3-rc-2811 B
Original Source

Reports unnecessary local variables that add nothing to the comprehensibility of a method, including:

  • Local variables that are immediately returned.
  • Local variables that are immediately assigned to another variable and then not used.
  • Local variables that always have the same value as another local variable or parameter.

Example:

boolean yes() {
    boolean b = true;
    return b;
  }

After the quick-fix is applied:

boolean yes() {
    return true;
  }

Configure the inspection:

  • Use the Ignore immediately returned or thrown variables option to ignore immediately returned or thrown variables. Some coding styles suggest using such variables for clarity and ease of debugging.
  • Use the Ignore variables which have an annotation option to ignore annotated variables.