This type can be more specific.


Alternate names: MutableConstantField, MutableMethodReturnType

The problem

Where possible, prefer referring to some types by a subtype which conveys more information. For example: ImmutableList conveys more information (the immutability of the type). than List, List conveys more information than Collection, and ListMultimap conveys more information than Multimap.

This is consistent with Effective Java 3rd Edition §64, which says to refer to objects by their interfaces.

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.