Skip to content

Commit dc3ad5e

Browse files
authored
Fix a bug in IteratorPlusOne (#1423)
`next()` should throw a `NoSuchElementException` when there are no more elements. Before it returned `null`.
1 parent bb57cde commit dc3ad5e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

util/src/main/java/com/ibm/wala/util/collections/IteratorPlusOne.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package com.ibm.wala.util.collections;
1212

1313
import java.util.Iterator;
14+
import java.util.NoSuchElementException;
1415
import org.jspecify.annotations.Nullable;
1516

1617
/** A utility to efficiently compose an iterator and a singleton */
@@ -37,15 +38,18 @@ public boolean hasNext() {
3738
return it.hasNext() || (xtra != null);
3839
}
3940

40-
@Nullable
4141
@Override
4242
public T next() {
4343
if (it.hasNext()) {
4444
return it.next();
4545
} else {
4646
T result = xtra;
47-
xtra = null;
48-
return result;
47+
if (result != null) {
48+
xtra = null;
49+
return result;
50+
} else {
51+
throw new NoSuchElementException();
52+
}
4953
}
5054
}
5155

0 commit comments

Comments
 (0)