Skip to content

Commit ea7eb0c

Browse files
SONARJAVA-5630 Check that iterator contains a next element before calling next (#5199)
1 parent 381a07c commit ea7eb0c

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

java-checks/src/main/java/org/sonar/java/checks/RedundantJumpCheck.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Arrays;
2020
import java.util.HashSet;
21+
import java.util.Iterator;
2122
import java.util.List;
2223
import java.util.Set;
2324
import org.sonar.check.Rule;
@@ -58,8 +59,8 @@ private void checkBlock(Block block) {
5859
&& !isSwitchCaseChild(terminator)) {
5960

6061
successorWithoutJump = nonEmptySuccessor(successorWithoutJump);
61-
Block successor = nonEmptySuccessor(block.successors().iterator().next());
62-
if (successorWithoutJump.equals(successor)) {
62+
Iterator<Block> successors = block.successors().iterator();
63+
if (successors.hasNext() && nonEmptySuccessor(successors.next()).equals(successorWithoutJump)) {
6364
reportIssue(terminator, "Remove this redundant jump.");
6465
}
6566
}

java-checks/src/test/files/checks/RedundantJumpCheck.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,11 @@ public void throwing_exception() {
120120

121121
public abstract void abstract_method();
122122

123+
void undefinedLabel(int values[]) {
124+
for (int v: values) {
125+
if (v == 2) {
126+
continue label; // this should be a compiler error, but we want to test that the check does not crash
127+
}
128+
}
129+
}
123130
}

0 commit comments

Comments
 (0)