A variable is assigned to itself
What it finds
x = x, this.size = this.size, a[i] = a[i]: assigning something to itself does nothing, and usually stands for an assignment to something else.
The common case is a constructor or setter whose parameter is named differently from the field, as this.name = name without a parameter name: the name then refers to the field. That is reported only when the field is declared in the class of this and nothing closer declares the name, so that the name certainly is the field and not an inherited one or a parameter.
Targets that call methods or create objects on the way, as next().value = next().value, may name different variables on each side and are left alone, and so are volatile fields, whose writes are seen by other threads and can be the point.
From the rule's source, self_assignment.rs.
class Point { private int x; private int y; private volatile int version; private final int[] cells = new int[4]; Point(int x, int yValue) { this.x = x; this.y = y;tx0 reports: this.y is assigned to itself x = x;tx0 reports: x is assigned to itself this.x = this.x;tx0 reports: this.x is assigned to itself cells[x] = cells[x];tx0 reports: cells[x] is assigned to itself version = version; y = yValue; } void move(int dx) { int x = dx; this.x = x; x += x; next().x = next().x; } Point next() { return this; }} class Inheriting extends Point { Inheriting() { super(1, 2); } int size; void resize(int length) { this.size = length; this.size = size;tx0 reports: this.size is assigned to itself this.length = length; }} 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:self-assignment" = "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:self-assignment] reason="..."tx0 explain tx0/java:self-assignment shows the same in the terminal. Found a false positive? Report it.