[false-negative]S1656 can not detect static variables self-assigned in a special case: foo = foo = bar

Affects Sonarqube Version:
Sonarqube version:
sonar-scanner version:

S1656: Variables should not be self-assigned

Unable to detect static variables self-assigned in a special case: foo = foo = bar.
This rule is implemented in java-checks-!\org\sonar\java\checks\SelfAssignementCheck.java.
This rule uses if (SyntacticEquivalence.areEquivalent(node.expression(), node.variable())) { to determine whether the two sides of assignment statements are the same.
In the special case of foo = foo = bar, the left and right sides of the first equal sign are different, so it can not be detected

Code Sample demonstrating the issue:

Example 1:
while (x > 0 && x < screensize().width) {
	x = x = vx;
	g.drawImage(c.getCardImage(cardBack), x, y, this);

Example 2:
if (currentCount >= estimatedTotalCount) {
	estimatedTotalCount = estimatedTotalCount *= 2;

Expected outcome:

Running Sonarqube through:
command line

Hi wongwuchiu,

Thank you for the detailed report. I have had a look into the issue you are reporting and while you are right that S1656 does not trigger in that case, S4165 does and indicates that the useless assignment should be removed. As such, it is not a problem that S1656 doesn’t trigger here. Have you perhaps disabled S4165?

This topic was automatically closed after 18 days. New replies are no longer allowed.