A method callers rely on for a description or a copy hands back null

tx0/java:null-from-to-stringrecommendedwarningbug

What it finds

toString() and clone() that return null break what callers rely on: string concatenation prints null, String.valueOf and loggers may fail, and a clone is expected to be a copy. toString() returns some description, and clone() a copy or an exception.

A return null in a catch block, as after a CloneNotSupportedException that cannot happen, is left alone.

From the rule's source, null_from_to_string.rs.

Example from the rule's tests
class Node implements Cloneable {  String name;   @Override  public String toString() {    if (name == null) {      return null;tx0 reports: toString() returns null    }    java.util.function.Supplier<String> s = () -> {      return null;    };    return name;  }   @Override  public Node clone() {    return null;tx0 reports: clone() returns null  }}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:null-from-to-string" = "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:null-from-to-string] reason="..."

tx0 explain tx0/java:null-from-to-string shows the same in the terminal. Found a false positive? Report it.