Inspectopedia
 
2024.3

Ambiguous non-local 'break' or 'continue'

Info
New
Last modified: 03 December 2024

Reports break or continue usages inside of lambdas of loop-like functions.

break and continue keywords always apply to the real loops (for, while, do-while). break and continue never apply to any function; for example, break and continue don't apply to forEach, filter, map.

Using break or continue inside a loop-like function (for example, forEach) may be confusing. The inspection suggests adding a label to clarify to which statement break or continue applies to.

Since Kotlin doesn't have a concept of loop-like functions, the inspection uses the heuristic. It assumes that functions that don't have one of callsInPlace(EXACTLY_ONCE) or callsInPlace(AT_LEAST_ONCE) contracts are loop-like functions.

Example:

The quick-fix adds clarifying labels: