[java S5413] FP when outer loop is ascending

Qube: Communit 9.9

When there are two nested loops, with the outer loop being ascending and the inner descending, the Java rule S5413 reports a FP.

Example:

for (int i = 0; i < 10; i++)
{
    for (int j = list.size() - 1; j >= 0; j-- )
    {
        if (j == 5)
            list.remove(j); // FP
    }
}

Ok, this even goes a litte bit further. If there is only one loop with a remove() fully independent of the loop, the rule will hit.

for (int i = 0; i < 100; i++)
    list.remove(3); // FP

Hello @mfroehlich,

Thank you for spotting this. This is indeed an FP. I’ve created a ticket to fix it:

https://sonarsource.atlassian.net/browse/SONARJAVA-4599

Best,
Margarita

I have another one for you.

for (int i = 0, j = 0; i < 10; i++)
    j++; // FP

Hello @mfroehlich,

I’m not sure I completely understood what is exactly the problem here. Could you, please, provide a complete reproducer, if you’re talking about the same rule?

If you’re talking about a different rule, I suggest opening a new thread to not mix different issues in one place.

Best,
Margarita

It’s definitely about this rule. I will have a second look, if this reduced testcase will reproduce the FP.

Point is, that in the loop body, j is incremented, but it is not the loop counter, especially not in the loop condition.

Then you, don’t need to create another example, it’s the same issue. Currently, the rule doesn’t take into account the argument of remove at all. So whatever you put there may trigger the issue.

Best,
Margarita