The type arguments of an instance creation repeat those of the declared type

tx0/java:diamondstrictwarningstyle

What it finds

List<String> names = new ArrayList<String>(); repeats the type arguments, which the compiler infers from the declared type when they are left out: new ArrayList<>().

Reported are instance creations that initialize a field or local variable whose declared type ends in the same type arguments. Anonymous classes are left alone. The fix writes the diamond, which infers the same arguments.

From the rule's source, diamond.rs.

Example from the rule's tests
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; class Registry {  private final Map<String, List<Integer>> index = new HashMap<String, List<Integer>>();tx0 reports: the type arguments <String, List<Integer>> can be inferred   void fill() {    List<String> names = new ArrayList<String>();tx0 reports: the type arguments <String> can be inferred    List<String> copy = new ArrayList<>(names);    List<? extends Number> numbers = new ArrayList<Integer>();    Runnable r = new Runnable() {      public void run() {}    };    var all = new ArrayList<String>();  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:diamond" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:diamond] reason="..."

tx0 explain tx0/java:diamond shows the same in the terminal. Found a false positive? Report it.