An array of primitives passed to Arrays.asList becomes the single element of the list

tx0/java:primitive-array-as-listdefaultwarningbug

What it finds

Arrays.asList(counts) with an int[] makes a list with one element, the array itself: the varargs parameter takes objects, and a primitive array is one object. Arrays.stream(counts).boxed() or a loop gives the elements. List.of and Stream.of are left alone, since lists of arrays are often made with them on purpose.

Reported when the only argument is a variable declared as an array of a primitive type.

From the rule's source, primitive_array_as_list.rs.

Example from the rule's tests
import java.util.Arrays;import java.util.List; class Counts {  void show(int[] counts, Integer[] boxed, int... more) {    List<int[]> a = Arrays.asList(counts);tx0 reports: Arrays.asList(counts) holds the array as its only element    List<Integer> b = Arrays.asList(boxed);    var c = List.of(more);    long d = java.util.stream.Stream.of(counts).count();  }}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:primitive-array-as-list" = "off"

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:primitive-array-as-list] reason="..."

tx0 explain tx0/java:primitive-array-as-list shows the same in the terminal. Found a false positive? Report it.