menu

suspicious-operator-implementation

Disallow direct operator implementations built on a different operator

Directly combining an operator receiver and operand with a different operator commonly indicates a copied or mistyped body. Instead, you SHOULD use the operator implemented by the enclosing protocol.

Reported

1struct Score {2    value: int32;3}4 5extension of Score implements Add<Score> {6    type Output = Score;7 8    add(&readonly this, other: Score): Score {9        return Score { value: this.value - other.value };10    }11}

Accepted

1struct Score {2    value: int32;3}4 5extension of Score implements Add<Score> {6    type Output = Score;7 8    add(&readonly this, other: Score): Score {9        return Score { value: this.value + other.value };10    }11}