Why is this an issue?

If a collection is declared and populated but its values are never read anywhere in the code, it can be considered unused. This often arises from incomplete refactoring, copy-pasting errors, or typos. Unused collections can lead to wasted memory and degraded application performance. Additionally, their presence makes the code harder to read and understand.

How to fix it

Remove unused collection.

Code examples

Noncompliant code example

int getLength(String a, String b, String c) {
  List<String> strings = new ArrayList<>();  // Noncompliant: List is declared and populated but never read.
  strings.add(a);
  strings.add(b);
  strings.add(c);

  return a.length() + b.length() + c.length();
}

Compliant solution

int getLength(String a, String b, String c) {
  return a.length() + b.length() + c.length();
}