A copy constructor leaves out a field of its class

tx0/java:incomplete-copy-constructorrecommendedwarningbug

What it finds

A copy constructor, Point(Point other), promises an object equal to other. One that leaves out a field gives a copy with that field at its default value, which shows up only when the field is read. It usually happens when a field is added to the class later.

Reported is a field of the class that a constructor with a single parameter of the class's own type does not mention, when that constructor reads at least two fields of its parameter, which makes it a copy rather than a link to a parent. Static and transient fields, final fields with an initializer, and constructors that hand the parameter to another method or constructor, or call a method of their own, which may copy the rest, are left alone.

From the rule's source, incomplete_copy_constructor.rs.

Example from the rule's tests
class Settings {  private String host;  private int port;  private boolean secure;  private static int created;  private final int version = 1;   Settings(Settings other) {tx0 reports: does not copy secure    this.host = other.host;    this.port = other.port;  }   Settings(Settings other, int port) {    this.host = other.host;    this.port = port;  }} class Profile {  private String name;  private int age;   Profile(Profile other) {    copy(other);  }   void copy(Profile other) {    name = other.name;    age = other.age;  }}
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:incomplete-copy-constructor" = "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:incomplete-copy-constructor] reason="..."

tx0 explain tx0/java:incomplete-copy-constructor shows the same in the terminal. Found a false positive? Report it.