Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ForEachOnRange on Range.toList() #516

Open
roman-wedemeier opened this issue Jul 17, 2024 · 0 comments
Open

ForEachOnRange on Range.toList() #516

roman-wedemeier opened this issue Jul 17, 2024 · 0 comments

Comments

@roman-wedemeier
Copy link

When i write

fun main() {
    (0..9).toList().forEach { println(it) }
}

Then the part (0..9).toList() is marked with the following message:
detekt - ForEachOnRange: Using the forEach method on ranges has a heavy performance cost. Prefer using simple for loops.

I would expect, that the toList() Method creates a List<Int>, where i can do the forEach safely.

When i write

fun main() {
    val list = (0..9).toList()

    list.forEach { println(it) }
}

no warning comes up (despite another warning complaining the magic number 9, but this is another Story).

Did i understand something wrong?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant