This type can be more specific.


Alternate names: MutableMethodReturnType

The problem

Prefer referring to types by the immutable type (such as ImmutableList) instead of the general collection interface type (such as List). This communicates important semantic guarantees.

This is consistent with Effective Java 3rd Edition §64, which says to refer to objects by their interfaces. Guava’s immutable collection classes offer meaningful behavioral guarantees – they are not merely a specific implementation as in the case of, say, ArrayList. They should be treated as interfaces in every important sense of the word.

That is, prefer this:

ImmutableList<String> getCountries() {
  return ImmutableList.of("Denmark", "Norway", "Sweden");

to this:

List<String> getCountries() {
  return ImmutableList.of("Denmark", "Norway", "Sweden");

TIP: Using the immutable type for the method return type allows Error Prone to prevent accidental attempts to modify the collection at compile-time (see DoNotCall).


Suppress false positives by adding the suppression annotation @SuppressWarnings("PreferredInterfaceType") to the enclosing element.