KT.ITERATOR_HAS_NEXT_CALLS_NEXT_METHOD

Side effect in hasNext() method of an Iterator implementation

Verifies implementations of the Iterator interface. The hasNext() method of an Iterator implementation should not have any side effects. This rule reports implementations that call the next() method of the Iterator inside the hasNext() method.

Noncompliant Code

Copy
class MyIterator : Iterator<String> {

    override fun hasNext(): Boolean {
        return next() != null
    }
}

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html