Self-assignment

ID

java.self_assignment

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:480, reliability, suspicious-comparison

Description

Reports assignments where the left-hand side and right-hand side are the same expression (e.g., x = x). Such a statement has no effect and is almost always a bug.

Rationale

A self-assignment is a no-op: the value of the variable is unchanged. This pattern typically arises from a copy-paste error or a typo where the developer intended to assign from a different variable or qualify one side with this..

// Bad - no-op
void setName(String name) {
    name = name; // intended: this.name = name
}

Remediation

Verify the intended source and target of the assignment. Usually one side needs to be qualified with this. or the variable name needs to differ.

// Good
void setName(String name) {
    this.name = name;
}