Variable assigned to itself
ID |
scala.correctness.self_assignment |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Suspicious Code |
Language |
Scala |
Description
Reports an assignment whose right-hand side is the same expression as its left-hand side, such as offset = offset or this.count = this.count.
Rationale
The statement has no effect, so either it is dead code or — far more often — it is a typo for an assignment that was meant to happen. The classic case is a constructor or setter that shadows a field with a parameter of the same name: offset = offset then assigns the parameter to itself and the field silently keeps its old value, producing a defect that no test on the setter can see.
The following code illustrates the pattern detected by this rule:
class Cursor {
private var offset: Int = 0
private var limit: Int = 0
def reset(offset: Int): Unit = {
// FLAGGED: Variable assigned to itself
offset = offset
limit = 0
}
}