The Object[] from toArray() is cast to a more specific array type
What it finds
(String[]) names.toArray() fails with a ClassCastException: toArray() without an argument always creates an Object[], whatever the collection holds, and an Object[] cannot be cast to a more specific array type. names.toArray(new String[0]) creates an array of the right type.
A cast to an array of a type variable, as (E[]), is unchecked and does not fail, and is left alone.
From the rule's source, untyped_to_array_cast.rs.
import java.util.List; class Names { String[] all(List<String> names) { String[] a = (String[]) names.toArray();tx0 reports: the Object[] from toArray() cannot be cast to String[] Object[] b = (Object[]) names.toArray(); String[] c = names.toArray(new String[0]); return a; }} 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:
[rules]
"tx0/java:untyped-to-array-cast" = "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:untyped-to-array-cast] reason="..."tx0 explain tx0/java:untyped-to-array-cast shows the same in the terminal. Found a false positive? Report it.