Fix #3076 - UnusedAssignment false positive in for-each assignment

This commit is contained in:
Clément Fournier
2021-06-23 23:51:15 +02:00
parent b71c1cf92e
commit f84cd0e36d
2 changed files with 24 additions and 0 deletions

View File

@ -859,6 +859,7 @@ public class UnusedAssignmentRule extends AbstractJavaRule {
}
private SpanInfo checkIncOrDecrement(JavaNode unary, SpanInfo data) {
super.visit(unary, data);
ASTVariableDeclaratorId var = getVarFromExpression(unary.getChild(0), true, data);
if (var != null) {
data.use(var);

View File

@ -3316,5 +3316,28 @@ public class UnusedAssignmentNative {
}
]]></code>
</test-code>
<test-code>
<description>[java] UnusedAssignment false positive in for-each assignment #3076</description>
<rule-property name="reportUnusedVariables">true</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static java.lang.System.arraycopy;
import static java.util.Arrays.fill;
public class RadixSort {
private static final int TEN = 10;
int[] a;
void countSort(int exp) {
int[] output = new int[a.length];
int[] count = new int[TEN];
fill(count, 0);
for (int val : a) // error flagged here
count[(val / exp) % TEN]++;
}
}
]]></code>
</test-code>
</test-data>