Skip to content

exit-or-cycle-in-unlabelled-loop (C142)

What does it do?

Checks for exit or cycle in unnamed do loops

Why is this bad?

Using loop labels with exit and cycle statements prevents bugs when exiting the wrong loop, and helps readability in deeply nested or long loops. The danger is particularly enhanced when code is refactored to add further loops.

Example

do i = 1, 10
  do j = 1, 10
    if (i + j > 5) cycle
  end do
end do

Use instead:

do i = 1, 10
  inner: do j = 1, 10
    if (i + j > 5) cycle inner
  end do inner
end do

Settings

See allow-unnested-loops