You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] AvoidArrayLoops: False negative when break inside switch statement #5877
Description:
The AvoidArrayLoops is not triggered when there is a break statement inside a switch-case statement in the loop. In the following code, if we remove/comment out the break statement, the issue is triggered at line 7. I suspect the reason could be that the XPath expression explicitly checks [not(.//BreakStatement)], regardless of whether the break is exiting the loop or the switch statement.
Code Sample demonstrating the issue:
importjava.util.Arrays;
classArrayLoopBugDemo {
publicint[] showBug(int[] source, intcondition) {
int[] destination = newint[source.length];
for (inti = 0; i < source.length; i++) { // should raise AvoidArrayLoops heredestination[i] = source[i];
switch (condition) {
case0:
System.out.println("This is an unreachable switch case.");
break;
}
}
returndestination;
}
publicstaticvoidmain(String[] args) {
ArrayLoopBugDemodemo = newArrayLoopBugDemo();
int[] dest = demo.showBug(newint[]{1, 2, 3, 4, 5}, 1);
}
}
Expected outcome:
PMD should report a violation at line 7, but doesn't. This is a false-negative.
changed the title [-][java] AvoidArrayLoops false-negative when break inside switch statement[/-][+][java] AvoidArrayLoops: False negative when break inside switch statement[/+]on Dec 24, 2025
Activity
[-][java] AvoidArrayLoops false-negative when break inside switch statement[/-][+][java] AvoidArrayLoops: False negative when break inside switch statement[/+][doc] Update release notes (#5877, #6316)
[java] Fix #5877: AvoidArrayLoops false-negative when break inside sw…
RM-37163: CPD 7.21.0 upgrade (#8)