Pattern: Missing NoSuchElementException
for Iterator
Issue: -
Reports implementations of the Iterator interface which do not throw a NoSuchElementException
in the
implementation of the next()
method. When there are no more elements to return an Iterator should throw a
NoSuchElementException
.
Example of incorrect code:
class MyIterator : Iterator<String> {
override fun next(): String {
return ""
}
}
Example of correct code:
class MyIterator : Iterator<String> {
override fun next(): String {
if (!this.hasNext()) {
throw NoSuchElementException()
}
// ...
}
}